In: Computer Science
C PROGRAM
In Stage 1, you will be implementing the Draw Line command to draw horizontal and vertical lines.
The Draw Line command is given four additional integers, which describe two pixels: the start and end pixels of the line.
Each pixel consists of two numbers: the index of the row, and the index of the column.
For example, the command 1 10 3 10 10 tells your program to draw a line (1), starting at the pixel at row 10 and column 3, and ending at the pixel at row 10 and column 10.
When given the Draw Line command, your program should set the colour of the relevant elements in the canvas array, starting at the provided start pixel location, and continuing along the horizontal or vertical line until it reaches the end pixel location (including both the start and end pixels themselves).
Hints
Handling Invalid Input
Input Commands
Starter Code.
#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 BLACK 0 #define WHITE 4 // IF YOU NEED MORE #defines ADD THEM HERE // Provided helper functions: // Display the canvas. void displayCanvas(int canvas[N_ROWS][N_COLS]); // Clear the canvas by setting every pixel to be white. void clearCanvas(int canvas[N_ROWS][N_COLS]); // ADD PROTOTYPES FOR YOUR FUNCTIONS HERE int main(void) { int canvas[N_ROWS][N_COLS]; clearCanvas(canvas); // TODO: Add your code here! // Hint: start by scanning in the command. // // If the command is the "Draw Line" command, scan in the rest of // the command (start row, start col, length, direction) and use // that information to draw a line on the canvas. // // Once your program can draw a line, add a loop to keep scanning // commands until you reach the end of input, and process each // command as you scan it. displayCanvas(canvas); return 0; } // ADD CODE FOR YOUR FUNCTIONS HERE // Displays the canvas, by printing the integer value stored in // each element of the 2-dimensional canvas array. // // You should not need to change the displayCanvas function. void displayCanvas(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 clearCanvas function. void clearCanvas(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++; } }
#include <stdio.h>
#include<stdlib.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 BLACK 0
#define WHITE 4
// IF YOU NEED MORE #defines ADD THEM HERE
// Provided helper functions:
// Display the canvas.
void displayCanvas(int canvas[N_ROWS][N_COLS]);
// Clear the canvas by setting every pixel to be white.
void clearCanvas(int canvas[N_ROWS][N_COLS]);
void drawLine(int canvas[N_ROWS][N_COLS]);
// ADD PROTOTYPES FOR YOUR FUNCTIONS HERE
int main(void) {
int canvas[N_ROWS][N_COLS];
int command;
while(1){
printf("\n1. drawLine\n2. display \n3.clearCanvas\n4.exit\n");
scanf("%d",&command);
switch(command){
case 1:
drawLine(canvas);
break;
case 2:
displayCanvas(canvas);
break;
case 3:
clearCanvas(canvas);
break;
case 4:
exit(0);
}
}
// TODO: Add your code here!
// Hint: start by scanning in the command.
//
// If the command is the "Draw Line" command, scan in the rest of
// the command (start row, start col, length, direction) and use
// that information to draw a line on the canvas.
//
// Once your program can draw a line, add a loop to keep scanning
// commands until you reach the end of input, and process each
// command as you scan it.
return 0;
}
int outside(int x,int y){
return x < 0 || x >= N_ROWS || y < 0 || y >= N_COLS;
}
// ADD CODE FOR YOUR FUNCTIONS HERE
int max(int a ,int b){
return a<b?b:a;
}
int min(int a ,int b){
return a<b?a:b;
}
void drawLine(int canvas[N_ROWS][N_COLS]){
int ax,ay,bx,by;
printf("enter ax ay bx by ");
scanf("%d %d %d %d",&ax,&ay,&bx,&by);
if(!(ax == bx || ay == by)){
return;
}
if(outside(ax,ay) && outside(bx,by))
return ;
if(ax == bx){
int mn= min(ay ,by);
int mx= max(ay ,by);
mn = max(0,mn);
mx = min(N_COLS,mx);
for(int i=mn;i<=mx;i++){
canvas[ax][i] = BLACK;
}
}
else{
int mn= min(ax ,bx);
int mx= max(ax ,bx);
mn = max(0,mn);
mx = min(N_COLS,mx);
for(int i=mn;i<=mx;i++){
canvas[i][ay] = BLACK;
}
}
}
// Displays the canvas, by printing the integer value stored in
// each element of the 2-dimensional canvas array.
//
// You should not need to change the displayCanvas function.
void displayCanvas(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 clearCanvas function.
void clearCanvas(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++;
}
}