CGAssignment
DrawUtil Class Reference
Collaboration diagram for DrawUtil:

Classes

struct  RGB
 

Public Member Functions

void initializeColors ()
 
void PlotPixel (int x, int y)
 
int findOctant (int dx, int dy)
 
tuple< int, int, int, int > switchOctantToZero (int x1, int y1, int x2, int y2, int n)
 
tuple< int, int > switchOctantFromZero (int x1, int y1, int n)
 
void drawLine (int x1, int y1, int x2, int y2)
 
void draw_circle_octants (int x, int y, int x0, int y0)
 
void drawCircle (int x0, int y0, int r)
 
void drawStars (int W, int H, int count)
 
void drawGrass (int W)
 
void drawSunset (int W, int H)
 
void drawNightSky (int W, int H)
 
void fillCircle (int x, int y, int r)
 
void glConfig (int W, int H)
 
void Stayawake (GLFWwindow *window)
 
void setColor (double r, double g, double b)
 
void changePointSize (int psize)
 
GLFWwindow * StartWindow (int _h, int _w)
 

Public Attributes

RGB Colors [4]
 

Member Function Documentation

void DrawUtil::changePointSize ( int  psize)
inline
322  {
323  glPointSize(psize);
324  }
void DrawUtil::draw_circle_octants ( int  x,
int  y,
int  x0,
int  y0 
)
inline
183  {
184 
185  PlotPixel(x+x0, y+y0); //draw in 1st octant
186  PlotPixel(x0-x, y0+y); // draw in the 2nd octant
187  PlotPixel(x+x0, y0-y); // draw in the 6th octant
188  PlotPixel(x0-x, y0-y);; // draw in the 5th octant
189  PlotPixel(y+x0, x+y0); // draw in the 0th octant
190  PlotPixel(x0-y, x+y0); // draw in the 7th octant
191  PlotPixel(y+x0, y0-x); // draw in the 3rd octant
192  PlotPixel(x0-y, y0-x); // draw in the 4th octant
193  }
void PlotPixel(int x, int y)
Definition: DrawUtil.cpp:31
void DrawUtil::drawCircle ( int  x0,
int  y0,
int  r 
)
inline
197  {
198  int x=0;
199  int y=r;
200  int d=1-r; // intial decision variable
201  int deltaE=3; // second difference value when the East pixel is chosen
202  int deltaSE=-2*r+5; // second difference value when the South East pixel is chosen
203 
204  // Draw the points that lie on the four principle axis
205  PlotPixel(x+x0,y+y0);
206  PlotPixel(y+x0, x+y0);
207  PlotPixel(x+x0,y0-y);
208  PlotPixel(x0-y,x+y0);
209 
210  while(x<=y) // draw in the 1st octant till x is less than the y coordinate
211  {
212  if(d<0) //Choose East pixel
213  {
214  d+=deltaE; // increment the decision variable by amount deltaE
215  deltaSE+=2; //update the second difference values
216  deltaE+=2;
217  }
218  else //Choose South East pixel
219  {
220  d+=deltaSE; // update the decision variable
221  deltaSE+=4; // update the second difference values
222  deltaE+=2;
223  y--; // move vertically downward by one pixel
224  }
225  x++; // move forward on the x axis by 1 pixel
226  draw_circle_octants(x,y, x0, y0); // draw all the symmetrical points
227  }
228  }
void draw_circle_octants(int x, int y, int x0, int y0)
Definition: DrawUtil.cpp:182
void PlotPixel(int x, int y)
Definition: DrawUtil.cpp:31
void DrawUtil::drawGrass ( int  W)
inline
249  {
250  glColor3f(0,0.4,0);
251  int i=0;
252  while(i<W)
253  {
254  int height = 5 + (rand() % static_cast<int>(25 - 5 + 1));
255  drawLine(i,0, i+1, height);
256  i++;
257  }
258  }
void drawLine(int x1, int y1, int x2, int y2)
Definition: DrawUtil.cpp:124
void DrawUtil::drawLine ( int  x1,
int  y1,
int  x2,
int  y2 
)
inline
125  {
126  // if the line is vertical, then simply plot the vertical pixels withoug applying the Bresenhamm's Algorithm
127  if(x1 == x2)
128  {
129  int ystart = y2 > y1 ? y1 : y2;
130  int yend = y2 > y1 ? y2 : y1;
131  while(ystart <= yend)
132  {
133  PlotPixel(x1, ystart);
134  ystart++; // plot the next vertical pixel
135  }
136  }
137  else // apply the Bresenhamm's Line Drawing algorithm
138  {
139  int n = findOctant(x2-x1, y2-y1); //find the octant the line lies in
140  tie(x1, y1, x2, y2) = switchOctantToZero(x1, y1, x2, y2, n); //switch the octant accordingly to the 0 octant
141  int dy = y2 - y1;
142  int dx = x2 - x1;
143  int d_param = 2*dy - dx; // decision variable
144  int x = x1;
145  int y = y1;
146  int incr_NE = dy - dx; //value to increment the decision variable if North East pixel is chosen
147  int incr_E = dy; //value to increment the decision variable if the East pixel is chosen
148  int x_out;
149  int y_out;
150  while(x != x2) //keep drawing till the end point of the line
151  {
152  if(d_param >= 0) // choose the north east pixel
153  {
154  d_param += incr_NE;
155  tie(x_out, y_out) = switchOctantFromZero(x,y,n); // switch back to initial octant system
156  PlotPixel(x_out, y_out);
157  y++; // increment the y coordinate
158  }
159  else // choose the east pixel
160  {
161  d_param += incr_E;
162  tie(x_out, y_out) = switchOctantFromZero(x,y,n); // switch back to the initial octant system
163  PlotPixel(x_out, y_out);
164  }
165  x++; //move to the next x coordinate
166  }
167  }
168  }
tuple< int, int > switchOctantFromZero(int x1, int y1, int n)
Definition: DrawUtil.cpp:107
int findOctant(int dx, int dy)
Definition: DrawUtil.cpp:48
void PlotPixel(int x, int y)
Definition: DrawUtil.cpp:31
tuple< int, int, int, int > switchOctantToZero(int x1, int y1, int x2, int y2, int n)
Definition: DrawUtil.cpp:81
void DrawUtil::drawNightSky ( int  W,
int  H 
)
inline
274  {
275  int j = 0;
276  while(j <= H)
277  {
278  glColor3f(0.02,0.02,.20*j/H);
279  drawLine(0,j,W,j+1);
280  j++;
281  }
282  }
void drawLine(int x1, int y1, int x2, int y2)
Definition: DrawUtil.cpp:124
void DrawUtil::drawStars ( int  W,
int  H,
int  count 
)
inline
234  {
235  int start = H; // start from the topmost horizontal line
236  while(count > 0)
237  {
238  int height = H - (rand() % static_cast<int>(H - 100 + 1)); //select a random height in the given range (H - (H-100))
239  int x = (rand() % static_cast<int>(W)); //select a random x coordinte
240  float intensity_factor = 1.0*float(height)/float(H); // pixel intesity factor calculated according to the height
241  glColor3f(intensity_factor, intensity_factor, intensity_factor); //set the color
242  PlotPixel(x, height);
243  count--;
244  }
245  }
void PlotPixel(int x, int y)
Definition: DrawUtil.cpp:31
void DrawUtil::drawSunset ( int  W,
int  H 
)
inline
262  {
263  int j = 0;
264  while(j <= H)
265  {
266  glColor3f(0.40,0.40*j/H,0);
267  drawLine(0,j,W,j+1);
268  j++;
269  }
270  }
void drawLine(int x1, int y1, int x2, int y2)
Definition: DrawUtil.cpp:124
void DrawUtil::fillCircle ( int  x,
int  y,
int  r 
)
inline
284  {
285  int i = r;
286  while(i>0)
287  {
288  drawCircle(x,y,i);
289  i--;
290  }
291  }
void drawCircle(int x0, int y0, int r)
Definition: DrawUtil.cpp:196
int DrawUtil::findOctant ( int  dx,
int  dy 
)
inline
49  {
50  int octant = 0;
51  if(dx>=0) // check for octants 0, 1, 6, 7 where the value of x coordinate increments
52  {
53  if(dy>=0 && dy<=dx){octant = 0;}
54  else if(dy>=0 && dy>dx){octant = 1;}
55  else if(dy<0 && abs(dy)<=abs(dx)){octant = 7;}
56  else{octant = 6;}
57  }
58  else //else choose from remaining octants i.e. 2, 3, 4, 5 where dx decrements
59  {
60  if(dy>=0 && abs(dy) > abs(dx)){octant = 2;}
61  else if(dy>=0 && abs(dy) <= abs(dx)){octant = 3;}
62  else if(dy<0 && abs(dy)<=abs(dx)){octant = 4;}
63  else{octant = 5;}
64  }
65  return octant; // return the final octant the line lies in
66  }
void DrawUtil::glConfig ( int  W,
int  H 
)
inline
295  {
296  const GLubyte* renderer = glGetString(GL_RENDERER); // get renderer string
297  const GLubyte* version = glGetString(GL_VERSION); // version as a string
298  printf("Renderer: %s\n", renderer);
299  printf("OpenGL version supported %s\n", version);
300 
301  glOrtho(0, W, 0, H, 0, 1); // specify the coordinate system and use the bottom-left point as (0,0) and (W,H) as the top right point of the window
302  glColor3f(0,0,1);
303  glfwSwapInterval(1); // Vertical Sync rate
304  }
void DrawUtil::initializeColors ( )
inline
18  {
19  RGB c1(0.0,0.5,0.0); //light green
20  RGB c2(0.0,0.25,0.0); //dark green
21  RGB c3(0.36,0.13,0.09); //brown
22  RGB c4(.96,0.14,0.35); //red
23  Colors[0] = c1;
24  Colors[1] = c2;
25  Colors[2] = c3;
26  Colors[3] = c4;
27  }
RGB Colors[4]
Definition: DrawUtil.cpp:15
void DrawUtil::PlotPixel ( int  x,
int  y 
)
inline
32  {
33  GLfloat pointerVertices[2]; // Glfloat type array to contain the x and y coordinates to plot
34  pointerVertices[0] = x;
35  pointerVertices[1] = y;
36  glEnableClientState(GL_VERTEX_ARRAY);
37  glVertexPointer(2, GL_FLOAT, 0, pointerVertices); // glVertexPointer(number_of_axes, type, 0, vertex)
38  glDrawArrays(GL_POINTS, 0, 1);
39  glDisableClientState(GL_VERTEX_ARRAY);
40  }
void DrawUtil::setColor ( double  r,
double  g,
double  b 
)
inline
318  {
319  glColor3f(r,g,b);
320  }
GLFWwindow* DrawUtil::StartWindow ( int  _h,
int  _w 
)
inline
327  {
328  if(glfwInit() == false) //check for glfw
329  {
330  fprintf(stderr, "Error initializing GLFW\n");
331  return NULL;
332  }
333 
334  int H = _h; //height of the window
335  int W = _w; //width of the window
336 
337  //create a glfw window instance
338  GLFWwindow* window = glfwCreateWindow(W, H, "OpenGL Example", NULL, NULL);
339  if(!window) //handle the error if the window instance could not be created and return
340  {
341  glfwTerminate();
342  fprintf(stderr, "Error while creating a window\n");
343  return NULL;
344  }
345 
346  //set context
347  glfwMakeContextCurrent(window);
348 
349  // configure gl window
350  glConfig(W,H);
351 
352  //return the final window object
353  return window;
354 
355  }
void glConfig(int W, int H)
Definition: DrawUtil.cpp:294
void DrawUtil::Stayawake ( GLFWwindow *  window)
inline
307  {
308  while(!glfwWindowShouldClose(window)) // main loop for the window
309  {
310  glfwSwapBuffers(window);
311  glfwWaitEvents();
312 
313  }
314  glfwDestroyWindow(window); //Destroy the window and terminate the glfwInstance if the program exits the main loop
315  glfwTerminate(); // The exit from the main loop is triggered when a user closes the window
316  }
tuple<int, int> DrawUtil::switchOctantFromZero ( int  x1,
int  y1,
int  n 
)
inline
108  {
109  switch(n) // transfer the line from octant 0 back to octant n and return the final tuple
110  {
111  case 0: return make_tuple(x1, y1);
112  case 1: return make_tuple(y1, x1);
113  case 2: return make_tuple(-y1, x1);
114  case 3: return make_tuple(-x1, y1);
115  case 4: return make_tuple(-x1, -y1);
116  case 5: return make_tuple(-y1, -x1);
117  case 6: return make_tuple(y1, -x1);
118  case 7: return make_tuple(x1, -y1);
119  }
120  }
tuple<int, int, int, int> DrawUtil::switchOctantToZero ( int  x1,
int  y1,
int  x2,
int  y2,
int  n 
)
inline
82  {
83  switch(n) // transform the line from octant n to octant 0 and return the final tuple
84  {
85  case 0: return make_tuple(x1, y1, x2, y2);
86  case 1: return make_tuple(y1, x1, y2, x2);
87  case 2: return make_tuple(y1, -x1, y2, -x2);
88  case 3: return make_tuple(-x1, y1, -x2, y2);
89  case 4: return make_tuple(-x1, -y1, -x2, -y2);
90  case 5: return make_tuple(-y1, -x1, -y2, -x2);
91  case 6: return make_tuple(-y1, x1, -y2, x2);
92  case 7: return make_tuple(x1, -y1, x2, -y2);
93  }
94  }

Member Data Documentation

RGB DrawUtil::Colors[4]

The documentation for this class was generated from the following file: