computer graphics using opengl, 3 rd edition f. s. hill, jr. and s. kelley

25
Computer Graphics using OpenGL, 3 rd Edition F. S. Hill, Jr. and S. Kelley Chapter 3 Additional Drawing Tools PART II

Upload: conor

Post on 19-Mar-2016

67 views

Category:

Documents


5 download

DESCRIPTION

Computer Graphics using OpenGL, 3 rd Edition F. S. Hill, Jr. and S. Kelley. Chapter 3 Additional Drawing Tools PART II. Clipping Lines. We want to draw only the parts of lines that are inside the world window. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley

Computer Graphics using OpenGL, 3rd Edition

F. S. Hill, Jr. and S. Kelley

Chapter 3Additional Drawing Tools

PART II

Page 2: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley
Page 3: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley

Clipping Lines• We want to draw only

the parts of lines that are inside the world window.

• To do this, we need to replace line portions outside the window by lines along the window boundaries. The process is called clipping the lines.

Page 4: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley

Clipping• The method we will

use is called Cohen-Sutherland clipping.

• There are 2 trivial cases: a line AB totally inside the window, which we draw all of, and a line CD totally outside the window, which we do not draw at all.

Page 5: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley

Clipping

• For all lines, we give each endpoint of the line a code specifying where it lies relative to the window W:

Page 6: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley
Page 7: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley
Page 8: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley
Page 9: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley

Clipping

• The diagram below shows Boolean codes for the 9 possible regions the endpoint lies in (left, above, below, right).

Page 10: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley

Clipping• A line consists of 2

endpoints (vertices), P1 and P2. If we do not have a trivial case, we must alter the vertices to the points where the line intersects the window boundaries (replace P1 by A).

Page 11: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley

Clipping

• In the diagram, d/dely = e/delx (similar triangles).

• Obviously, A.x = W.r.• Also, delx = p1.x –

p2.x, dely = p1.y – p2.y and e = p1.x – W.r.

• So A.y = p1.y – d.

Page 12: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley

Clipping• New Point p1 (A) must be

computed: its x-coordinate is clearly w.r, but y-coordinate requires adjusting p1.y by the amount d

• 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

Page 13: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley

Clipping• Similar reasoning is used for clipping

against the other three edges of the window.

Page 14: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley

A Line Needing 4 ClipsA situation that requires all four clips.• The first clip changes P1 to A;• The second alters P2 to B;• The third finds P1 still outside and

below and so changes A to C;• The last changes P2 to D. • For any choice of ordering for the

chopping tests, there will always be a situation in which all 4 clips are necessary.

Page 15: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley

Clipping

These ideas are collected in the routine clipSegment(). The endpoints of the segment are passed by reference, since changes made to the endpoints by clipSegment() must be visible in the calling routine

Page 16: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley

Cohen-Sutherland Clipping Algorithm

Page 17: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley

Cohen-Sutherland Clipping Algorithm

• Each time through the do loop the code for each endpoint is recomputed and tested.

• When trivial acceptance and rejection fail, the algorithm tests whether p1 is outside, and so, it clips that end of the segment to a window boundary. If p1 is inside, then p2 must be outside, so p2 is clipped to a window boundary.

• This version of the algorithm clips in the order left, then right, then bottom, and then top.

Page 18: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley
Page 19: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley
Page 20: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley
Page 21: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley
Page 22: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley

Drawing Regular Polygons, Circles, and Arcs

• A polygon is regular if it is simple, if all its sides have equal length, 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.

• We give the name n-gon to a regular polygon having n sides; familiar examples are the 4-gon (a square), an 8-gon (a regular octagon) and so on.

Page 23: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley

Regular Polygons

Page 24: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley

Drawing Circles and Arcs

• Two methods: – The center is given, along with a point on

the circle. • Here drawCircle( IntPoint center, int radius)

can be used as soon as the radius is known. If c is the center and p is the given point on the circle, the radius is simply the distance from c to p, found using the usual Pythagorean Theorem.

Page 25: Computer Graphics using OpenGL,  3 rd  Edition F. S. Hill, Jr. and S. Kelley

Drawing Circles and Arcs

– Three points are given through which the circle must pass. • It is known that a unique circle passes

through any three points that don't lie in a straight line.

• Finding the center and radius of this circle is discussed in Chapter 4.