The Canvas Class
-
Upload
lucian-woodward -
Category
Documents
-
view
32 -
download
0
description
Transcript of The Canvas Class
![Page 1: The Canvas Class](https://reader036.fdocuments.in/reader036/viewer/2022082818/568131a8550346895d98163e/html5/thumbnails/1.jpg)
The Canvas Class
Lecture 8Fri, Sep 12, 2003
![Page 2: The Canvas Class](https://reader036.fdocuments.in/reader036/viewer/2022082818/568131a8550346895d98163e/html5/thumbnails/2.jpg)
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.
![Page 3: The Canvas Class](https://reader036.fdocuments.in/reader036/viewer/2022082818/568131a8550346895d98163e/html5/thumbnails/3.jpg)
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.
![Page 4: The Canvas Class](https://reader036.fdocuments.in/reader036/viewer/2022082818/568131a8550346895d98163e/html5/thumbnails/4.jpg)
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.
![Page 5: The Canvas Class](https://reader036.fdocuments.in/reader036/viewer/2022082818/568131a8550346895d98163e/html5/thumbnails/5.jpg)
Classes
point2.hrect.hcanvas.hcanvas.cpp
![Page 6: The Canvas Class](https://reader036.fdocuments.in/reader036/viewer/2022082818/568131a8550346895d98163e/html5/thumbnails/6.jpg)
The moveTo() Function
void Canvas::moveTo(float x, float y){ currPt.set(x, y);}
void Canvas::moveTo(Point2 p){ currPt = p;}
![Page 7: The Canvas Class](https://reader036.fdocuments.in/reader036/viewer/2022082818/568131a8550346895d98163e/html5/thumbnails/7.jpg)
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);}
![Page 8: The Canvas Class](https://reader036.fdocuments.in/reader036/viewer/2022082818/568131a8550346895d98163e/html5/thumbnails/8.jpg)
The Canvas Class
DrawHouse.cpp
![Page 9: The Canvas Class](https://reader036.fdocuments.in/reader036/viewer/2022082818/568131a8550346895d98163e/html5/thumbnails/9.jpg)
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();
![Page 10: The Canvas Class](https://reader036.fdocuments.in/reader036/viewer/2022082818/568131a8550346895d98163e/html5/thumbnails/10.jpg)
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;}
![Page 11: The Canvas Class](https://reader036.fdocuments.in/reader036/viewer/2022082818/568131a8550346895d98163e/html5/thumbnails/11.jpg)
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.
![Page 12: The Canvas Class](https://reader036.fdocuments.in/reader036/viewer/2022082818/568131a8550346895d98163e/html5/thumbnails/12.jpg)
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();}
![Page 13: The Canvas Class](https://reader036.fdocuments.in/reader036/viewer/2022082818/568131a8550346895d98163e/html5/thumbnails/13.jpg)
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.
![Page 14: The Canvas Class](https://reader036.fdocuments.in/reader036/viewer/2022082818/568131a8550346895d98163e/html5/thumbnails/14.jpg)
Example: Drawing Arcs
SmileyFace.cppPieChart.cpp