1 / 14

The Canvas Class

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.

Download Presentation

The Canvas Class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Canvas Class Lecture 8 Fri, Sep 12, 2003

  2. 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.

  3. 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.

  4. 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.

  5. Classes • point2.h • rect.h • canvas.h • canvas.cpp

  6. The moveTo() Function void Canvas::moveTo(float x, float y) { currPt.set(x, y); } void Canvas::moveTo(Point2 p) { currPt = p; }

  7. 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); }

  8. The Canvas Class • DrawHouse.cpp

  9. 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();

  10. 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; }

  11. 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.

  12. 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(); }

  13. 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.

  14. Example: Drawing Arcs • SmileyFace.cpp • PieChart.cpp

More Related