OpenGLobs
The purpose of this paper is to describe how to use OpenGL to create a stand-along executable program for implementing drawing algorithm of yesterday…and to cut MATLAB out of the deal.
Here’s the algorithm
i.
Find the center (h,
k) of the ith arc’s
circle using ii. Partition the ith arc’s interval iii. Plot the arc as a sequence of line segments placed end-to-end using
i.
Use the arcs
table to compute the angular interval for the ith interior arc, ii. Compute the radius of the ith interior arc by : iii. Compute the common denominator , denom = sin(deltaTheta);
iv.
Compute the (h,
k) coordinates of the center of
the interior circle v. Partition the ith arc’s interval as either or , whichever works. vi. Plot the arc as a sequence of line segments placed end-to-end using
Here is the complete program:
The glob-defining inner arcs are defined by the table // This is the defining table of node index pairs, hardwired here GLfloat arcs[6][2] = {{0,1},{2,8},{3,7},{4,6},{4,5},{5,6}}; Thus
node 0 is connected to node 1, node 2 to 8 and so on. Armed with this table, it’s not too much
trouble to draw the arcs, but each may have, aside from the unique center,
also a unique dTheta and radius. So
the loop looks like this: for(int i = 0; i < 6; ++i) { dTheta = 2*Pi*(arcs[i][1]-arcs[i][0])/n; denom = sin(dTheta); center_x = (sin(2*Pi*arcs[i][1]/n)-sin(2*Pi*arcs[i][0]/n))/denom; center_y = (cos(2*Pi*arcs[i][0]/n)-cos(2*Pi*arcs[i][1]/n))/denom; radius = (1-cos(dTheta))/sin(dTheta); drawInArc(center_x, center_y, radius, arcs[i], n); } Then when it comes to drawing them, you need to figure out which part of the arc to draw. I use a condition as shown below: if(arcs[1]/n + 0.25 < arcs[0]/n + 0.75) { for(GLfloat t=2*Pi*(arcs[1]/n+0.25); t<=2*Pi*(arcs[0]/n+0.75); t+=.01) { glVertex2f(h+r*cos(t),k+r*sin(t)); glVertex2f(h+r*cos(t+.01),k+r*sin(t+.01)); } } else { for(GLfloat t=2*Pi*(arcs[0]/n+0.75); t<=2*Pi*(arcs[1]/n+0.25); t+=.01) { glVertex2f(h+r*cos(t),k+r*sin(t)); glVertex2f(h+r*cos(t+.01),k+r*sin(t+.01)); } }
|
Exercises: