This Week Windows, Viewports and Clippings Creating Useful Drawing - - PDF document

this week
SMART_READER_LITE
LIVE PREVIEW

This Week Windows, Viewports and Clippings Creating Useful Drawing - - PDF document


slide-1
SLIDE 1

Computer Graphics Computer Graphics Computer Graphics Computer Graphics

CSC CSC480 476 Chapter Three Chapter Three

Computer Graphics C S 4 8

This Week

  • Windows, Viewports and Clippings
  • Creating Useful Drawing Tools
  • Turtle Graphics
slide-2
SLIDE 2

Computer Graphics C S 4 8

Windows and Viewports

  • Previously we looked at an OpenGL window

where x and y were plotted as positive pixel values.

  • However, we may not be interested in

keeping track of pixels like this.

  • We may prefer to plot points in the

coordinates in which they are given.

  • This means handling both positive and

negative values.

Computer Graphics C S 4 8

Windows and Viewports

  • We need to distinguish between the

world, world coordinates, world windows, the screen window and the viewport.

  • It’s like this….
slide-3
SLIDE 3

Computer Graphics C S 4 8

Screen

Windows and Viewports

The World (what you can see, the real world) Screen Window The World Window (the bit we want to capture) Viewport

Computer Graphics C S 4 8

Windows and Viewports

  • The world window is a rectangle.
  • The viewport is a rectangle.
  • Both are not necessarily the same

size or have the same aspect ratio.

  • Coordinates need to be stretched,

shrunk and moved to make them fit.

slide-4
SLIDE 4

Computer Graphics CS 480 `

Windows and Viewports

World Window Example Viewports

Computer Graphics CS 480 ´

Windows and Viewports

World Window Viewport (0,0) (0,0) (100,0) (100,0) This is called Mapping

slide-5
SLIDE 5

Computer Graphics CS 480 ^

Windows and Viewports

  • Recall from the last lecture:
  • x’ = Ax + B
  • y’ = Cy + D
  • This is exactly how mapping is

achieved!!

Computer Graphics C S 4 8

Windows and Viewports

  • Mapping involves scaling and

translation (moving).

  • Both the world window and viewport

can be any aligned rectangle.

  • Usually the viewport is set to take up

the entire screen window.

slide-6
SLIDE 6

4/6/2009 Computer Graphics C S 4 8 4/6/2009 Computer Graphics C S 4 8

slide-7
SLIDE 7

`

4/6/2009 Computer Graphics C S 4 8

Mapping from the Window to the Viewport

  • The Figure shows a world window and a viewport in more detail. The world

window is described by its left, top, right, and bottom borders as W.l, W.t, W.r, and W.b

  • The viewport is described likewise in the coordinate system of the screen '
  • pened at some place on the screen) by

V.l, V.t, V.r, and V.b

Computer Graphics C S 4 8

Windows and Viewports

W.t = Window top W.l = Window left W.b = Window bottom W.r = Window right V.t = Viewport top V.l = Viewport left V.b = Viewport bottom V.r = Viewport right

slide-8
SLIDE 8

´

Computer Graphics C S 4 8

Windows and Viewports

xmin, ymin, xmax, ymax = W.l, W.b, W.r, W.t SCREENWIDTH = V.r – V.l SCREENHEIGHT = V.t – V.b

4/6/2009 Computer Graphics C S 4 8

  • We derive a mapping or transformation, called the window-to-viewport
  • mapping. This mapping is based on a formula that produces a point (sx, sy) in

the screen window coordinates for any given point (x, y) in the world. sx = Ax + C, sy = By + D

A C

slide-9
SLIDE 9

^

Computer Graphics CS 480 `

Windows and Viewports

b W t W b W y b V t V b V sy . . . . . . − − = − −

  • The same holds for y:
  • or by rearranging

      − − − + − − = b W b W t W b W t V b V y b W t W b V t V sy . . . . . . . . . .

B D

4/6/2009 Computer Graphics CS 480 ´

Using the formulas in Equation 3.3, we obtain A = 180, C = 40, B = 240, and D = 60. Thus, for this example, the window-to-viewport mapping is sx = 180x + 40; sy = 240y + 60.

slide-10
SLIDE 10

4/6/2009 Computer Graphics CS 480 ^

PRACTICE EXERCISE

  • Find values of A,B, C, and D for the case of a

world window of (-10.0,10.0, -6.0,6.0) and a viewport of (0,600,0,400). What about the -ve values ??

Computer Graphics C S 4 8

Windows and Viewports

Example What are A, B, C & D ??

World Window Viewport World Window Viewport

(10,6) (-10,-6) 400 600

slide-11
SLIDE 11

Computer Graphics C S 4 8

Windows and Viewports

  • Do you need to perform these

calculations each time you draw something with OpenGL??

Computer Graphics C S 4 8

Windows and Viewports

  • No
  • OpenGL does all the

hard work for you.

  • But it is important

that you understand what is going on…..

slide-12
SLIDE 12

Computer Graphics C S 4 8

Windows and Viewports

  • Each time you call for a vertex to be

drawn (e.g. glVertex2f() etc..) the the coordinates of the point are passed coordinates of the point are passed through a set of transformations through a set of transformations that map world coordinates into viewport coordinates.

Computer Graphics C S 4 8

Windows and Viewports

  • First set the world window

coordinates with:

void glOrtho2D(GLDouble left, GLDouble right, GLDouble bottom, GLDouble top);

  • Then set the viewport with:

void glViewport(GLint xmin, GLint ymin, GLint width, GLint height);

slide-13
SLIDE 13

4/6/2009 Computer Graphics C S 4 8

  • Doing It in OpenGL
  • The world window is set by the function gluOrtho2D(),andthe
  • viewport is set by the function gIViewport () .These functions have the

prototypes void void gluOrtho D(GLdouble gluOrtho2D(GLdouble left, GLdouble right left, GLdouble , GLdouble bottom right, GLdouble , GLdouble bottom, GLdouble top top); ); which sets the window to have a lower left corner of (left, bottom) and an upper right corner of (right, top), and void void gIViewport(GLint x, GLint gIViewport(GLint , GLint width , GLint y, GLint , GLint height width, GLint height); ); which sets the viewport to have a lower left corner of (x, y) and an upper right corner of (x + width, y + height). By default, the viewport is the entire screen window:

  • By default, the viewport is the entire screen window:

If W and H are the width and height of the screen window, respectively, the

default viewport has lower left corner at (0, 0) and upper right corner at (W, H).

4/6/2009 Computer Graphics C S 4 8

  • Because OpenGL uses matrices to set up all its transformations, gluOrtho2D()

must be preceded by two "setup" functions: gIMatrixMode (GL_PROJECTION) gILoadIdentity() // sets the window gIMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho gluOrtho2D( D(0.0, , 2.0, , 0.0, , 1.0);

);

// sets the viewport gIViewport( gIViewport(40 40, , 60 60, , 360 360, , 240 240);

);

slide-14
SLIDE 14

4/6/2009 Computer Graphics CS 480 `

In Figures 2.10 and 2.17, the programs used the following instructions:

  • 1. in main():

glutInitWindowSize(640,480); // set screen window size This instruction sets the size of the screen window to 640 by 480. The default viewport was used, since no gIViewport () The default viewport was used, since no gIViewport () command was issued; the default viewport is the command was issued; the default viewport is the entire screen window entire screen window.

  • 2. in mylnit () :

glMatrixMode(GL_PROJECTION); gILoadIdentity(); gluOrtho2D(0.0, 640.0, 0.0, 480.0);

Computer Graphics CS 480 ´

Windows and Viewports

Example

World Window Viewport

(10,6) (-10,-6) 400 600

slide-15
SLIDE 15

Computer Graphics CS 480 ^

Windows and Viewports

void myInit(void) { glClearColor(1.0,1.0,1.0,0.0); glColor3f(0,0,0); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //set the viewing coordinates

gluOrtho2D(-10.0, 10.0, -6.0, 6.0); glViewport(0,0,600,400);

}

Computer Graphics C S 4 8

Windows and Viewports

glPointSize(10.0); glBegin(GL_POINTS); glVertex2i(-10,-6); glVertex2i(0,0); glVertex2i(10,6); glEnd(); *NOTE: Vertex are given in World Coordinates and OpenGL maps them to the Viewport Coordinates.

slide-16
SLIDE 16

Computer Graphics C S 4 8

Handy Functions

//--------------- setWindow

  • void

setWindow(GLdouble left, Gldouble right, GLdouble bottom, GLdouble top) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(left, right, bottom, top); } //---------------- setViewport

  • void

setViewport(GLdouble left, Gldouble right, GLdouble bottom, GLdouble top) { glViewport(left, bottom, right – left, top - bottom); }

Computer Graphics C S 4 8

Windows and Viewports

  • Plotting a function revisted..

//set the viewing coordinates setWindow(xmin, xmax, ymin, ymax); setViewport(0,640,0,480); glBegin(GL_POINTS); for(GLdouble x = xmin; x < xmax; x+=0.005 ) { glVertex2d(x, pow(2.7183, -x)*cos(2*3.14*x)); } glEnd();

  • example322.cpp
slide-17
SLIDE 17

`

4/6/2009 Computer Graphics C S 4 8

sinc(x) = [sin(PI *x)] / PI * x

void myDisplay(void) // plot the sine function, using world coordinates { setWindow(-5.0, 5.0, -0.3, 1.0); // set the window setViewport(0, 640, 0, 480); // set the viewport glBegin(GL_LINE_STRIP); for(GLfloat x = -4.0; x < 4.0; x += 0.1) // draw the plot glVertex2f(x, sin(3.14159 * x) / (3.14159 * x)); glEnd(); glFlush(); }

4/6/2009 Computer Graphics C S 4 8

Laying lots of copies of the same thing side by side to cover the entire screen window is called tiling the window. The picture that is copied at different positions is often called a motif.

tiling the window tiling the window

slide-18
SLIDE 18

´

4/6/2009 Computer Graphics C S 4 8

Tiling a screen window is easily achieved by using a different viewport for each instance of the motif. The Fig. shows a tiling involving 25 copies

  • f the motif. This tiling was generated with the following code:

setWindow(0, 640.0, 0, 440.0); // set a fixed window for(int i=0; i < 5; i++) // for each column for(int j=0; j < 5; j++) // for each row { glViewport(i * 64, j * 44, 64, 44); // set the next viewport drawPolylineFile("dino.dat"); // draw it again }

Note that each copy is drawn in a viewport of size 64 by 44 pixels whose aspect ratio (64/44) matches that

  • f

the world

  • window. Thus, each dinosaur is

drawn without distortion. void void gIViewport gIViewport(GLint (GLint x, GLint , GLint y, GLint , GLint width width, GLint , GLint height height); );

4/6/2009 Computer Graphics C S 4 8

Shows another tiling, but here alternate motifs are flipped upside down to produce an intriguing effect. This was done by flipping the window upside down every other iteration: interchanging the top and bottom values in setWindow () for(int i = 0 ; i < 5; i++) for(int j = 0; j < 5; j++) { if((i+j) % 2 == 0) // if (i+j) is even

setWindow(0.0, 640.0, 440.0, 0.0); // upside-down else setWindow(0 . 0 , 640.0, 0.0, 440.0); // right-side-up glViewport(i * 64, j * 44, 64, 44); // set the next viewport drawPolylineFile("dino.dat"); // draw it again }

?

slide-19
SLIDE 19

^

4/6/2009 Computer Graphics CS 480 `

  • Setting the Window and Viewport

Automatically

Setting of the Window

  • Often, the programmer does not know where the object

Often, the programmer does not know where the object

  • f interest lies, or how big it is, in world coordinates
  • f interest lies, or how big it is, in world coordinates.

Like the dinosaur earlier, the object might be stored in a stored in a file file, or it might be generated procedurally generated procedurally by some algorithm whose details are not known.

  • In either case, it is convenient to let the application

it is convenient to let the application determine a good window to use determine a good window to use.

4/6/2009 Computer Graphics CS 480 ´

Windows and Viewports Windows and Viewports

  • Example: Zooming In

– To zoom in on an object and create an animation, we need to create a number of frames and draw one after the other.

slide-20
SLIDE 20

4/6/2009 Computer Graphics CS 480 ^

[A] Setting the Window [A] Setting the Window

  • When setting the window size, you will want to

consider the scene you are trying to capture.

  • Too big and the objects will appear too small
  • Too small and the objects will be clipped

4/6/2009 Computer Graphics C S 4 8

[A] Setting the Window [A] Setting the Window

  • To set the window to an appropriate size, you

need to predetermine the size of the coordinates to be captured.

  • Then set the window to the minimum and

maximum extent of these coordinates.

slide-21
SLIDE 21

4/6/2009 Computer Graphics C S 4 8

[A] Setting the Window [A] Setting the Window

  • The program needs to pass over the data

twice:

1. Execute the drawing routine, but don’t do any drawing. 2. Execute the drawing routine again, but this time draw!

4/6/2009 Computer Graphics C S 4 8

[A] Setting the Window [A] Setting the Window

GLdouble ytemp; for(GLdouble x = xmin+0.005; x < xmax; x+=0.005 ) { ytemp = pow(2.7183,

  • x)*cos(2*3.14*x);

if (ytemp > ymax) ymax = ytemp; if (ytemp < ymin) ymin = ytemp; } setWindow(xmin, xmax, ymin, ymax); glBegin(GL_POINTS); for(GLdouble x = xmin; x < xmax; x+=0.005 ) { glVertex2d(x, pow(2.7183,

  • x)*cos(2*3.14*x));

} glEnd();

Pass 1 to calculate suitable ymin and ymax Pass 2

Set the window boundaries

slide-22
SLIDE 22

4/6/2009 Computer Graphics C S 4 8

[B] Setting the Viewport

  • To draw an undistorted version of the data in a

viewport, you need to ensure the viewport and the window have the same aspect ratio .

  • i.e.

viewport viewport window window

H W H W =

4/6/2009 Computer Graphics C S 4 8

[B] Setting the Viewport

  • Recall, if the aspect ratio of a rectangle is less than 1,

the rectangle is taller than wide.

  • E.g. W/H = 3/5
  • If the aspect ratio is greater than 1, the rectangle is

wider than tall.

  • E.g. W/H = 5/3
slide-23
SLIDE 23

4/6/2009 Computer Graphics C S 4 8

Automatic Setting of the Viewport to Preserve the Aspect Ratio

  • You need to specify a viewport that has the same aspect

ratio as the world window to avoid distortion version of a figure and will fit in the screen window.

  • Suppose the aspect ratio of the world window is known to

be R and the screen window has width W and height H.

  • There are two distinct situations: The world window may

have a larger aspect ratio than the screen window (R >

W/H), or it may have a smaller aspect ratio (R < W/H).

4/6/2009 Computer Graphics C S 4 8

  • The two situations are shown in Figure 3.16.

We consider each in turn.

Case (a): R > W/H. the viewport to match aspect ratio R will extend fully across the screen window, therefore, the viewport will have width W and height W/R, so it is set with the following command setViewport( setViewport(0, W, , W, 0, W/R); , W/R); Case (b): R < W/H. the viewport to match aspect ratio R will reach from the top to the bot-tom of the screen window. At its largest, the viewport will have height H, but width HR, so it is set with the command setViewport( setViewport(0, H * R, , H * R, 0, H); , H);

