Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Assignment 1 19T2 COMP1511: CS Paint
- // paint.c
- //
- // This program was written by Shania Darmadi (z5213194)
- // on 4-07-2019
- //
- // Version 1.0.0 (2019-06-22): Assignment released.
- #include <stdio.h>
- // The dimensions of the canvas (20 rows x 36 columns).
- #define N_ROWS 20
- #define N_COLS 36
- // Shades (assuming your terminal has a black background).
- #define WHITE 4
- // Print out the canvas.
- void printCanvas(int canvas[N_ROWS][N_COLS]);
- void setBlankCanvas(int canvas[N_ROWS][N_COLS]);
- //Draw a line
- void drawLine(int startRow, int startCol, int endRow, int endCol,
- int shade, int canvas[N_ROWS][N_COLS]);
- //Fill a rectangle
- void fillRectangle(int startRow, int startCol, int endRow, int endCol,
- int shade, int canvas[N_ROWS][N_COLS]);
- int main(void) {
- int canvas[N_ROWS][N_COLS];
- setBlankCanvas(canvas);
- //determine the variables
- int startRow;
- int endRow;
- int startCol;
- int endCol;
- int instruction;
- int result;
- int shade = 0;
- //input the data from the user
- result = scanf("%d ", &instruction);
- //conditions from the input
- while(result == 1) {
- //Draw a Line
- if (instruction == 1) {
- scanf("%d %d %d %d", &startRow, &startCol, &endRow, &endCol);
- drawLine(startRow,startCol,endRow, endCol, shade, canvas);
- result = scanf("%d ", &instruction);
- }
- //Fill Rectangle
- else if (instruction == 2){
- scanf("%d %d %d %d", &startRow, &startCol, &endRow, &endCol);
- fillRectangle(startRow,startCol,endRow, endCol, shade, canvas);
- result = scanf("%d", &instruction);
- }
- //Change Shade
- else if (instruction == 3) {
- scanf("%d", &shade);
- // making sure that the shade is not out of range
- if (shade < 0 || shade > 4) {
- shade = 4;
- }
- // printf("Shade: %d\n", shade);
- result = scanf("%d", &instruction);
- }
- //Copy Paste
- else if (instruction == 4) {
- // printf("Shade: %d\n", shade);
- //determine the variable from the initial input
- int targetRow;
- int targetCol;
- int startRow;
- int startCol;
- int endRow;
- int endCol;
- scanf("%d %d %d %d %d %d", &startRow, &startCol, &endRow, &endCol, &targetRow, &targetCol);
- // printf("%d %d %d %d %d %d\n", startRow, startCol, endRow, endCol, targetRow, targetCol);
- int smallerRow = 0;
- int biggerRow = 0;
- int smallerColumn = 0;
- int biggerColumn = 0;
- //figuring out which one is bigger to set the target position
- //invalid lines
- int flag = 0;
- //checking if the starting point or ending point cannot be negative
- if (startCol < 0 || endCol < 0 || startRow < 0 || endRow < 0 || targetRow < 0|| targetCol < 0) {
- flag = 1;
- }
- //checking if the starting point or ending point cannot be greater than the canvas
- else if( startCol >= N_COLS || endCol >= N_COLS ||startRow >= N_ROWS || endRow >= N_ROWS || targetRow >= N_ROWS|| targetCol >= N_COLS){
- flag = 1;
- }
- //find the row position
- if (startRow > endRow) {
- // printf("startRow > endRow\n");
- smallerRow = endRow;
- biggerRow = startRow;
- if (startCol > endCol) {
- // printf("startCol > endCol\n");
- smallerColumn = endCol;
- biggerColumn = startCol;
- } else if (endCol > startCol) {
- // printf("endCol > startCol\n");
- smallerColumn = startCol;
- biggerColumn = endCol;
- }
- } else if (endRow > startRow) {
- // printf("endRow > startRow\n");
- smallerRow = startRow;
- biggerRow = endRow;
- if (startCol > endCol) {
- // printf("startCol > endCol\n");
- smallerColumn = endCol;
- biggerColumn = startCol;
- } else if (endCol > startCol) {
- // printf("endCol > startCol\n");
- smallerColumn = startCol;
- biggerColumn = endCol;
- }
- }
- // printf("smallerRow: %d\n", smallerRow);
- // printf("biggerRow: %d\n", biggerRow);
- // printf("smallerColumn: %d\n", smallerColumn);
- // printf("biggerColumn: %d\n", biggerColumn);
- //setting the smaller row and column to bethe size of the canvas
- int width = biggerColumn-smallerColumn;
- int height = biggerRow-smallerRow;
- int targetPosition[height+1][width+1];
- // printf("Target position height: %d\n", height);
- // printf("Target position width: %d\n", width);
- // Canvas counters
- int copy_start_row = smallerRow;
- int copy_start_column = smallerColumn;
- int copy_end_row = copy_start_row + height + 1;
- int copy_end_col = copy_start_column + width + 1;
- // Target counters
- int start_row = 0;
- int start_column = 0;
- int i = copy_start_row;
- int k = start_row;
- while (i < copy_end_row && flag == 0) {
- // printf("i: %d\n", i);
- int j = copy_start_column;
- int l = start_column;
- while (j < copy_end_col) {
- // printf("j: %d\n", j);
- targetPosition[k][l] = canvas[i][j];
- j++;
- l++;
- }
- i++;
- k++;
- }
- // Copy to canvas variables
- int start_paste_row = targetRow;
- int start_paste_col = targetCol;
- int end_paste_row = targetRow + height + 1;
- int end_paste_col = targetCol + width + 1;
- // Copying from targetPosition to canvas variables
- int paste_row = 0;
- int paste_col = 0;
- while (start_paste_row < end_paste_row && flag == 0) {
- start_paste_col = targetCol;
- paste_col = 0;
- while (start_paste_col < end_paste_col) {
- canvas[start_paste_row][start_paste_col] = targetPosition[paste_row][paste_col];
- start_paste_col++;
- paste_col++;
- }
- start_paste_row++;
- paste_row++;
- }
- // printf("Wait for input\n");
- result = scanf("%d", &instruction);
- }
- }
- printCanvas(canvas);
- return 0;
- }
- void drawLine(int startRow, int startCol, int endRow, int endCol, int shade, int canvas[N_ROWS][N_COLS]) {
- int row = 0;
- //invalid lines
- //checking if the starting point or ending point cannot be negative
- if (startCol < 0 || endCol < 0 || startRow < 0 || endRow < 0 ){
- return;
- }
- //checking if the starting point or ending point cannot be greater than the canvas
- else if( startCol >= N_COLS || endCol >= N_COLS ||startRow >= N_ROWS || endRow >= N_ROWS ){
- return;
- }
- //valid lines
- int i=0;
- while (row < N_ROWS) {
- //draw a line
- int col = 0;
- while (col < N_COLS) {
- int gradient = 0;
- if (endRow - startRow != 0) {
- gradient = (endCol - startCol) / (endRow - startRow);
- }
- //diagonal
- if (gradient == 1 || gradient == -1) {
- if (gradient == -1) {
- if (startRow < endRow && startCol > endCol) {
- if (row >= startRow && row <= endRow) {
- if (col >= endCol && col <= startCol) {
- if (row == startRow + i && col == startCol - i) {
- canvas[row][col]= shade;
- i++;
- }
- }
- }
- }
- } else {
- if (startRow == startCol && endRow == endCol) {
- if (startCol > endCol) {
- if (row >= endRow && row <= startRow) {
- if (col >= endCol && col <= startCol) {
- if (row == col) {
- canvas[row][col]= shade;
- }
- }
- }
- }
- if (row >= startRow && row <= endRow) {
- if (col >= startCol && col <= endCol) {
- if (row == col) {
- canvas[row][col]= shade;
- }
- }
- }
- } else if (startRow < endRow && startCol < endCol) {
- if (row >= startRow && row <= endRow) {
- if (col >= startCol && col <= endCol) {
- if (row == startRow + i && col == startCol + i) {
- canvas[row][col]= shade;
- i++;
- }
- }
- }
- }
- }
- }
- //horizontal
- if (startRow == endRow && row == startRow ) {
- if (col >= startCol && col <= endCol){
- canvas[row][col]= shade;
- } else if (col <= startCol && col >= endCol){
- canvas[row][col]= shade;
- }
- } //vertical
- else if (startCol == endCol && col == startCol) {
- if (row >= startRow && row <= endRow){
- canvas[row][col]= shade;
- } else if (row <= startRow && row >= endRow) {
- canvas[row][col]= shade;
- }
- }
- col++;
- }
- row++;
- }
- }
- void fillRectangle(int startRow, int startCol, int endRow, int endCol, int shade, int canvas[N_ROWS][N_COLS]) {
- int row = 0;
- //checking if the starting point or ending point cannot be negative
- if (startCol < 0 || endCol < 0 || startRow < 0 || endRow < 0 ){
- return;
- }
- //checking if the starting point or ending point cannot be greater than the canvas
- else if( startCol >= N_COLS || endCol >= N_COLS ||startRow >= N_ROWS || endRow >= N_ROWS ){
- return;
- }
- while (row < N_ROWS) {
- int col = 0;
- while (col < N_COLS) {
- if (row >= startRow && row <= endRow) {
- if (col >= startCol && col <= endCol){
- canvas[row][col]= shade;
- } else if (col <= startCol && col >= endCol){
- canvas[row][col]= shade;
- }
- } else if (col >= startCol && col <= endCol) {
- if (row >= startRow && row <= endRow) {
- canvas[row][col]= shade;
- } else if (row <= startRow && row >= endRow) {
- canvas[row][col]= shade;
- }
- }
- col++;
- }
- row++;
- }
- }
- // Prints the canvas, by printing the integer value stored in
- // each element of the 2-dimensional canvas array.
- //
- // You should not need to change the printCanvas function.
- void printCanvas(int canvas[N_ROWS][N_COLS]) {
- int row = 0;
- while (row < N_ROWS) {
- int col = 0;
- while (col < N_COLS) {
- printf("%d ", canvas[row][col]);
- col++;
- }
- row++;
- printf("\n");
- }
- }
- // Sets the entire canvas to be blank, by setting each element in the
- // 2-dimensional canvas array to be WHITE (which is #defined at the top
- // of the file).
- //
- // You should not need to change the setBlankCanvas function.
- void setBlankCanvas(int canvas[N_ROWS][N_COLS]) {
- int row = 0;
- while (row < N_ROWS) {
- int col = 0;
- while (col < N_COLS) {
- canvas[row][col] = WHITE;
- col++;
- }
- row++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement