CSC 706 Computer Graphics Turtle graphicsnatacha/TeachFall_2009/CSC706/... · drawArc(Point2...

9
1 CSC 706 Computer Graphics Turtle graphics Relative drawing 2 Class Point2 class Point2 { public: Point2() {x = y = 0.0f;} // constructor1 Point2(float xx, float yy) {x = xx; y = yy;} // constructor2 void set(float xx, float yy) {x = xx; y = yy;} float getX() {return x;} float getY() {return y;} void draw(void) { glBegin(GL_POINTS); // draw this point glVertex2f((Glfloat)x, (Glfloat)y); glEnd();} private: float x, y; }; 3 Class IntRect class IntRect { public: IntRect() {l = 0; r = 100; b = 0; t = 100;}// constructors IntRect(int left, int right, int bottom, int top) {l = left; r = right; b = bottom; t = top;} void set(int left, int right, int bottom, int top) {l = left; r = right; b = bottom; t = top;} void draw(void); // draw this rectangle using OpenGL private: int l, r, b, t; }; 4 Class RealRect class RealRect { same as intRect except use float instead of int }; 5 class Canvas { public: Canvas(int width, int height, char* windowTitle); // constructor void setWindow(float l, float r, float b, float t); void setViewport(int l, int r, int b, int t); IntRect getViewport(void); // divulge the viewport data RealRect getWindow(void); // divulge the window data float getWindowAspectRatio(void); void clearScreen(); void setBackgroundColor(float r, float g, float b); void setColor(float r, float g, float b); void lineTo(float x, float y); void lineTo(Point2 p); void moveTo(float x, float y); void moveTo(Point2 p); others later private: Point2 CP; // current position in the world IntRect viewport; // the current window RealRect window; // the current viewport others later }; 6

Transcript of CSC 706 Computer Graphics Turtle graphicsnatacha/TeachFall_2009/CSC706/... · drawArc(Point2...

Page 1: CSC 706 Computer Graphics Turtle graphicsnatacha/TeachFall_2009/CSC706/... · drawArc(Point2 center, float radius, float startAngle, float sweep) { // startAngle and sweep are in

1

1

CSC 706 Computer Graphics

Turtle graphicsRelative drawing

2

Class Point2

class Point2{public:

Point2() {x = y = 0.0f;} // constructor1Point2(float xx, float yy) {x = xx; y = yy;} // constructor2void set(float xx, float yy) {x = xx; y = yy;}float getX() {return x;}float getY() {return y;}void draw(void) { glBegin(GL_POINTS); // draw this pointglVertex2f((Glfloat)x, (Glfloat)y);glEnd();}

private:float x, y;

};

3

Class IntRect

class IntRect{public:

IntRect() {l = 0; r = 100; b = 0; t = 100;}// constructorsIntRect(int left, int right, int bottom, int top){l = left; r = right; b = bottom; t = top;}void set(int left, int right, int bottom, int top){l = left; r = right; b = bottom; t = top;}void draw(void); // draw this rectangle using OpenGL

private:int l, r, b, t;

};

4

Class RealRect

class RealRect{same as intRect except use float instead

of int};

5

Declaration of Class Canvasclass Canvas {public:

Canvas(int width, int height, char* windowTitle); // constructorvoid setWindow(float l, float r, float b, float t);void setViewport(int l, int r, int b, int t);IntRect getViewport(void); // divulge the viewport dataRealRect getWindow(void); // divulge the window datafloat getWindowAspectRatio(void);void clearScreen();void setBackgroundColor(float r, float g, float b);void setColor(float r, float g, float b);void lineTo(float x, float y);void lineTo(Point2 p);void moveTo(float x, float y);void moveTo(Point2 p);

others laterprivate:

Point2 CP; // current position in the worldIntRect viewport; // the current windowRealRect window; // the current viewport

others later};

6

Page 2: CSC 706 Computer Graphics Turtle graphicsnatacha/TeachFall_2009/CSC706/... · drawArc(Point2 center, float radius, float startAngle, float sweep) { // startAngle and sweep are in

2

7 8

9

Turtle Graphics

• Drawing lines and points on the screen.

• Commanding a drawing turtle (pen in a plotter).

• moveto(), forward(), turn()

10

Turtle graphicsKeeps track not only of “where we are” with the CP, but also “the direction in which we are headed”. This is a form of turtlegraphics, which has been found to be a natural way to program in graphics. The notion is that a “turtle”, which is conceptually similar to the pen in a pen plotter, migrates over the page, leaving a trail behind itself which appears as a line segment. The turtle is positioned at the CP, headed in a certain direction called the current direction. CD is the number of degrees measured counterclockwise (CCW) from the positive x-axis.

void moveto(GLint x, GLint y){CP.x = x; CP.y = y; // update the CP}

void lineto(GLint x, GLint y){glBegin(GL_LINES); // draw the lineglVertex2i(CP.x, CP.y);glVertex2i(x, y);glEnd();glFlush();CP.x = x+10; CP.y = y+10; // update the CP}

11

void Canvas :: forward(float dist, int vis) {

#define RadPerDeg 0.017453393 //radians per degree float x = CP.x + dist * cos(RadPerDeg * CD);float y = CP.y + dist * sin(RadPerDeg * CD); if(vis) lineTo(x, y);else moveTo(x, y);CP.x = x; CP.y = y;

}void turn(float ang) {

CD += ang;}void turnTo(float ang) {

CD = ang;}

Building a figure upon a hook motif. The 3-segment “hook” motif shown in fig.a can be drawn using the commands:forward(3 * L, 1); // L is the length of the short sidesturn(90);forward(L, 1);turn(90);forward(L, 1);turn(90);for some choice of L. Suppose that procedure hook() encapsulates these instructions.

Turtle graphics

12

Turtle Graphics

Example: Polyspirals

for( some iterations ){

forward(length, 1);turn(angle);length += increment;

}

- polyspi.cpp

Page 3: CSC 706 Computer Graphics Turtle graphicsnatacha/TeachFall_2009/CSC706/... · drawArc(Point2 center, float radius, float startAngle, float sweep) { // startAngle and sweep are in

3

13

Polyspiral

polyspiral(float length, float angle, float incr, int num){for(int i=1; i<=num; i++){forward(length,1); // draw a line in the current direction

turn(angle); // turn through angle degreeslength += incrt; // increment the line length}return;}

14

Turtle graphics - examples

cg1

15

Turtle Graphics

Example: Polyspiralsfloat angle = 87, inc = 0.5;

float angle = 170, inc = 1;

float angle = 89.5, inc = 1;

16

Definition:

A polygon is regular if it is simple, if all its sides have equal lengths, and if adjacent sides meet at equal interior angles.

A polygon is simple if no two of its edges cross each other (more precisely: only adjacent edges can touch, and only at their shared endpoint).

The vertices of an n-gon lie on a circle, the so-called “parent circle“ of the n-gon, and their locations are easily calculated. The case of the hexagon is shown in Figure where the vertices lie equispaced every 60 o around the circle. The parent circle of radius R (not shown) is centered at the origin, and the first vertex P0 has been placed on the positive x-axis. The other vertices follow accordingly, as P i = ( R cos(i ×a), R sin(i × a)), for i =1,...,5, where a is 2p/6 radians. Similarly, the vertices of the general n-gonlie at:P i = ( R cos( 2pi / n ), R sin(2pi / n )), for i = 0,..., n-1 (3.6)It’s easy to modify this n-gon. To center it at position (cx, cy) we need only add cx and cy to the x- and y-coordinates, respectively. To scale it by factor S we need only multiply R by S. To rotate through angle A we need only add A to the arguments of cos() and sin().

N-gons

17

Drawing n-gonsTurtle-driven n-gon. It is also simple to draw an n-gon using turtlegraphics. The initial position and direction of the turtle is indicated by the small triangle. The turtle simply goes forward six times, making a CCW turn of 60 degrees between each move:for (i = 0; i < 6; i++){cvs.forward(L, 1);cvs.turn(60);}One vertex is situated at the initial CP, and both CP and CD are left unchanged by the process.

18

Regular Polygons

ngon(6,0.0,0.0,50.0, 20.0);

ngon(10,0.0,0.0,40.0, 30.0);

ngon(4,0.0,0.0,30.0, 10.0);

ngon(20,0.0,0.0,20.0, 5.0);

Page 4: CSC 706 Computer Graphics Turtle graphicsnatacha/TeachFall_2009/CSC706/... · drawArc(Point2 center, float radius, float startAngle, float sweep) { // startAngle and sweep are in

4

19

Interesting variations based on the vertices of an n-gon can also be drawn. The n-gon vertices may be connected in various ways to produce a variety of figures.

Variations of n-gons

The rosette is an n-gon with each vertex joined to every other vertex. A rosette is sometimes used as a test pattern for computer graphics devices. Its orderly shape reveals any distortions, and the resolution of the device can be determined by noting the amount of “crowding” and blurring exhibited by the bundle of lines that meet at each vertex.

cg2

20

Drawing circles and arcs

Drawing a circle is equivalent to drawing an n-gon that has a large number of vertices.

cg4

cg3

21

Circles and Arcs

• A regular polygon begins to represent a circle as the number of vertices becomes larger.

void drawCircle(Point2 center, float radius){

const int numVerts = 50;ngon(numVerts, center.getX(), center.getY(), radius, 0);

}

22

Circles and Arcs• However this code

won’t allow you to draw an arc.

• To draw an arc or circle you need to know, the centre, the radius, the starting angle (a) and the sweep angle (b).

23

Circles and ArcsThe code is similar to ngon() however it allows an arc to be drawn, which means that the shape isn’t drawn around 360 degrees.

drawArc(Point2 center, float radius, float startAngle, float sweep){ // startAngle and sweep are in degrees

const int n = 30; // number of intermediate segments in arc float angle = startAngle * 3.14159265 / 180; // initial angle in radiansfloat angleInc = sweep * 3.14159265 /(180 * n); // angle incrementfloat cx = center.getX(), cy = center.getY();cvs.moveTo(cx + radius * cos(angle), cy + radius * sin(angle));for(int k = 1; k < n; k++, angle += angleInc)

cvs.lineTo(cx + radius * cos(angle), cy + radius * sin(angle));}

24

Circles and Arcsp.set(0.0,0.0);cvs.clearScreen();glLineWidth(2.0);drawArc(p, 50,45,90);drawArc(p, 40,15,90);drawArc(p, 30,0,90);drawArc(p, 20,90,90);

Page 5: CSC 706 Computer Graphics Turtle graphicsnatacha/TeachFall_2009/CSC706/... · drawArc(Point2 center, float radius, float startAngle, float sweep) { // startAngle and sweep are in

5

25

Parametric Curves

A curve can be described:1. Implicitly

F(x,y) = 0;F(x,y) = (y – Ay)(Bx-Ax) – (x-Ax)(By-Ay)F(x,y) = 0 for all points on the curve.F(x,y) > 0 for all points outside the curve.F(x,y) < 0 for all points inside the curve.

26

Parametric form for curves

There are two principal ways to describe the shape of a curved line: implicitly and parametrically. The implicit form describes a curve by a function F(x, y) that provides a relationship between the x and ycoordinates.

For example, the straight line through points A and B has implicit form:

(3.8)

and the circle with radius R centered at the origin has implicit form:

(3.9)

A benefit of using the implicit form is that you can easily testwhether a given point lies on the curve: simply evaluate F(x, y) at the point in question.

27

Parametric Curves

• F(x,y) = x2 + y2 - R2

• F(x,y) < 0 (inside)

28

Parametric Curves

• F(x,y) = x2 + y2 - R2

• F(x,y) > 0 (outside)

29

Parametric Curves

• F(x,y) = x2 + y2 - R2

• F(x,y) = 0 (on the circle)

30

A parametric form for a curve produces different points on the curve based on the value of a parameter. Parametric forms can bedeveloped for a wide variety of curves, and they have much to recommend them, particularly when one wants to draw or analyze the curve. A parametric form suggests the movement of a point through time, which we can translate into the motion of a pen as it sweeps out the curve. The path of the particle traveling along the curve is fixed by two functions, x( ) and y( ), and we speak of (x(t), y(t)) as the position of the particle at time t. The curve itself is the totality of points “visited” by the particle as t varies over some interval. For any curve, therefore, if we can dream up suitable functions x( ) and y( ) they will represent the curve concisely and precisely.

cg5

Page 6: CSC 706 Computer Graphics Turtle graphicsnatacha/TeachFall_2009/CSC706/... · drawArc(Point2 center, float radius, float startAngle, float sweep) { // startAngle and sweep are in

6

31

Parametric Curves

• Problem with implicit representationThere may be more than one x value for each y value or vice versa.

Some functions may be single valued in x.Some functions may be single valued in y.

xRy 22−±=32

Parametric Curves

single valued in y single valued in x

33

Parametric Curves

A curve can be described:2. Parametrically

• different points on a curve are produced for a parameter.

• the value of the parameter suggests the movement along the curve through time.

34

Parametric Curves

time

35

Parametric Curves

time

36

Parametric Curves

time

Page 7: CSC 706 Computer Graphics Turtle graphicsnatacha/TeachFall_2009/CSC706/... · drawArc(Point2 center, float radius, float startAngle, float sweep) { // startAngle and sweep are in

7

37

Parametric Curves

time

38

Parametric Curves

time

39

Parametric Curves

time

40

Parametric Curves

time

41

Parametric Curves

• The path of the particle moving along the curve is fixed by two functions, x() and y().

• The position of the particle at any point in time is (x(t), y(t)).

42

Parametric Curves

For example:A straight line from A to B. At time t=0

the particle is at A and at time t=1 the particle is at B.

A

Bt=1

t=0

Page 8: CSC 706 Computer Graphics Turtle graphicsnatacha/TeachFall_2009/CSC706/... · drawArc(Point2 center, float radius, float startAngle, float sweep) { // startAngle and sweep are in

8

43

Parametric Curves

Therefore:x(t) = Ax + (Bx – Ax)ty(t) = Ay + (By – Ay(t)The point moves through all points on the line

between A and B at t goes from 0 to 1.

A

Bt=1

t=0

44

Parametric Curves

For example: An Ellipse.

H * sin(t)

W * cos(t)

45

Parametric Curves

//plot ellipseGLdouble width = 5.0;GLdouble height = 2.0;GLdouble TWOPI = 2 * 3.14159265;float interval = 250.0;

setWindow(-width - 2, width + 2, -height - 2, height + 2);

//leave a gap of 2 around the plotsetViewport(0,640,0,480);glBegin(GL_POINTS);

for(GLdouble t = 0; t <= TWOPI; t += TWOPI/interval ){

glVertex2d(width * cos(t), height * sin(t));}

glEnd();

46

Parametric Curves

47

Polar Coordinates

• Each point on a curve is represented by a radial distance (r) and an angle (Θ).

• r and Θ are a function of t• The point t is therefore

specified as (r(t), Θ(t))• x(t) = r(t)cos(Θ(t))• y(t) = r(t)sin(Θ(t))

48

Polar Coordinates

• For each point, x and y can be simplified to:

x = f(Θ)cos(Θ)y = f(Θ)sin(Θ)

Page 9: CSC 706 Computer Graphics Turtle graphicsnatacha/TeachFall_2009/CSC706/... · drawArc(Point2 center, float radius, float startAngle, float sweep) { // startAngle and sweep are in

9

49

Polar CoordinatesExample: Rose Curves

void drawPolar(Point2 center, float K, int n){

int samples = 100;float x, y;float o=0, inc = (360*PI/180.0)/(float)samples;

x = K*cos(n*o)*cos(o) + center.getX();y = K*cos(n*o)*sin(o) + center.getY();cvs.moveTo(x,y);

for(int i = 0; i <= samples; o+=inc, i++){

x = K*cos(n*o)*cos(o) + center.getX();y = K*cos(n*o)*sin(o) + center.getY();cvs.lineTo(x,y);

}

} - Rosecurves 50

Polar Coordinates

Example: Rose Curves

cvs.setColor(1.0, 0.0, 0.0);drawPolar(center, 70, 10);cvs.setColor(0.0, 1.0, 0.0);drawPolar(center, 60, 6);cvs.setColor(0.0, 0.0, 1.0);drawPolar(center, 30, 3);