Advertisement
Guest User

Untitled

a guest
Jul 12th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.41 KB | None | 0 0
  1. // Assignment 1 19T2 COMP1511: CS Paint
  2. // paint.c
  3. //
  4. // This program was written by Shania Darmadi (z5213194)
  5. // on 4-07-2019
  6. //
  7. // Version 1.0.0 (2019-06-22): Assignment released.
  8.  
  9. #include <stdio.h>
  10.  
  11. // The dimensions of the canvas (20 rows x 36 columns).
  12. #define N_ROWS 20
  13. #define N_COLS 36
  14.  
  15. // Shades (assuming your terminal has a black background).
  16. #define WHITE 4
  17.  
  18.  
  19. // Print out the canvas.
  20. void printCanvas(int canvas[N_ROWS][N_COLS]);
  21.  
  22. void setBlankCanvas(int canvas[N_ROWS][N_COLS]);
  23.  
  24. //Draw a line
  25. void drawLine(int startRow, int startCol, int endRow, int endCol,
  26. int shade, int canvas[N_ROWS][N_COLS]);
  27.  
  28. //Fill a rectangle
  29. void fillRectangle(int startRow, int startCol, int endRow, int endCol,
  30. int shade, int canvas[N_ROWS][N_COLS]);
  31.  
  32.  
  33.  
  34. int main(void) {
  35.  
  36. int canvas[N_ROWS][N_COLS];
  37.  
  38. setBlankCanvas(canvas);
  39.  
  40. //determine the variables
  41. int startRow;
  42. int endRow;
  43. int startCol;
  44. int endCol;
  45. int instruction;
  46. int result;
  47. int shade = 0;
  48.  
  49. //input the data from the user
  50.  
  51. result = scanf("%d ", &instruction);
  52.  
  53. //conditions from the input
  54. while(result == 1) {
  55.  
  56. //Draw a Line
  57. if (instruction == 1) {
  58.  
  59. scanf("%d %d %d %d", &startRow, &startCol, &endRow, &endCol);
  60.  
  61. drawLine(startRow,startCol,endRow, endCol, shade, canvas);
  62.  
  63. result = scanf("%d ", &instruction);
  64.  
  65. }
  66.  
  67. //Fill Rectangle
  68. else if (instruction == 2){
  69.  
  70. scanf("%d %d %d %d", &startRow, &startCol, &endRow, &endCol);
  71.  
  72. fillRectangle(startRow,startCol,endRow, endCol, shade, canvas);
  73.  
  74. result = scanf("%d", &instruction);
  75.  
  76. }
  77.  
  78. //Change Shade
  79. else if (instruction == 3) {
  80.  
  81. scanf("%d", &shade);
  82.  
  83. // making sure that the shade is not out of range
  84. if (shade < 0 || shade > 4) {
  85. shade = 4;
  86. }
  87. // printf("Shade: %d\n", shade);
  88. result = scanf("%d", &instruction);
  89. }
  90.  
  91. //Copy Paste
  92. else if (instruction == 4) {
  93. // printf("Shade: %d\n", shade);
  94.  
  95. //determine the variable from the initial input
  96. int targetRow;
  97. int targetCol;
  98. int startRow;
  99. int startCol;
  100. int endRow;
  101. int endCol;
  102.  
  103.  
  104. scanf("%d %d %d %d %d %d", &startRow, &startCol, &endRow, &endCol, &targetRow, &targetCol);
  105. // printf("%d %d %d %d %d %d\n", startRow, startCol, endRow, endCol, targetRow, targetCol);
  106.  
  107. int smallerRow = 0;
  108. int biggerRow = 0;
  109. int smallerColumn = 0;
  110. int biggerColumn = 0;
  111.  
  112. //figuring out which one is bigger to set the target position
  113.  
  114. //invalid lines
  115. int flag = 0;
  116.  
  117. //checking if the starting point or ending point cannot be negative
  118. if (startCol < 0 || endCol < 0 || startRow < 0 || endRow < 0 || targetRow < 0|| targetCol < 0) {
  119. flag = 1;
  120. }
  121. //checking if the starting point or ending point cannot be greater than the canvas
  122. else if( startCol >= N_COLS || endCol >= N_COLS ||startRow >= N_ROWS || endRow >= N_ROWS || targetRow >= N_ROWS|| targetCol >= N_COLS){
  123. flag = 1;
  124. }
  125.  
  126. //find the row position
  127. if (startRow > endRow) {
  128. // printf("startRow > endRow\n");
  129. smallerRow = endRow;
  130. biggerRow = startRow;
  131. if (startCol > endCol) {
  132. // printf("startCol > endCol\n");
  133. smallerColumn = endCol;
  134. biggerColumn = startCol;
  135. } else if (endCol > startCol) {
  136. // printf("endCol > startCol\n");
  137. smallerColumn = startCol;
  138. biggerColumn = endCol;
  139. }
  140.  
  141. } else if (endRow > startRow) {
  142. // printf("endRow > startRow\n");
  143. smallerRow = startRow;
  144. biggerRow = endRow;
  145. if (startCol > endCol) {
  146. // printf("startCol > endCol\n");
  147. smallerColumn = endCol;
  148. biggerColumn = startCol;
  149. } else if (endCol > startCol) {
  150. // printf("endCol > startCol\n");
  151. smallerColumn = startCol;
  152. biggerColumn = endCol;
  153. }
  154. }
  155.  
  156. // printf("smallerRow: %d\n", smallerRow);
  157. // printf("biggerRow: %d\n", biggerRow);
  158. // printf("smallerColumn: %d\n", smallerColumn);
  159. // printf("biggerColumn: %d\n", biggerColumn);
  160.  
  161. //setting the smaller row and column to bethe size of the canvas
  162.  
  163. int width = biggerColumn-smallerColumn;
  164. int height = biggerRow-smallerRow;
  165. int targetPosition[height+1][width+1];
  166.  
  167. // printf("Target position height: %d\n", height);
  168. // printf("Target position width: %d\n", width);
  169.  
  170. // Canvas counters
  171. int copy_start_row = smallerRow;
  172. int copy_start_column = smallerColumn;
  173. int copy_end_row = copy_start_row + height + 1;
  174. int copy_end_col = copy_start_column + width + 1;
  175.  
  176. // Target counters
  177. int start_row = 0;
  178. int start_column = 0;
  179.  
  180. int i = copy_start_row;
  181. int k = start_row;
  182. while (i < copy_end_row && flag == 0) {
  183. // printf("i: %d\n", i);
  184. int j = copy_start_column;
  185. int l = start_column;
  186. while (j < copy_end_col) {
  187. // printf("j: %d\n", j);
  188. targetPosition[k][l] = canvas[i][j];
  189. j++;
  190. l++;
  191. }
  192. i++;
  193. k++;
  194. }
  195.  
  196. // Copy to canvas variables
  197. int start_paste_row = targetRow;
  198. int start_paste_col = targetCol;
  199. int end_paste_row = targetRow + height + 1;
  200. int end_paste_col = targetCol + width + 1;
  201.  
  202. // Copying from targetPosition to canvas variables
  203. int paste_row = 0;
  204. int paste_col = 0;
  205.  
  206. while (start_paste_row < end_paste_row && flag == 0) {
  207. start_paste_col = targetCol;
  208. paste_col = 0;
  209. while (start_paste_col < end_paste_col) {
  210. canvas[start_paste_row][start_paste_col] = targetPosition[paste_row][paste_col];
  211. start_paste_col++;
  212. paste_col++;
  213. }
  214. start_paste_row++;
  215. paste_row++;
  216. }
  217.  
  218. // printf("Wait for input\n");
  219. result = scanf("%d", &instruction);
  220.  
  221. }
  222.  
  223.  
  224. }
  225. printCanvas(canvas);
  226. return 0;
  227. }
  228.  
  229.  
  230.  
  231. void drawLine(int startRow, int startCol, int endRow, int endCol, int shade, int canvas[N_ROWS][N_COLS]) {
  232. int row = 0;
  233.  
  234. //invalid lines
  235. //checking if the starting point or ending point cannot be negative
  236. if (startCol < 0 || endCol < 0 || startRow < 0 || endRow < 0 ){
  237. return;
  238. }
  239. //checking if the starting point or ending point cannot be greater than the canvas
  240. else if( startCol >= N_COLS || endCol >= N_COLS ||startRow >= N_ROWS || endRow >= N_ROWS ){
  241. return;
  242.  
  243. }
  244.  
  245. //valid lines
  246. int i=0;
  247. while (row < N_ROWS) {
  248. //draw a line
  249. int col = 0;
  250. while (col < N_COLS) {
  251.  
  252. int gradient = 0;
  253.  
  254. if (endRow - startRow != 0) {
  255. gradient = (endCol - startCol) / (endRow - startRow);
  256. }
  257.  
  258. //diagonal
  259. if (gradient == 1 || gradient == -1) {
  260.  
  261. if (gradient == -1) {
  262.  
  263. if (startRow < endRow && startCol > endCol) {
  264. if (row >= startRow && row <= endRow) {
  265. if (col >= endCol && col <= startCol) {
  266. if (row == startRow + i && col == startCol - i) {
  267. canvas[row][col]= shade;
  268. i++;
  269. }
  270. }
  271. }
  272. }
  273. } else {
  274. if (startRow == startCol && endRow == endCol) {
  275. if (startCol > endCol) {
  276. if (row >= endRow && row <= startRow) {
  277. if (col >= endCol && col <= startCol) {
  278. if (row == col) {
  279. canvas[row][col]= shade;
  280. }
  281. }
  282. }
  283. }
  284. if (row >= startRow && row <= endRow) {
  285. if (col >= startCol && col <= endCol) {
  286. if (row == col) {
  287. canvas[row][col]= shade;
  288. }
  289. }
  290. }
  291. } else if (startRow < endRow && startCol < endCol) {
  292. if (row >= startRow && row <= endRow) {
  293. if (col >= startCol && col <= endCol) {
  294. if (row == startRow + i && col == startCol + i) {
  295. canvas[row][col]= shade;
  296. i++;
  297. }
  298. }
  299. }
  300. }
  301. }
  302. }
  303.  
  304. //horizontal
  305. if (startRow == endRow && row == startRow ) {
  306. if (col >= startCol && col <= endCol){
  307. canvas[row][col]= shade;
  308. } else if (col <= startCol && col >= endCol){
  309. canvas[row][col]= shade;
  310. }
  311.  
  312. } //vertical
  313. else if (startCol == endCol && col == startCol) {
  314. if (row >= startRow && row <= endRow){
  315. canvas[row][col]= shade;
  316. } else if (row <= startRow && row >= endRow) {
  317. canvas[row][col]= shade;
  318. }
  319.  
  320. }
  321.  
  322.  
  323. col++;
  324.  
  325. }
  326. row++;
  327. }
  328. }
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335. void fillRectangle(int startRow, int startCol, int endRow, int endCol, int shade, int canvas[N_ROWS][N_COLS]) {
  336.  
  337. int row = 0;
  338.  
  339. //checking if the starting point or ending point cannot be negative
  340. if (startCol < 0 || endCol < 0 || startRow < 0 || endRow < 0 ){
  341. return;
  342. }
  343. //checking if the starting point or ending point cannot be greater than the canvas
  344. else if( startCol >= N_COLS || endCol >= N_COLS ||startRow >= N_ROWS || endRow >= N_ROWS ){
  345. return;
  346.  
  347. }
  348.  
  349. while (row < N_ROWS) {
  350.  
  351. int col = 0;
  352.  
  353. while (col < N_COLS) {
  354. if (row >= startRow && row <= endRow) {
  355. if (col >= startCol && col <= endCol){
  356. canvas[row][col]= shade;
  357. } else if (col <= startCol && col >= endCol){
  358. canvas[row][col]= shade;
  359. }
  360. } else if (col >= startCol && col <= endCol) {
  361. if (row >= startRow && row <= endRow) {
  362. canvas[row][col]= shade;
  363. } else if (row <= startRow && row >= endRow) {
  364. canvas[row][col]= shade;
  365. }
  366. }
  367. col++;
  368. }
  369. row++;
  370. }
  371. }
  372.  
  373.  
  374.  
  375.  
  376. // Prints the canvas, by printing the integer value stored in
  377. // each element of the 2-dimensional canvas array.
  378. //
  379. // You should not need to change the printCanvas function.
  380. void printCanvas(int canvas[N_ROWS][N_COLS]) {
  381. int row = 0;
  382. while (row < N_ROWS) {
  383. int col = 0;
  384. while (col < N_COLS) {
  385. printf("%d ", canvas[row][col]);
  386. col++;
  387. }
  388. row++;
  389. printf("\n");
  390. }
  391. }
  392.  
  393.  
  394. // Sets the entire canvas to be blank, by setting each element in the
  395. // 2-dimensional canvas array to be WHITE (which is #defined at the top
  396. // of the file).
  397. //
  398. // You should not need to change the setBlankCanvas function.
  399. void setBlankCanvas(int canvas[N_ROWS][N_COLS]) {
  400. int row = 0;
  401. while (row < N_ROWS) {
  402. int col = 0;
  403. while (col < N_COLS) {
  404. canvas[row][col] = WHITE;
  405. col++;
  406. }
  407. row++;
  408. }
  409. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement