Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS 103 Lab - 2D Arrays & Image Processing 1 Introduction In this lab you will co

ID: 3666865 • Letter: C

Question

CS 103 Lab - 2D Arrays & Image Processing 1 Introduction In this lab you will complete a program that allows you to draw rectangles and ellipses to a BMP image file 2 What you will learn After completing this lab you should be able * Utilize 2D arrays and understand their indexing · Accept multiple values from the keyboard using cin and make decisions . Utilize multiple source code files * Utilize a Makefile to compile your code 3 Background Information and Notes In this lab we have provided you code that can save a 256x256 2D array as a grayscale BMP file that you can view on your VM or other machine. You will be asked to write functions that let the user describe a rectangle and/or ellipse and have it "drawn" in black on a white background. We will use a 256-by-256 2D array that will represent an image. Each entry in the array represents 1 pixel. The value of the pixel should be a value of 0 255 where 0 is black and 255 is white. Recall that indexing of a 2D array starts with the [0][0] location in the upper left-hand corner (this is different from the Cartesian plane). See the diagram below The 2D array for an image uses [row][column] indexing. The first index is the row (top 0, bottom 255), and the second is the column (left 0, right 255) Col. 0 Col. 1 Col. 2 Col. 255 Row 00[01[2] Row 1 10[11[2 Row 22021][2112] [01(255] [11255] I21255] .[2551[255] Row [2551[0] [255][1] |[2551[2] 255:

Explanation / Answer

#define SIZE 256
unsigned char image[SIZE][SIZE];

void draw_rectangle(int top, int left, int height, int width)
{

// black background
for (int i=0; i<SIZE; i++) for (int j=0; j<SIZE; j++) image[i][j] = 0;

// plot white boundary lines
for (int t=left; t<width; t++) { image[t][t] = 255; image[t+height][t+height] = 255; }
for (int t=top; t<height; t++) { image[t][t] = 255; image[t+width][t+width] = 255; }
showGSBMP(image);
}

void draw_ellipse(int cy, int cx, int height, int width)
{
// black background
for (int i=0; i<SIZE; i++) for (int j=0; j<SIZE; j++) image[i][j] = 0;

// plot white boundary lines
for(int y=-height/2; y<=height/2; y++)
{
for(int x=-width/2; x<=width/2; x++)
{
double dx = (double)x / (double)width;
double dy = (double)y / (double)height;
if(dx*dx+dy*dy == 1) image[x][y] = 255;
}
}

}

void printToBmpAndQuit()
{
writeGSBMP("output.bmp", image);
exit(0);
}

int main()
{
int option, arg1, arg2, arg3, arg4;
cout << "To draw a rectangle, enter: 0 top left height width" << endl;
cout << "To draw an ellipse, enter: 1 cy cx height width" << endl;
cout << "To save your drawing as "output.bmp" and quit, enter: 2" << endl;
cin >> option;

if(option==0 || option==1) cin >> arg1 >> arg2 >> arg3 >> arg4;
else printToBmpAndQuit();

if(option==0) draw_rectangle(arg1, arg2, arg3, arg4);
else if(option==1) draw_ellipse(arg1, arg2, arg3, arg4);

return 0;
}