Computer Graphic 2 D Viewing
2-Dimensional Viewing Subjects : 2D-Viewing Viewing Transformation Clipping
2D Viewing Generally, Objects are defined in world coordinate coordinates value To display these on 2D screen, We need to transform Window Coordinates ( WC) value to screen (or display device) value (VC) Viewing Transformation : transformation of world coord. value to device coord. value
2D Viewing
Viewing Transformation Window a world-coord rectangular area selected for display View point a rectangular area on display device to which window is mapped a normalized device coord. are most often used
Viewing Transformation The process of viewing transformation can be performed by using the following steps: Clip the picture against the window Translate the clipped picture together with window until the lower left corner of windows comes at the origin the picture and the window are scaled until the window has the dimensions of the viewport Translate the scaled window to the correct position on the screen (view port ) Translation Scaling Translation
Steps for window- to- viewport transformation Y Y Ymax Ymin X X Xmin Xmax Window translated to origin Window in WC v v (Umax, Vmax) (Umin, Vmin) u u Window scaled to viewport size Translated by (Umin, Vmin) to final position
Viewing Transformation For given window boundary (xwmin, xwmax, ywmin, ywmax) and viewpoint boundary (xvmin, xvmax, yvmin, yvmax) A position (xw, yw) in window mapped into position(xv, yv) in viewport Where : scale factor
For Y-coordinate
Viewing Transformation Matrix Translate Scaling
Reverse Translation The combined viewing transformation matrix is:
Example: Derive the normalization matrix that maps a window specified by coordinates (1,1) and (4,5) onto viewport specified by coordinates (2,4) and (8,10)
Clipping of Lines in 2D Point Clipping clipping is performed relative to the window boundaries in world coordinate. Point Clipping check window boundaries with point P(Px, Py) If all four inequalities are satisfied then accept the point, otherwise reject it.
Line Clipping: Parametric Lines From the parametric representation of a line from (x1,y1) to (x2,y2) x = x1 + t*(x2-x1) y = y1 + t*(y2-y1) where t Î [0,1] we check for intersection points with the four boundaries x = xmin, x = xmax, y = ymin, y = ymax (lines parallel to the edges treated separately) • Computationally easier methods exist
Line Clipping line clipping determine wholly within the window wholly outside the window partially within the window boundary Cohen-Sutherland clipping algorithm Liang-Barsky clipping algorithm
Cohen-Sutherland Clipping Basic idea: every point (x,y) is coded using four bits to denote the position of the point w.r.t the window:[above,below,right,left]. For example: 1001 above and to the left Breaking Up Space above below right left 1 2 3 Region Code Legend Each region is represented by a 4-bit outcode b0b1b2b3:
Clipping Procedures Given a line segment, let o1=outcode(x1,y1), o2=outcode(x2, y2) (o1=o2=0) it inside clipping window (AB) (o1&o20) it is on the same outside sides of the window (EF) (o10, o2=0; or vice versa) one or two intersections must be computed, and the outcode of the intersection point is re-examined (CD) (o1&o2=0) Cannot tell, find the outcode of one intersection point (GH, IJ)
For lines that cannot be identified with 1) and 2) check for intersection with boundaries. ex) P1P2, P3P4 For P1P2 : Start from P1 1. check P1 region code : below window 2. find P1’ with bottom boundary discard P1, P1’ 3. check P2 region code : above & left of window 4. find P2’ : left window 5. find P2” : above window 6. save P1’ and P2’’
Intersection point with vertical boundary. For P3P4 : Start from P3 1. check P3 : left of window. 2. calculate P3’ : discard P3P3’ 3. check P3’ and P4 : discard Intersection point with vertical boundary. For P1(x1 y1) P2(x2 y2) Intersection point with horizontal boundary
Example: Consider the line from P1(-1,1) to P2(9,3) Example: Consider the line from P1(-1,1) to P2(9,3) . Determine its visibility against the clipping window defined by vertices (0,0 ),(8,0),(8,4),(0,4) using Cohen- Sutherland algorithm D(0,4) C(8,4) P2(9,3) P1(-1,1) A(0,0) B(8,0) Ymin=0 , Ymax=4 Xmin=0 Xmax=8 For p1: the opcode is o1=0001 for p2: the opcode is o2=0010
The slop of the line is The intersections with the window boundaries are calculated as follows: For Left edge : X=Xmin=0 ,Y=m(Xmin-X1)+Y1=0.2(0+1)+1=1.2 For right edge: X=Xmax=8 , Y=m(Xmax-X1)+Y1=0.2(8+1)+1=2.8 For bottom edge: Y=Ymin=0 , =5(0-1)+1=-6 For Top Edge Y=Ymax=4 , =5(4-1)-1=14 Check whether It seen that the above conditions are satisfied by the intersections with left and right edges only: Hence , the intersection points is (0,1.2) and (8,2.8)
Line Clipping: Liang-Barsky Clipping Algorithm Basic idea: Using the parametric representation of the line we solve for the parameter to see if and where the line intersects the window boundaries. For given (x1, y1), (x2, y2) parametric equation If a point (x, y) along the line is inside a window, then
Liang-Barsky Clipping Algorithm these inequalities can be written in the form k=1, 2, 3, 4 where p and q are defined as (Left) k=1 p1 = -Dx q1= x1- xwmin (Right) k=2 p2 = Dx q2= xwmax - x1 (Bottom) k=3 p3 = -Dy q3= y1- ywmin (Top) k=4 p4 = Dy q4= ywmax - y1
Liang-Barsky Clipping Algorithm Strategy : 1. If pk = 0 for some k then the line is parallel to a clipping boundary. Now test qk : if one qk < 0 for these k then line is outside if all qk ≥ 0 then line is inside 2. For all pk < 0 calculate u1 = max (0, {qk / pk}) to determine intersection point with the possibly extended clipping boundary k and obtain a new starting point for the line at u1. 3. For all pk > 0 calculate u2 = min (1, {qk / pk}) to determine intersection points with extended clipping boundary k and obtain a new end point at u2. 4. If u1 > u2 then discard the line 5. The line is now between [u1,u2]
Example 1: p1 < 0 , p2 > 0 , p3 < 0, p4 > 0 Example 1: p1 < 0 , p2 > 0 , p3 < 0, p4 > 0. For k=1,3 calculate q1/p1 (P1) and q3/p3 (P3) and choose u1 = max (0, q1/p1, q3/p3 ) = q1/p1 • Likewise for k=2,4 calculate q2/p2 (P2) and q4/p4 (P4) and choose u2 = min (1, q2/p2, q4/p4 ) = q4/p4 Ymin Xmin Xmax Ymax (x1,y1) P3 P1 P4 P2
Example 2: Consider horizontal lines with Δy = 0, p3 = p4 = 0. • For line 1, q4 < 0 and will be discarded. • For line 2, p1 < 0, p2 > 0, q3 > 0 and q4 > 0. We proceed to calculate u1 and u2. Line: 1 Ymax Line: 2 (x1,y1) (x2,y2) Ymin Xmin Xmax
• p2 > 0, calculate q2/p2 and choose u2 = min (q2/p2, 1) = q2/p2. Example 2: p1 < 0, note that q1 > 0 hence q1/p1 < 0, and u1 = max{0,q1/p1} = 0 • p2 > 0, calculate q2/p2 and choose u2 = min (q2/p2, 1) = q2/p2. • u1 < u2 and the line is between [u1,u2] Ymin Xmin Xmax Ymax (x1,y1) Line: 2 (x2,y2)
– p1 < 0, p2 > 0 , p3 < 0 and p4 > 0. Example 3: – p1 < 0, p2 > 0 , p3 < 0 and p4 > 0. – calculate q1/p1 (P1) and q3/p3 (P3) and choose u1 = max (0, q1/p1, q3/p3 ) = q1/p1 – calculate q2/p2 (P2) and q4/p4 (P4) and choose u2 = min (1, q2/p2, q4/p4 ) = q4/p4 – u1 > u2 hence discard line (x2,y2) P2 P1 P4 Ymax P3 Ymin (x1,y1) Xmin Xmax
Example:Let P1 (-1, -2), P2 (2, 4) XL = 0, XR = 1, YB = 0, YT = 1 For u1<u2 there is visible section compute new end points
The new intersection points (0,0) and (1/2,2)
ex) (3, 9) (4, 6) (2, 2) P1(x1’,y1’) (8, 1) (3, 1)
Polygon Clipping • Polygon Clipping Algorithm Uses a divide and conquer strategy, one window boundary at a time Accepts a series of polygon vertices and output another series of vertices defining the clipped polygon
Sutherland-Hodgman Algorithm Out In No output Output E Output I Output I,E
Sutherland-Hodgman Algorithm Out In No output Output E Output I Output I,E
Sutherland-Hodgman Algorithm Out In No output Output E Output I Output I,E
Sutherland-Hodgman Algorithm Out In No output Output E Output I Output I,E
Sutherland-Hodgman Algorithm Out In No output Output E Output I Output I,E
Sutherland-Hodgman Algorithm Out In No output Output E Output I Output I,E
Sutherland-Hodgman Algorithm Out In No output Output E Output I Output I,E
Sutherland-Hodgman Algorithm Out In No output Output E Output I Output I,E
Sutherland-Hodgman Algorithm Out In No output Output E Output I Output I,E
Sutherland-Hodgman Algorithm Out In No output Output E Output I Output I,E
Sutherland-Hodgman Algorithm Out In No output Output E Output I Output I,E
Sutherland-Hodgman Algorithm Out In No output Output E Output I Output I,E
Sutherland-Hodgman Algorithm Out In No output Output E Output I Output I,E
Sutherland-Hodgman Algorithm Out In No output Output E Output I Output I,E
Sutherland-Hodgman Algorithm Out In No output Output E Output I Output I,E
Sutherland-Hodgman Algorithm Out In No output Output E Output I Output I,E
Sutherland-Hodgman Algorithm Out In No output Output E Output I Output I,E Possible error for non-convex polygons