Advertisement
Guest User

Canvass.cpp

a guest
Dec 17th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. #include "Canvas.h"
  2.  
  3. bool Canvas::growHoriztontal()
  4. {
  5. if (getWidth() < MAX_HORI) {
  6. for (unsigned int i = 0; i < getHeight(); i++) {
  7. _canvas[i].push_back(false);
  8. }
  9. return true;
  10. }
  11. return false;
  12. }
  13.  
  14. bool Canvas::growVertical()
  15. {
  16. if (getHeight() != MAX_VERTI) {
  17. _canvas.push_back(std::vector<bool>(getWidth(), false));
  18. return true;
  19. }
  20. return false;
  21. }
  22.  
  23. bool Canvas::processDrawAction(PEN_AXIS plane, int extent)
  24. {
  25. if (plane == DEPTH) {
  26. if (_currInstruction != NULL) {
  27. _instructions.push_back(_currInstruction);
  28. }
  29. if (extent == 1) {
  30. _instructions.push_back((new Instruction(SET, _pen._x, _pen._y)));
  31. }
  32. }
  33. else if (isPenDown()) {
  34. INSTRUCTION newInstruction = plane == HORI ? DRH : DRV;
  35. if (_currInstruction != NULL) {
  36. if (_currInstruction->cmd == newInstruction) {
  37. _currInstruction->arg1 += extent;
  38. }
  39. else {
  40. _instructions.push_back(_currInstruction);
  41. _currInstruction = new Instruction(newInstruction, extent);
  42. }
  43. }
  44. else {
  45. _currInstruction = new Instruction(newInstruction, extent);
  46. }
  47. return true;
  48. }
  49. return false;
  50. }
  51.  
  52. void Canvas::stop()
  53. {
  54. if (_currInstruction != NULL) {
  55. _instructions.push_back(_currInstruction);
  56. _currInstruction = NULL;
  57. }
  58. }
  59.  
  60. void Canvas::mark()
  61. {
  62. if (isPenDown())
  63. {
  64. _canvas[_pen._y][_pen._x] = true;
  65. }
  66. }
  67.  
  68. Canvas::Canvas()
  69. {
  70. for (unsigned int i = 0; i < INIT_DIM; i++) {
  71. _canvas.push_back(std::vector<bool>(INIT_DIM, false));
  72. }
  73. }
  74.  
  75. void Canvas::draw(std::ostream & sout, bool showDetails)
  76. {
  77. if (showDetails) {
  78. sout << "Pen Position: " << _pen._x << "," << _pen._y << std::endl;
  79. sout << "Image Size:" << getWidth() << "," << getHeight() << std::endl;
  80. }
  81.  
  82. for (unsigned int y = 0; y < getHeight(); y++) {
  83. for (unsigned int x = 0; x < getWidth(); x++) {
  84. if (_pen._show && _pen._x == x && _pen._y == y) {
  85. sout << (_pen._down ? PEN_STATUS_DOWN : PEN_STATUS_UP);
  86. }
  87. else {
  88. sout << (_canvas[y][x] ? '*' : ' ');
  89. }
  90. }
  91. sout << std::endl;
  92. }
  93. }
  94.  
  95. unsigned int Canvas::getWidth()
  96. {
  97. return _canvas[0].size();
  98. }
  99.  
  100. unsigned int Canvas::getHeight()
  101. {
  102. return _canvas.size();
  103. }
  104.  
  105. unsigned int Canvas::getPenX()
  106. {
  107. return _pen._x;
  108. }
  109.  
  110. unsigned int Canvas::getPenY()
  111. {
  112. return _pen._y;
  113. }
  114.  
  115. bool Canvas::movePen(PEN_AXIS pa, bool positive)
  116. {
  117. mark();
  118. if (pa == HORI) {
  119. if (positive) {
  120. if (_pen._x + 1 == getWidth())
  121. {
  122. if (!growHoriztontal()) {
  123. return false;
  124. }
  125. }
  126. _pen._x++;
  127. processDrawAction(pa, 1);
  128. return true;
  129. }
  130. else {
  131. if (_pen._x <= 0) {
  132. return false;
  133. }
  134. _pen._x--;
  135. processDrawAction(pa, -1);
  136. return true;
  137. }
  138. }
  139. else {
  140. if (positive) {
  141. if (_pen._y + 1 == getHeight())
  142. {
  143. if (!growVertical()) {
  144. return false;
  145. }
  146. }
  147. _pen._y++;
  148. processDrawAction(pa, 1);
  149. return true;
  150. }
  151. else {
  152. if (_pen._y <= 0) {
  153. return false;
  154. }
  155. _pen._y--;
  156. processDrawAction(pa, -1);
  157. return true;
  158. }
  159. }
  160.  
  161. return false;
  162. }
  163.  
  164. void Canvas::togglePen()
  165. {
  166. processDrawAction(DEPTH, _pen._down ? -1 : 1);
  167. _pen._down = !_pen._down;
  168. }
  169.  
  170. void Canvas::toggleShowPen()
  171. {
  172. _pen._show = !_pen._show;
  173. }
  174.  
  175. bool Canvas::isPenDown()
  176. {
  177. return _pen._down;
  178. }
  179.  
  180. bool Canvas::doCommand(ACTION a)
  181. {
  182. switch (a) {
  183. case LEFT:
  184. movePen(HORI, false);
  185. break;
  186. case RIGHT:
  187. movePen(HORI, true);
  188. break;
  189. case DOWN:
  190. movePen(VERTI, true);
  191. break;
  192. case UP:
  193. movePen(VERTI, false);
  194. break;
  195. case PEN_TOGGLE:
  196. togglePen();
  197. break;
  198. case PEN_POSITION:
  199. toggleShowPen();
  200. break;
  201. case STOP:
  202. stop();
  203. break;
  204. }
  205.  
  206. return false;
  207. }
  208.  
  209. std::vector<Instruction*> Canvas::getInstructions()
  210. {
  211. return _instructions;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement