Title: Circles, Ellipses, Curves
1Circles, Ellipses, Curves
2Circle
- Circle equation (x-xc)2 (y-yc)2 r2
- Can use mid-point algorithm to rasterize circle
- Useful to use polar coordinates
- x xc r cos q
- y yc r sin q
- Sweep through q to draw circle
- glBegin(GL_LINE_STRIP)
- for (theta0 thetaltTWO_PI theta0.01)
- glVertex3f(xcrcos(theta),ycrsin(theta),0.0)
-
- glEnd()
- Exercise
- How do you draw a filled circle using
GL_TRIANGLES?
3Bresenhams Circle Algorithm
- Adapt Line Algorithm to Circle
- Start with first quadrant
- Check the value of fcirc(x,y) to determine next
pixel
end
start
fcirc(x,y) x2 y2 - r2
4Ellipse
- All points on an ellipse satisfies
- Whats the ellipse equation?
d1 d2 constant
d2
d1
5Ellipse
y
ry
2
2
y - yc
x - xc
yc
rx
1
rx
ry
x
xc
6Conic Section
- Circles and ellipses belong to a class called
conic sections
7Conic Section
- Conic section equation
- Discriminant B2 4AC
Ax2 By2 Cxy Dx Ey F 0
lt 0, generates ellipse 0, generates
parabola gt 0, generates hyperbola
8Parabola
- A parabola is the set of all points in the plane
equidistant from a given plane (called the
directrix) and a given point not on the line
(called the focus). - For a parabola opening to the right with vertex
at (0, 0), the equation in Cartesian coordinates
is
9Other curves
- Hyperbola
- Polynomials y f(x)
- x can go up to the nth degree
- Splines control points
10GLUI notes
include lttime.hgt clock_t old_time float delay
1.0 void playbutton_cb(int id) playing
id glutSetWindow(win) glutPostRedisplay()
void myGlutIdle( void ) clock_t
new_time new_time clock() if
(((double)(new_time-old_time)/(double)CLOCKS_PER_S
EC)gtdelay) old_time new_time if
(playing) // if playing flag is set, then
update graphics here ...
glutSetWindow(win) glutPostRedisplay()
int main() win
glutCreateWindow(Game) ... dynamics_glui
GLUI_Master.create_glui( "Animation", 0,
controlx2, controly2 ) GLUI_Panel
dynamics_panel1 dynamics_glui-gtadd_panel( "",
GLUI_PANEL_NONE ) GLUI_Panel
dynamics_animation_panel dynamics_glui-gtadd_pan
el_to_panel ( dynamics_panel2, "Animation" )
dynamics_glui-gtadd_button_to_panel(dynamics_animat
ion_panel, "Play", 1, playbutton_cb)
dynamics_glui-gtadd_button_to_panel(dynamics_animat
ion_panel, Stop", 0, playbutton_cb)
GLUI_Master.set_glutIdleFunc( myGlutIdle )
Set current window to the display window
Called every time button is pressed
Argument to the callback function
11GLUT Window Resizing
int main(int argc, char argv) //
GLUT stuff
glutInitDisplayMode( GLUT_RGB
GLUT_DOUBLE GLUT_DEPTH ) side_window
glutCreateWindow( "BGPViz" )
glutPositionWindow( 10 , 10 ) // starting x, y
of window glutReshapeWindow( 512, 512 ) // x,
y size of window glutDisplayFunc(
sideGlutDisplay ) glutReshapeFunc(
sideGlutReshape ) glutKeyboardFunc(
sideGlutKeyboard ) glutMotionFunc(
sideGlutMotion ) glutMouseFunc( sideGlutMouse
) // GLUI stuff
glui
GLUI_Master.create_glui( "Control Panel", 0,
520,20 ) /
name, flags, x, and y / / Add invisible
panel to hold rest of controls / GLUI_Panel
panel1 glui-gtadd_panel( "", GLUI_PANEL_NONE
) / Start a new column in this panel /
glui-gtadd_column_to_panel(panel1, false) /
'false' means don't draw bar /
glui-gtadd_button_to_panel(panel1, "Clear", 0,
clear_cb) mode_chkbx0 glui-gtadd_checkbox_to
_panel(panel1, "OpenGL Line", (DL.mode), 0,
mode_cb) mode_chkbx1 glui-gtadd_checkbox_to_
panel(panel1, "DDL", (DL.mode), 1, mode_cb)
mode_chkbx2 glui-gtadd_checkbox_to_panel(panel1
, "Bresenham", (DL.mode), 2, mode_cb)
mode_cb(0) GLUI_Master.set_glutIdleFunc(
myGlutIdle ) // GLUT stuff
glutMainLoop() return 0
This function called whenever the window size is
changed.
12GLUT Window Resizing
512
- Consider the following code
int winx 512 int winy 512 void
display_func() glMatrixMode(GL_PROJECTION)
glLoadIdentity() gluOrtho2D(0.0,1.0,0.0,1.0)
// left, right, bottom, top
glViewport(0,0,winx,winy) // startx, starty,
xsize, ysize glMatrixMode(GL_MODELVIEW)
glLoadIdentity() DrawSquare(0.3,0.3,0.5)
// startx, starty, size void
reshape_func(int x, int y) winx x winy
y int main() window glutCreateWindow(
Hex" ) glutPositionWindow( 10 , 10 ) //
starting x, y of window glutReshapeWindow(
winx, winy) // x, y size of window
glutDisplayFunc( display_func )
glutReshapeFunc( reshape_func )
512
User resizes window
496
270
13GLUT Window Resizing
512
- Now consider the following code
int winx 512 int winy 512 void
display_func() glMatrixMode(GL_PROJECTION)
glLoadIdentity() gluOrtho2D(0.0,1.0,0.0,1.0)
// left, right, bottom, top
glViewport(0,0,winx,winy) // startx, starty,
xsize, ysize glMatrixMode(GL_MODELVIEW)
glLoadIdentity() DrawSquare(0.3,0.3,0.5)
// startx, starty, size void
reshape_func(int x, int y) winx x winy
y if (winy lt winx) winx winy if (winx lt
winy) winy winx int main() window
glutCreateWindow( Hex" ) glutPositionWindow(
10 , 10 ) // starting x, y of window
glutReshapeWindow( winx, winy) // x, y size of
window glutDisplayFunc( display_func )
glutReshapeFunc( reshape_func )
512
User resizes window
496
270
14GLUT Window Resizing
512
512
Exercise How would you write code to do this?
User resizes window
496
270