Presentation is loading. Please wait.

Presentation is loading. Please wait.

9.1 Si23_03 SI23 Introduction to Computer Graphics Lecture 9 – Clipping.

Similar presentations


Presentation on theme: "9.1 Si23_03 SI23 Introduction to Computer Graphics Lecture 9 – Clipping."— Presentation transcript:

1 9.1 Si23_03 SI23 Introduction to Computer Graphics Lecture 9 – Clipping

2 9.2 Si23_03 Clipping Fundamental operation in computer graphics Extracts a portion of graphical data we wish to see Needs to be fast, so often implemented in hardware

3 9.3 Si23_03 Clipping Points to a Window P Q.. xminxmax ymin ymax (x,y) VISIBLE IF xmin < x < xmax; ymin < y < ymax Here a simple test can be applied:

4 9.4 Si23_03 Clipping Lines to a Window A B C D E F G H I J Can we quickly recognise lines which need clipping?

5 9.5 Si23_03 Clipping to a Window Looking at end-points gives us a quick classification: – Both ends visible => line visible (AB) – One end visible, other invisible => line partly visible (CD) – Both ends invisible: If both end-points lie to same side of window edge, line is invisible (EF) Otherwise, line may be invisible (IJ) or partially visible (GH)

6 9.6 Si23_03 Cohen-Sutherland Line Clipping Algorithm Each end-point is coded according to its position relative to the window – Four-bit code assigned as follows: Bit 1Set if x < xmin Bit 2Set if x > xmax Bit 3Set if y < ymin Bit 4Set if y > ymax Both end-point codes 0000 => VISIBLE Logical AND = NOT 0000 => INVISIBLE Logical AND = 0000 => INVISIBLE or PART VISIBLE 100110001010 000100000010 010101000110

7 9.7 Si23_03 Cohen-Sutherland Line Clipping Algorithm To clip P 1 P 2 : – Check if P 1 P 2 totally visible or invisible – If not, for each edge in turn (left/right/ bottom/top): (i) Is edge crossed ? (if so, the corresponding bit is set for ONE of the points, say P 1 ) (ii) If so, replace P 1 with intersection with edge.

8 9.8 Si23_03 Example Clip against left, right, bottom, top boundaries in turn. P 1 : 1001 P 2 : 0100 P1P1 P2P2 x=xmin P 1 First clip to left edge, giving P 1 P 2

9 9.9 Si23_03 Example P2P2 x=xmin P 1 P 1 : 1000 P 2 : 0100 No need to clip against right edge Clip against bottom gives P 1 P 2 Clip against top gives P 1 P 2 P 2 P 1

10 9.10 Si23_03 Calculating the Intersection To calculate intersection of P 1 P 2 with, say left edge: Left edge: x = xmin Line : y - y 2 = m (x-x 2 ) wherem = (y 2 - y 1 ) / (x 2 -x 1 ) Thus intersection is (xmin, y*) where y* = y 2 + m (xmin - x 2 ) P1P1 P2P2

11 9.11 Si23_03 Other Line Clippers Cohen-Sutherland is efficient for quick acceptance or rejection of lines. Less so when many lines need clipping. Other algorithms are: – Liang-Barsky – Nicholl-Lee-Nicholl see:Hearn and Baker for details

12 9.12 Si23_03 Ivan Sutherland n Founder figure of computer graphics n Sketchpad developed in 1963 n See: http://www.sun.com/ 960710/feature3/iva n-profile.html

13 9.13 Si23_03 Clipping in SVG n There is very powerful clipping support in SVG – Elements are clipped against a path n The clip path can be more general than just a rectangle

14 9.14 Si23_03 Clipping in SVG n This clip path is then associated with the drawing elements to be clipped, as part of the style attribute {… drawing elements…}

15 9.15 Si23_03 Polygon Clipping Basic idea: clip each polygon side - but care needed to ensure clipped polygon is closed. A B C D E F

16 9.16 Si23_03 Sutherland-Hodgman Algorithm This algorithm clips a polygon against each edge of window in turn, ALWAYS keeping the polygon CLOSED Points pass through as in a pipeline INPUT: List of polygon vertices OUTPUT: List of polygon vertices on visible side of window edge

17 9.17 Si23_03 Sutherland-Hodgman Algorithm Consider a polygon side: starting vertex S; end vertex P and window edge x = xmin. What vertices are output? xmin S P S P S P S P II OUTPUT: - I, P I P

18 9.18 Si23_03 Example - Sutherland- Hodgman Algorithm Take each edge in turn - start with left edge Take each point in turn: (i) Input point and call it P - thus P = A (ii) If P is first point: - store as P 1 - output if visible (not in this particular example) - let S = P A B C D E F Input: A B C D E F

19 9.19 Si23_03 Example - Sutherland- Hodgman Algorithm A B C D E F Input: A B C D E F (iii) If P not first point, then if SP crosses window edge: - compute intersection I - output I output P if visible (iv) let S = P Output: A B C D E F A

20 9.20 Si23_03 Example - Sutherland- Hodgman Algorithm Finally, if some points have been output, then if SP 1 crosses window edge: - compute intersection I - output I A B C D E F Input: A B C D E F A G Output: A B C D E F G

21 9.21 Si23_03 Example - after clipping to left edge B C D E F A G The result of clipping against the left edge

22 9.22 Si23_03 Example - clip against right edge B C D E F A G E E B C D F A G E E INPUT: A B C D E F GOUTPUT: A B C D E E F G

23 9.23 Si23_03 Example - clip against bottom edge B C D F A G E E E F B C D A G E E F INPUT: A B C D E E E F GOUTPUT: A B C D E E F G

24 9.24 Si23_03 Example - clip against top edge B C D A G E E F C D A G E E F A BAB INPUT: A B C D E E E F GOUTPUT: A A B C D E E F G


Download ppt "9.1 Si23_03 SI23 Introduction to Computer Graphics Lecture 9 – Clipping."

Similar presentations


Ads by Google