slide-24
SLIDE 24

4/6/2009 Computer Graphics CS 480 `

Set the Viewport

a) R > W/H (where R is the aspect ratio of the world window)

  • If the world window is ‘flatter’ than the screen window, there will

be unused space above and/or below.

  • The width of the world window will be mapped to the entire width
  • f the screen window. setViewport(0,W,0,W/R);

4/6/2009 Computer Graphics CS 480 ´

Set the Viewport

b) R < W/H (where R is the aspect ratio of the world window)

  • If the world window is ‘taller’ than the screen window, there will

be unused space on the sides.

  • The height of the world window will be mapped to the entire

height of the screen window. setViewport(0,H*R,0,H);

slide-25
SLIDE 25

4/6/2009 Computer Graphics CS 480 ^

  • EXAMPLE 3.2.7 A tall window

Suppose the window has aspect ratio R = 1.6 and the screen window has H = 200 and W = 360, and hence, W/H = 1.8. Therefore, Case (b) applies, and the viewport is set to have a height of H = 200 pixels and a width of = H * R = 320 pixels.

  • EXAMPLE 3.2.8 A short window

Suppose R = 2 and the screen window is the same as in the previous example. Then Case (a) applies, and the viewport is set to have a height of W/R = 180 pixels and a width of W = 360 pixels.

4/6/2009 Computer Graphics C S 4 8

Resizing the Screen Window

  • In a windows-based system the user can resize the screen window

at run time. This action generates a resize event that the system can respond to.

// specifies the function myReshape called on an resize event

  • glutReshapeFunc(myReshape);
  • void myReshape(GLsizei W, GLsizei H);

When this function is executed, the system automatically passes it the new width and height of the screen window, which the function can then use in its calculations. (GLsizei is a 32-bit integer; see Figure 2.7.)

  • This works the same as for the glutMouseFunc and

glutKeyboardFunc …

  • If you don’t run a glViewport after a window resize, the glViewport

defaults to the window coordinates.

slide-26
SLIDE 26

4/6/2009 Computer Graphics C S 4 8

Making a Matched Viewport

  • One common approach is to find a new viewport

that both fits into the new screen window and has the same aspect ratio as the world window. Matching the aspect ratios of the viewport and world window in this way will prevent distortion in the new picture.

  • Figure 3.17 shows a version of myReshape ()

that does the desired matching:

4/6/2009 Computer Graphics C S 4 8

void myReshape(GLsizei W, GLsizei H) { if(R > W/H) // use (global) window aspect ratio setViewport(0, W, 0, W/R); else setViewport(0, H * R, 0, H); }

  • It finds the largest matching viewport (i.e., one that matches

the aspect ratio R of the window) that will fit into the new screen window. The routine obtains the width and height of the new screen window through its arguments. The code is a simple embodiment of the result in Figure 3.16.

slide-27
SLIDE 27

`

4/6/2009 Computer Graphics C S 4 8

PRACTICE EXERCISES Page 95 By the student

4/6/2009 Computer Graphics C S 4 8

Clipping Lines

  • In this section, we describe a classic line-clipping

algorithm, the Cohen-Sutherlan clipper, that computes which part (if any) of a line segment with endpoints p 1 and p2 lies inside the world window lies inside the world window and then reports back the endpoints of that part.

slide-28
SLIDE 28

´

4/6/2009 Computer Graphics C S 4 8

  • Figure 3.18 shows a typical situation covering some of the many

possible actions of a clipper.

  • The function clipSegment ( ) does one of four things to each line

segment

  • 1. If the entire line lies within the window (e.g., like segment CD), the

function returns a value of 1.

  • 2. If the entire line lies outside the window (e.g., like segment AB), the

