CS 200 - Lab 21

Display Hack

In this lab, you will implement a function called draw_rectangle, which draws a rectangle on the screen using text characters.

The draw_rectangle function has three parameters:

struct rect r - describes the rectangle to be drawn

int fg_color - the foreground character to use when drawing the rectangle

int bg_color - the background character to use when drawing the rectangle

struct rect contains the following fields:

int top_row - row number of top row of rectangle (row 0 is the top of the window)

int bottom_row - row number of the bottom row of the rectangle

int left_col - column number of the left column of the rectangle (column 0 is the left of the window)

int right_col - column number of the right column of the rectangle

You can draw the rectangle either as a solid block of characters, or you can just draw the outline.  You can use any character to print the rectangle.

The file Console.h, under "Header Files" in the Solution Explorer, describes the functions available for you to call.

In order to choose the color of the characters you draw, call the change_color function.  This function takes two parameters, representing the foreground and background colors to use.

In order to draw the rectangle at the specified position on the screen, you can call the move_cursor function, which moves the cursor to the specified row and column.  The general idea is, for each row of the rectangle, move the cursor to the beginning of the row, and then use printf to draw each character in the row.

Once you have implemented the draw_rectangle function, you can run the program using the already-implemented main function.  The program draws a number of random rectangles of different sizes and positions on the screen.  When the program finishes, the screen should look something like this:

Getting Started

If you have a solution open in Visual Studio, close it (File->Close Solution).

Download lab21.zip.  Import it into Visual Studio (File->Import...).

Add your code to the source file called Draw.c.