Upload
lucian-woodward
View
32
Download
0
Embed Size (px)
DESCRIPTION
The Canvas Class. Lecture 8 Fri, Sep 12, 2003. The Canvas Class. We will create a simple Canvas class that will encapsulate the commands that create and manage the drawing environment. The Canvas class user interface will make no references to OpenGL. Disclaimer. - PowerPoint PPT Presentation
Citation preview
The Canvas Class
Lecture 8Fri, Sep 12, 2003
The Canvas Class
We will create a simple Canvas class that will encapsulate the commands that create and manage the drawing environment.The Canvas class user interface will make no references to OpenGL.
Disclaimer
This class is for demonstration purposes only.It would take more effort than it is worth to develop this class properly.It ought to include an interactive (mouse, keyboard) interface.You are welcome to develop it on your own.
Some Supporting Classes
Some classes supporting the Canvas class are Point2 – 2-dimensional points. Rect<T> – Rectangles with vertex
coordinates of type T (e.g., int or float).
Some future supporting classes Point3, Point4, Vector.
Classes
point2.hrect.hcanvas.hcanvas.cpp
The moveTo() Function
void Canvas::moveTo(float x, float y){ currPt.set(x, y);}
void Canvas::moveTo(Point2 p){ currPt = p;}
The lineTo() Function
void Canvas::lineTo(float x, float y){ glBegin(GL_LINES); glVertex2f(currPt.x, currPt.y); glVertex2f(x, y); glEnd(); currPt.set(x, y);}
The Canvas Class
DrawHouse.cpp
Drawing Circles
We have seen that to draw a circle, we should draw a polygon with many sides.float dx, dy, angle = 0.0;
glBegin(GL_POLYGON); for (int i = 0; i < 40; i++) { dx = cos(angle); dy = sin(angle); glVertex2f(cen.x + rad*dx, cen.y + rad*dy); angle += dAngle; }glEnd();
Drawing Circles
The following program segment uses the Canvas class to draw a circle.canvas.moveTo(cen.x + rad, cen.y);float angle = 0.0;float dAngle = PI/20.0;for (int i = 0; i < 40; i++){ float dx = cos(angle); float dy = sin(angle); canvas.lineTo(cen.x + rad*dx, cen.y + rad*dy); angle += dAngle;}
Drawing Circles
The previous example is inefficient.Why? It draws every point twice.
It would be better to include a drawCircle() member function in the Canvas class that uses GL_POLYGON for efficiency.
The drawCircle() Function
void Canvas::drawCircle(Point2 cen, float r, int n){ float angle = 0.0; float dAngle = 2*PI/n; glBegin(GL_POLYGON); for (int i = 0; i < n; i++) { float dx = cos(i*angle); float dy = sin(i*angle); glVertex2f(cen.x + rad*dx, cen.y + r*dy); angle += dAngle; } glEnd();}
Drawing Arcs
An arc is a portion of a circle.It can be described by a center a radius a start angle an end angle
Use the same algorithm as drawCircle(), but limit the range of the angle.
Example: Drawing Arcs
SmileyFace.cppPieChart.cpp