function it returns a value of 0.

  • 3. If one endpoint is inside the window and one is outside (e.g., like

segment ED),| the function clips the portion of the segment that lies

  • utside the window and returns a value of 1.
  • 4. If both endpoints are outside the window, but a portion of the segment

passe( through it (e.g., like segment AE), the function clips both ends and returns value of 1.

4/6/2009 Computer Graphics C S 4 8

Left -- Above ---- Right --- Below

slide-29
SLIDE 29

^

4/6/2009 Computer Graphics CS 480 `

Testing for a Trivial Accept or Trivial Reject

  • Trivial accept: Both code words are FFFF;
  • Trivial reject: The code words have a T in the

same position; both points are to the left of the window, or both are above, etc. How? Code1 AND Code2 != 0 . First, endpoint pairs are checked for trivial acceptance. If the line cannot be trivially accepted, region checks are done. Now logically AND together the two region codes. A non-zero result means the line must be totally outside the window. Why?

4/6/2009 Computer Graphics CS 480 ´

Chopping When There Is Neither Trivial Accept nor Trivial Reject

  • The Cohen

Cohen-

  • Sutherland algorithm

Sutherland algorithm uses a divide-and-conquer strategy. If the segment can be neither trivially accepted nor rejected, it is broken into two parts at one of the window

  • boundaries. One part lies outside the window

and is discarded. The other part is potentially visible, so the entire process is repeated for this segment against another of the four window boundaries.

slide-30
SLIDE 30

4/6/2009 Computer Graphics CS 480 ^

  • The entire procedure gives rise to the following strategy:

do{ form the code words for p 1 and p2 if (trivial accept) return 1;

if (trivial reject) return 0; chop the line at the "next" window border;

against the left edge against the right edge against the bottom edge against the top edge

discard the "outside" part; }while(1); Thus, after at most four iterations, trivial acceptance or rejection is assured.

4/6/2009 Computer Graphics C S 4 8

Clipping Lines

Cohen-Sutherland Clipping Algorithm

Clipping when there is neither trivial accept nor reject.

  • A line that cannot be trivial accepted or rejected will have one

end point inside the window and one outside, or;

  • Will have one endpoint on one side and the other endpoint on the
  • ther so that the line crosses the window.

These lines need to be chopped at the window border intersections.

slide-31
SLIDE 31

4/6/2009 Computer Graphics C S 4 8

Clipping Lines

Cohen-Sutherland Clipping Algorithm

We need to determine where A is.

  • its x coordinate is window.right
  • its y coordinate can be

calculated using similar triangles..

delx e dely d =

4/6/2009 Computer Graphics C S 4 8

  • How is the chopping at each boundary done? Figure 3.22

shows an example involving the right edge of the window.

Look to the book page 98

slide-32
SLIDE 32

4/6/2009 Computer Graphics C S 4 8

Clipping Lines

Cohen-Sutherland Clipping Algorithm

We need to determine where A is.

e = p1.x – W.right delx = p2.x – p1.x; dely = p2.y – p1.y; d = e/delx * dely; p1.y += (W.right – p1.x) * dely/delx

4/6/2009 Computer Graphics C S 4 8

The type Point2 holds a 2D point, and the type RealRect holds an aligned rectangle. Both types are described fully in Section 3.4.)

int clipSegment(Point2& pi, Point2& p2, RealRect W) { do{

if (trivial accept) return 1; // some portion survives if (trivial reject) return 0; //no portion survives if (p1 is outside} { if (p1 is to the left) chop against the left edge else if (p1 is to the right) chop against the right edge else if (p1 is below) chop against the bottom edge else if (p1 is above) chop against the top edge } else // p2 is outside { if (p2 is to the left) chop against the left edge else if (p2 is to the right) chop against the right edge else if (p2 is below) chop against the bottom edge else if (p2 is above) chop against the top edge }

}while(1); }

slide-33
SLIDE 33

4/6/2009 Computer Graphics C S 4 8

  • A situation that requires

all four clips is shown in Figure 3.24.

The 1st clip changes p1 to A 2nd finds p1 still outside and bellow and so changes A to C Last changes p2 to D 3rd alters p2 to B

4/6/2009 Computer Graphics C S 4 8

slide-34
SLIDE 34

4/6/2009 Computer Graphics CS 480 `

PRACTICE EXERCISE

3.3.1 Hand simulation of clipSegment () Go through the clipping routine by hand for the case of a window given by {left, right, bottom, top} = (30, 220, 50, 240) and the following line segments:

  • 1. p1 = (40,140),p2 = (100,200);
  • 2. p2 = (10, 270), p1 = (300, 0);
  • 3. p1 = (20,10),p2 = (20,200);
  • 4. p1 = (0, 0), p2 = (250,250);

In each case, determine the endpoints of the clipped segment, and for a visual check, sketch the situation on graph paper.

4/6/2009 Computer Graphics CS 480 ´

Section: 3.4 and 3.5

  • We will study only Some useful Supporting Classes
slide-35
SLIDE 35

4/6/2009 Computer Graphics CS 480 ^

Developing The CANVAS class

Some useful Supporting Classes

class Point2 //single point w/ floating point coordinates { public: Point2() {x = y = 0.0f;} //constructor 1 Point2(float xx, float yy) {x=xx; y=yy;} //constructor 2 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; };

4/6/2009 Computer Graphics CS 480 `

class IntRect //aligned rectangle with integer coordinates, used for viewport { 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 int getL(void) { return l; } int getR(void) { return r; } int getT(void) { return t; } int getB(void) { return b; } private: int l, r, b, t;

};

slide-36
SLIDE 36

4/6/2009 Computer Graphics CS 480 `

class RealRect //simlar to IntRect but w/ floating points & used for world window { public: RealRect() {l = 0; r=100; b=0; t=100;} //constructors RealRect(float left, float right, float bottom, float top) {l = left; r=right; b=bottom; t=top;} void set(float left, float right, float bottom, float top) { l=left; r=right; b=bottom; t=top; } float getL(void) { return l; } float getR(void) { return r; } float getT(void) { return t; } float getB(void) { return b; } void draw(void); //draw this rectangle using OpenGL private: float l, r, b, t;

}; //<<End Support Classes>>>

