computer graphics using opengl, 3 rd edition f. s. hill, jr. and s. kelley
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 PresentationTRANSCRIPT
![Page 1: Computer Graphics using OpenGL, 3 rd Edition F. S. Hill, Jr. and S. Kelley](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/2.jpg)
![Page 3: Computer Graphics using OpenGL, 3 rd Edition F. S. Hill, Jr. and S. Kelley](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/6.jpg)
![Page 7: Computer Graphics using OpenGL, 3 rd Edition F. S. Hill, Jr. and S. Kelley](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/7.jpg)
![Page 8: Computer Graphics using OpenGL, 3 rd Edition F. S. Hill, Jr. and S. Kelley](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/8.jpg)
![Page 9: Computer Graphics using OpenGL, 3 rd Edition F. S. Hill, Jr. and S. Kelley](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/16.jpg)
Cohen-Sutherland Clipping Algorithm
![Page 17: Computer Graphics using OpenGL, 3 rd Edition F. S. Hill, Jr. and S. Kelley](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/18.jpg)
![Page 19: Computer Graphics using OpenGL, 3 rd Edition F. S. Hill, Jr. and S. Kelley](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/19.jpg)
![Page 20: Computer Graphics using OpenGL, 3 rd Edition F. S. Hill, Jr. and S. Kelley](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/20.jpg)
![Page 21: Computer Graphics using OpenGL, 3 rd Edition F. S. Hill, Jr. and S. Kelley](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/21.jpg)
![Page 22: Computer Graphics using OpenGL, 3 rd Edition F. S. Hill, Jr. and S. Kelley](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/23.jpg)
Regular Polygons
![Page 24: Computer Graphics using OpenGL, 3 rd Edition F. S. Hill, Jr. and S. Kelley](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052604/56814f16550346895dbca799/html5/thumbnails/25.jpg)
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.