14
The Canvas Class Lecture 8 Fri, Sep 12, 2003

The Canvas Class

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

Page 1: The Canvas Class

The Canvas Class

Lecture 8Fri, Sep 12, 2003

Page 2: The Canvas Class

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

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

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

Classes

point2.hrect.hcanvas.hcanvas.cpp

Page 6: The Canvas Class

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

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

The Canvas Class

DrawHouse.cpp

Page 9: The Canvas Class

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

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

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

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

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

Example: Drawing Arcs

SmileyFace.cppPieChart.cpp