4/6/2009 Computer Graphics CS 480 `

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); void moveRel(float dx, float dy); private: Point2 CP; //current position in the world IntRect viewport; //the current window RealRect window; //the current viewport

} ;

slide-37
SLIDE 37

`

4/6/2009 Computer Graphics CS 480 `

Relative Drawing

  • This section refers to the functions developed in

the Canvas class from section 3.4 in the textbook.

  • You will need to program this class on your own

as you will need to implement it.

4/6/2009 Computer Graphics CS 480 `

Relative Drawing

  • Relative Drawing refers to moving the drawing

point on the canvas relative to the current drawing location.

  • Think of it like moving a pen on paper.
  • moveRel() and lineRel()

The function moveRel() is simple: it just moves the CP through the displacement (dx, dy).

slide-38
SLIDE 38

´

4/6/2009 Computer Graphics CS 480 `

void Canvas:: moveRel(float dx, float dy) { CP.set(CP.getX() + dx, CP.getY() + dy); }

The function lineRel(float dx, float dy) does this too, but it first draws a line from the old CP to the new one.

void Canvas:: lineRel( float dx, float dy) { float x = CP.getX() + dx, y = CP.getY() + dy ; lineTo(x,y); CP.set(x, y); }

4/6/2009 Computer Graphics CS 480 `

Turtle Graphics

  • Drawing lines and points on the screen.
  • Commanding a drawing turtle (pen in a plotter).

moveto(), forward(), turn() turnTo(float angle) This function turns the turtle to given angle and is implemented as Void Canvas :: turnTo(float angle) { CD = angle;} turn (float angle) This function turns the turtle through angle degrees Void Canvas :: turn(angle) {CD += angle;} See page 107-110 for more details

slide-39
SLIDE 39

^

4/6/2009 Computer Graphics CS 480 ` `

Void Canvas :: forward(float dist, int isVisible) { // Code …. See page 107 } Old world CP New world CP

4/6/2009 Computer Graphics CS 480 ` ´

slide-40
SLIDE 40

4/6/2009 Computer Graphics CS 480 ` ^

Turtle Graphics

Example: Polyspirals

for( some iterations ) { forward(length, 1); turn(angle); length += increment; }

  • polyspiral.cpp

4/6/2009 Computer Graphics CS 480 ´

Turtle Graphics

Example: Polyspirals

float angle = 87, inc = 0.5;

slide-41
SLIDE 41

4/6/2009 Computer Graphics CS 480 ´

Turtle Graphics

Example: Polyspirals

float angle = 170, inc = 1;

4/6/2009 Computer Graphics CS 480 ´

Turtle Graphics

Example: Polyspirals

float angle = 89.5, inc = 1;

slide-42
SLIDE 42

4/6/2009 Computer Graphics CS 480 ´

3.6 Figures Based on Regular Polygons

  • Definition: A polygon is a regular if it is simple, if all its

sides have equal lengths equal lengths, and if adjacent sides meet at equal interior angles equal interior angles.

  • Give the name n-gon to a regular polygon having n sides.

4/6/2009 Computer Graphics CS 480 ´

Regular Polygons

  • To draw a regular

polygon we need to specify the centre, the radius and a rotation angle.

( )

/n) i sin(2π R i/n), cos(2π R i

P =

0 <= i <= n -1 The center is (0, 0)

slide-43
SLIDE 43

4/6/2009 Computer Graphics CS 480 ´

void ngon(int n, float cx, float cy, float radius, float rotAngle) { //assumes global Canvas object, cvs if(n < 3) return ; //bad number of sides double angle = rotAngle * 3.14159265 / 180; // rotAngle is the starting angle = 0 for the

// above figure … angle in radians

double angleInc = 2 * 3.14159265 / n; //calculate angle increment cvs.moveTo(radius * cos(angle) + cx, radius * sin(angle) + cy); // (cx,cy) is the origin (0,0) for the above figure. for(int k=0; k<n; k++) //repeat n times { angle += angleInc; cvs.lineTo(radius * cos(angle) + cx, radius + sin(angle) + cy); } } The center point (cx, cy) 60o

4/6/2009 Computer Graphics CS 480 ´

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

  • ngon.cpp

void ngon(int n, float cx, float cy, float radius, float rotAngle)

slide-44
SLIDE 44

4/6/2009 Computer Graphics CS 480 ´ `

The rosette and the golden 5-rosette

  • Rosettes are easy to draw: Simply connect

every vertex to every other.

The 5, 11, and 17 rosettes

4/6/2009 Computer Graphics CS 480 ´ ´

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; // void ngon(int n, float cx, float cy, float radius, float rotAngle) ngon(numVerts, center.getX(), center.getY(), radius, 0); }

number of intermediate segments in circle

slide-45
SLIDE 45

4/6/2009 Computer Graphics CS 480 ´ ^

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

4/6/2009 Computer Graphics CS 480 ^

Circles and Arcs

The 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; // starting angle in

radians

float angleInc = sweep * 3.14159265 /(180 * n); // angle increment float cx = center.getX(), cy = center.getY(); // get center 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));} }

slide-46
SLIDE 46

4/6/2009 Computer Graphics CS 480 ^

Circles and Arcs

// set center with (0, 0) p.set(0.0,0.0); cvs.clearScreen(); glLineWidth(2.0);

//drawArc(center, radius,startAngle, sweep)

drawArc(p, 50,45,90); drawArc(p, 40,15,90); drawArc(p, 30,0,90); drawArc(p, 20,90,90);