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

I have a program that draws ellipses and rectangles with user inputted dimension

ID: 640397 • Letter: I

Question

I have a program that draws ellipses and rectangles with user inputted dimensions.

How do I make it so the the center of the ellipse is at cx and cy?

Also how do make it so the drawings do not wrap around?

#include <iostream>
#include <cmath>
#include "bmplib.h"

using namespace std;

unsigned char image[SIZE][SIZE];

void draw_rectangle(int top, int left, int height, int width)
{
//i pertains to height
//j pertains to width

for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if ((i == height - 1) || (j == width - 1) || i == 0 || j == 0)
{
image[i+top][j+left] = 0;
} //1st If
} //2nd For
} //1st For

}

void draw_ellipse(int cy, int cx, int height, int width)
{
for (double theta = 0.0; theta < (2*M_PI); theta += 0.01)
{
double y = (height/2)*sin(theta); //Height
double x = (width/2)*cos(theta); //Width
  
x += (SIZE/2);
y += (SIZE/2);
  
image[(int)y][(int)x] = 0;
}
}


int main() {

//For choosing type of drawing
int number;

//Rectangle
int top;
int left;
int height;
int width;

//Ellipse
int cy;
int cx;

// initialize the image to all white pixels
for (int i=0; i < SIZE; i++) {
for (int j=0; j < SIZE; j++) {
image[i][j] = 255;
}
}

// Main program loop here


do
{
cout << "To draw a rectangle, enter: 0" << endl;
cout << "To draw an ellipse, enter: 1" << endl;
cout << "To save your drawing as 'output.bmp' and quit, enter: 2" << endl;
  
cin >> number;

switch (number)
{
  
case 0:
cout << "Enter starting point for:" << endl;
  
cout << "Top:";
cin >> top;
  
cout << "Left:";
cin >> left;
  
cout << "Height:";
cin >> height;
  
cout << "Width:";
cin >> width;

draw_rectangle(top, left, height, width);
break;

case 1:
cout << "Enter starting point for:" << endl;
  
cout << "CY:";
cin >> cy;

cout << "CX:";
cin >> cx;

cout << "Height:";
cin >> height;

cout << "Width:";
cin >> width;

draw_ellipse(cy, cx, height, width);
break;
  
case 2:
cout << "Saving image to output.bmp" << endl;
break;
  
}
}while (number != 2);
cout << "Closing Program" << endl;

// Write the resulting image to the .bmp file
writeGSBMP("output.bmp", image);

return 0;
}

Explanation / Answer

#include

#include

#include

#include

void onEllipsePoint(int xCenter,int yCenter,float x,float y)

{

putpixel ((xCenter + x),( yCenter + y),10);

putpixel ((xCenter - x), (yCenter + y),10);

putpixel ((xCenter + x),( yCenter - y),10);

putpixel ((xCenter - x), (yCenter- y),10);

}

void drawEllipse(int xcenter,int ycenter,int a,int b)

{

float x,y,d;

x = 0;

y = b;

d = pow(b,2) - pow(a,2)*b + pow(a,2)/4;

onEllipsePoint(xcenter,ycenter,x,y);

while( (a*a*(y-.5)) > (b*b*(x+1)) )

{

if(d<0)

{

d = d + pow(b,2)*(2*x+3);

x=x+1;

}

else

{

d = d + pow(b,2)*(2*x+3) + pow(a,2)*(-2*y+2);

x=x+1;

y=y-1;

}

onEllipsePoint(xcenter,ycenter,x,y);

}

d = b*b*pow((x+.5),2) + a*a*pow((y-1),2) - a*a*b*b;

while(y>0)

{

if(d<0)

{

d = d + b*b*(2*x+2) + a*a*(-2*y+3);

x++;

y++;

}

else

{

d = d + a*a*(-2*y+3);

y--;

}

onEllipsePoint(xcenter,ycenter,x,y);

}

}

void main()

{int ch;
printf("What do you want to draw? 1.Ellipse 2.Rectangle");
scanf("%d",ch);
switch(ch)
{
Case 1:

int a,b,xc,yc;

int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\TC\BGI");

printf("Enter coordinates of center : ");

scanf("%d",&xc);

scanf("%d",&yc);

printf(" Enter Major axis length : ");

scanf("%d",&a);

printf(" Enter Minor axis length : ");

scanf("%d",&b);

drawEllipse(xc,yc,a,b);

getch();
break;

Case 2:
int strx,stry,endx,endy,col;
printf("Enter start x & y coordinates");
scanf("%d%d",&strx,&stry);

printf("Enter end x & y coordinates");
scanf("%d%d",&endx,&endy);

printf("enter the color"

scanf("%d",&col);

box(strx,stry,endx,endy,col);


}

void box(int startx, int starty, int endx, int endy, int color)

{

line(startx , starty , endx , starty , color) ;

line(startx , starty , startx , endy , color) ;

line(startx , endy , endx , endy , color) ;

line(endx , starty , endx , endy , color) ;

}

void line(int startx, int starty, int endx, int endy, int color)

{

register int t , distance ;

int xerr = 0 , yerr = 0 , deltax , deltay ;

int incx , incy ;

deltax = endx - startx ;

deltay = endy - starty ;

if(deltax > 0)

incx = 1 ;

else if(deltax == 0)

incx = 0 ;

else

incx =- 1 ;

if(deltay > 0)

incy = 1 ;

else if(deltay == 0)

incy = 0 ;

else

incy =- 1 ;

deltax = abs(deltax) ;

deltay = abs(deltay) ;

if(deltax > deltay)

distance = deltax ;

else

distance = deltay ;

for(t = 0 ; t < distance + 1 ; t++) {

mempoint(startx , starty , color) ;

xerr += deltax ;

yerr += deltay ;

if(xerr > distance) {

xerr -= distance ;

startx+=incx ;

}

if(yerr > distance) {

yerr -= distance ;

starty+=incy ;

}

}

}