Help With Rectangle/Ellipse Image Drawing

Hello,

I currently need to write a program for my class that continuously requires me to give the user 3 choices of either drawing a rectangle, an ellipse, or to save the previous images and exit the loop and program.

if rectangle or ellipse is selected, the user is then prompted to input 4 dimensions that aid in drawing the selected image in a maximum box of 256, i suppose pixels?.

I am having trouble writing the four loops for my function calls regarding either shape.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  #include <iostream>
//contains constant SIZE and other necessary functions
//SIZE is in a "bmplib.h", a file the professor provides to us to call
#include "bmplib.h"
using namespace std;

// global variable. bad style but ok for this lab
unsigned char image[SIZE][SIZE];

//forward calling of function
void draw_rectangle(int, int, int, int);
void draw_ellipse(int, int, int, int);


int main()
{
	
	//populates 2D array
   for (int i=0; i < SIZE; i++)
   	{
      for (int j=0; j < SIZE; j++)
	   	{
         image[i][j] = 255;
         
    	}
 	}
    
    //Variable Decalartions
	int a, top, left, height, width, cy, cx;
	
	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 >> a;
	

		//Does loop continuously until exit condition is inputted
		while(a!=2)
		{
			//enters rectangle 
			if(a == 0)
			{
				cin >> top >> left >> height >> width;
				draw_rectangle(top,left,height,width);
			}
			else
			//enters ellipse
			if( a == 1)
			{
				cin >> cy >> cx >> height >> width;
				draw_ellipse(int cy, int cx, int height, int width);
			}
			//asks user for new selection
			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 >> a;
		
		}
			//after exit condition is entered, saves image and prints 
			writeGSBMP("cross.bmp", image);


	
//end program
return 0;
}


// Fill in this function:
void draw_rectangle(int top, int left, int height, int width)
{
	
}

// Fill in this function:
void draw_ellipse(int cy, int cx, int height, int width) {

}


Apparently the program is to also print what it can in the allocated draw space even if someone enters a dimension outside of it and it does not wrap around..whatever that means :(

Any help or suggestions would be greatly appreciated!

Louie
Assuming your 2D array image is what you're supposed to draw, it should be passed as an argument to your functions draw_rectangle and draw_ellipse.

Why are you initialising all of your array's values to 255, they are chars, why not set them to whitespaces ' '?

What is it that you're having trouble with, is it the logic behind drawing to your 2D array?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

const bool FILL   = true ;    // fill figures or not?
const int  SIZE   = 60   ;    // change to 256 when you want
const char MARKER = '*'  ;

unsigned char image[SIZE][SIZE];

// Forward declarations
int iround( double x );
void column( int j, int i1, int i2, bool solid );
void drawEllipse( int cy, int cx, int height, int width );
void drawRectangle( int top, int left, int height, int width );
void plot();
void clear();


int main()
{
   int answer, top, left, height, width, cy, cx;
        
   while( true )
   {
      clear();

      cout << "To draw a rectangle, enter: 1 top left height width\n";
      cout << "To draw an ellipse, enter: 2 cy cx height width\n";
      cout << "Anything else will quit.\n";
      cin >> answer;
           
      switch( answer )
      {
         case 1:
         {
            cin >> top >> left >> height >> width;
            drawRectangle( top, left, height, width );
            plot();
            break;
         }
         case 2:
         {
            cin >> cy >> cx >> height >> width;
            drawEllipse( cy, cx, height, width );
            plot();
            break;
         }                  
         default:
         {
            return 0;
         }
      }                                                                                                                                 
   }                                                                                                                                 
}

//==================

int iround( double x )
{
   int i = ( x >= 0 ? x + 0.5 : x - 0.5 );
   return i;
}

//==================

void column( int j, int i1, int i2, bool solid )
{
   if ( j < 0 || j >= SIZE ) return;                       // clip left and right
   if ( i1 >= 0 && i1 < SIZE ) image[i1][j] = MARKER;      // clip top
   if ( i2 >= 0 && i2 < SIZE ) image[i2][j] = MARKER;      // clip bottom
   if ( solid )
   {
      for ( int i = max( i1 + 1, 0 ); i <= min( i2 - 1, SIZE - 1 ); i++ ) image[i][j] = MARKER;
   }
}

//==================

void drawEllipse( int cy, int cx, int height, int width )
{
   double a = width / 2.0, b = height / 2.0;    // ellipse semi-axes
   for ( int x = iround( cx - a ); x <= iround( cx + a ); x++ )
   {
      double xrel = ( x - cx ) / a;
      double yrel = sqrt( 1.0 - xrel * xrel );
      int y1 = iround( cy - b * yrel );
      int y2 = iround( cy + b * yrel );
      column( x, y1, y2, FILL );
    }
}

//==================

void drawRectangle( int top, int left, int height, int width )
{
   int y1 = top, y2 = top + height;
   column( left        , y1, y2, true );
   column( left + width, y1, y2, true );
   for ( int x = left + 1; x <= left + width - 1; x++ ) column( x, y1, y2, FILL );
}

//==================

void plot()
{
   for ( int i = 0; i < SIZE; i++ )
   {
      for (int j = 0; j < SIZE; j++ ) cout << image[i][j];
      cout << '\n';
   }
}

//==================

void clear()
{
   for ( int i = 0; i < SIZE; i++ )
   {
      for (int j = 0; j < SIZE; j++ ) image[i][j] = ' ';
   }
}

//================== 


To draw a rectangle, enter: 1 top left height width
To draw an ellipse, enter: 2 cy cx height width
Anything else will quit.
1 5 5 10 30
                                                            
     *******************************                        
     *******************************                        
     *******************************                        
     *******************************                        
     *******************************                        
     *******************************                        
     *******************************                        
     *******************************                        
     *******************************                        
     *******************************                        
     *******************************                        
                                                            
                                                            


                                                            
To draw a rectangle, enter: 1 top left height width
To draw an ellipse, enter: 2 cy cx height width
Anything else will quit.
2 30 30 10 40



                      *****************                     
                *****************************               
             ***********************************            
           ***************************************          
           ***************************************          
          *****************************************         
           ***************************************          
           ***************************************          
             ***********************************            
                *****************************               
                      *****************                     



To draw a rectangle, enter: 1 top left height width
To draw an ellipse, enter: 2 cy cx height width
Anything else will quit.
0
Topic archived. No new replies allowed.