Question

In: Computer Science

C PROGRAM In Stage 1, you will be implementing the Draw Line command to draw horizontal...

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

  • Your program will only be drawing either horizontal or vertical lines in Stage 1, which means that either row1 and row2 will be the same, or col1 and col2 will be the same.
  • If row1 == row2 && col1 == col2, your program should draw a single pixel, at the location (row1, col1), (row2, col2).

Handling Invalid Input

  • If the given command both starts and ends outside the canvas, you should do nothing, and ignore that Draw Line command. Note that ignoring the command should still read in it's arguments. Otherwise, if one end pixel of the line is within the canvas you should draw the section of the line that is within the canvas.
  • If the given start and end pixels would not give an entirely horizontal or vertical line, your program should ignore that Draw Line command and do nothing. Note that ignoring the command should still read in it's arguments.

Input Commands

  • Input to your program will be via standard input (similar to typing into a terminal).
  • You can assume that the input will always be integers and that you will always receive the correct number of arguments for a command. You will never be given a integer that represents a command that doesn't exist.
  • You can assume that input will always finish with the "End of Input" signal (Ctrl-D in the terminal).

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++;
    }
}

Solutions

Expert Solution

#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++;
    }
}


Related Solutions

Introduction Write in C++ at the Linux command line a program that is the same as...
Introduction Write in C++ at the Linux command line a program that is the same as the previous collection app project but now uses a class to store the items and also can save the items to a file that can be read back into the array by the user when the program is re-started. You can use your project 1 submission as a starting point or you can do something new as long as it meets the listed requirements....
A C program that accepts a single command line argument and converts it in to binary...
A C program that accepts a single command line argument and converts it in to binary with array length of 16 bits. The array should contain the binary of the int argument. the program should also convert negative numbers. Side note the command line arg is a valid signed int.
A C program that going to accept a single command line arg and convert it to...
A C program that going to accept a single command line arg and convert it to hexadecimal as an array of char that has 16 bits. The array should contain the hex of the int argument. Side note the command line arg is a valid signed int and the program should be able to convert negative numbers.
Write a C program that accepts a port number as a command line argument, and starts...
Write a C program that accepts a port number as a command line argument, and starts an HTTP server. This server should constantly accept() connections, read requests of the form: GET /path HTTP/1.1\r\n\r\n read the file indicated by /path, and send it over the "connect" file descriptor returned by the call to accept().
Write a C++ program that prints out all of the command line arguments passed to the...
Write a C++ program that prints out all of the command line arguments passed to the program. Each command line argument should be separated from the others with a comma and a space. If a command line argument ends in a comma, then another comma should NOT be added
Use C++ to write a program that reads in a binary string from the command line...
Use C++ to write a program that reads in a binary string from the command line and applies the following (00, 1101) tag-system: if the first bit is 0, deletes the first three bits and append 00; if the first bit is 1, delete the first three bits and append 1101. Repeat as long as the string has at least 3 bits. Try to determine whether the following inputs will halt or go into an infinite loop: 10010, 100100100100100100. Use...
Write a C program that accepts a port number as a command line argument, and starts...
Write a C program that accepts a port number as a command line argument, and starts an HTTP server. This server should constantly accept() connections, read requests of the form GET /path HTTP/1.1\r\n\r\n read the file indicated by /path, and send it over the "connect" file descriptor returned by the call to accept().
IN PYTHON: Compose a recursive program to draw Sierpinski triangles. Use a command-line argument to control...
IN PYTHON: Compose a recursive program to draw Sierpinski triangles. Use a command-line argument to control the depth of the recursion.
program c Write a program called filesearch that accepts two command-line arguments: A string A filename...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename If the user did not supply both arguments, the program should display an error message and exit. The program opens the given filename. Each line that contains the given string is displayed. Use the strstr function to search each line for the string. You may assume no line is longer than 255 characters. The matching lines are displayed to standard output (normally the screen).
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that...
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that appear on the command line when the program is invoked. Use the file name cl.c for your c program. Test your program with cl hello goodbye and cl 1 2 3 4 5 6 7 8 and cl 1b) Write a C program that reads in a string from the keyboard. Use scanf with the conversion code %s. Recall that the 2nd arg in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT