Computer Graphics clipping opengl 
1Computer GraphicsClippingFall FCC 2006Line ClippingWhat happens when one or both endpoints of a line segment are not inside the specified drawing area?Draw just the portions of a line (or object)that fall within a given region/window/screen (usually rectangular)DrawingAreaLine Clipping•Strategies for clipping:a)Check (in inner loop)if each point is inside Works, but slowb)Clip invalid coordinate(s) to boundary Incorrect resultsc)Find intersection of line with boundary Correctif (x >= xmin && x <= xmax &&y >= ymin && y <= ymax)drawPoint(x,y,c);if (x < xmin) x = xmin;else if (x > xmax) x = xmax;if (y < ymin) y = ymin;else if (y > ymax) y = ymax;InputOutputClip xClip yClip line tointersectionLine Clipping: Possible Configurations1.Both endpoints are inside the region (line AB)No clipping necessary2.One endpoint in, oneout (line CD)Clip at intersection point3.Both endpoints outsidethe region:a.No intersection (lines EF, GH)b.Line intersects the region (line IJ)-Clip line at both intersection pointsABCDFEIJGHLine Clipping: Cohen-Sutherland•Basic algorithm:Accept (and draw)lines that have both endpoints inside the regionFETrivially rejectABTrivially acceptHCDIJGClip andretestClip the remaining lines at a region boundary and repeat steps 1 and 2 on the clipped line segmentsReject (and don’t draw)lines that have both endpoints less than xminor yminor greater than xmaxor ymaxCohen-Sutherland: Accept/Reject Tests•Assign 4-bit code to each endpoint corresponding to its position relative to region:Firstbit (1000): if y> ymaxSecondbit (0100): if y< yminThirdbit (0010): if x> xmaxFourthbit (0001): if x< xmin•Test:if code0OR code1= 0000accept (draw)else if code0AND code10000reject (don’t draw)else clip and retest010001010110100010011010000100100000Cohen-Sutherland: Line ClippingIntersectionalgorithm:if code00000 then code= code0else code= code1dx= x1–x0; dy= y1–y0if codeAND1000 then begin//ymaxx= x0+ dx* (ymax–y0) /dy; y= ymaxendelse if codeAND0100 then begin//yminx= x0+ dx* (ymin–y0) /dy; y= yminendelse if codeAND0010 then begin//xmaxy= y0+ dy* (xmax–x0) /dx; x= xmaxendelse begin//xminy= y0+ dy* (xmin–x0) /dx; x= xminendif code= code0thenbegin x0= x; y0= y; endelsebegin x1= x; y1= y; end(x1, y1)(x0, y0)ymaxymindxdy(x, y)xminxmaxCohen-Sutherland: Line Clipping ExampleIntersectionalgorithm:if code00000 then code= code0else code= code1dx= x1–x0; dy= y1–y0if codeAND1000 then begin//ymaxx= x0+ dx* (ymax–y0) /dy; y= ymaxendelse if codeAND0100 then begin//yminx= x0+ dx* (ymin–y0) /dy; y= yminendelse if codeAND0010 then begin//xmaxy= y0+ dy* (xmax–x0) /dx; x= xmaxendelse begin//xminy= y0+ dy* (xmin–x0) /dx; x= xminendif code= code0thenbegin x0= x; y0= y; endelsebegin x1= x; y1= y; endCodedxyxdy(x1, y1)(400, 300)Code (1010)(x0, y0)(150, 150)Code (0000)ymax=200ymin=100Xmin= 100xmax = 300Cohen-Sutherland: Line Clipping ExampleIntersectionalgorithm:if code00000 then code= code0else code= code1dx= x1–x0; dy= y1–y0if codeAND1000 then begin//ymaxx= x0+ dx* (ymax–y0) /dy; y= ymaxendelse if codeAND0100 then begin//yminx= x0+ dx* (ymin–y0) /dy; y= yminendelse if codeAND0010 then begin//xmaxy= y0+ dy* (xmax–x0) /dx; x= xmaxendelse begin//xminy= y0+ dy* (xmin–x0) /dx; x= xminendif code= code0thenbegin x0= x; y0= y; endelsebegin x1= x; y1= y; endCodedxyxdy(x1, y1)(400, 300)Code (1010)(x0, y0)(150, 150)Code (0000)ymax=200ymin=100Xmin= 100xmax = 300Cohen-Sutherland: Line Clipping ExampleIntersectionalgorithm:if code00000 then code= code0else code= code1dx= x1–x0; dy= y1–y0if codeAND1000 then begin//ymaxx= x0+ dx* (ymax–y0) /dy; y= ymaxendelse if codeAND0100 then begin//yminx= x0+ dx* (ymin–y0) /dy; y= yminendelse if codeAND0010 then begin//xmaxy= y0+ dy* (xmax–x0) /dx; x= xmaxendelse begin//xminy= y0+ dy* (xmin–x0) /dx; x= xminendif code= code0thenbegin x0= x; y0= y; endelsebegin x1= x; y1= y; endCode1010dxyxdy(x1, y1)(400, 300)Code (1010)(x0, y0)(150, 150)Code (0000)ymax=200ymin=100Xmin= 100xmax = 300Cohen-Sutherland: Line Clipping ExampleIntersectionalgorithm:if code00000 then code= code0else code= code1dx= x1–x0; dy= y1–y0if codeAND1000 then begin//ymaxx= x0+ dx* (ymax–y0) /dy; y= ymaxendelse if codeAND0100 then begin//yminx= x0+ dx* (ymin–y0) /dy; y= yminendelse if codeAND0010 then begin//xmaxy= y0+ dy* (xmax–x0) /dx; x= xmaxendelse begin//xminy= y0+ dy* (xmin–x0) /dx; x= xminendif code= code0thenbegin x0= x; y0= y; endelsebegin x1= x; y1= y; end(x1, y1)(400, 300)Code (1010)(x0, y0)(150, 150)Code (0000)ymax=200ymin=100Xmin= 100xmax = 300Code1010dx250yxdy150Cohen-Sutherland: Line Clipping ExampleIntersectionalgorithm:if code00000 then code= code0else code= code1dx= x1–x0; dy= y1–y0if codeAND1000 then begin//ymaxx= x0+ dx* (ymax–y0) /dy; y= ymaxendelse if codeAND0100 then begin//yminx= x0+ dx* (ymin–y0) /dy; y= yminendelse if codeAND0010 then begin//xmaxy= y0+ dy* (xmax–x0) /dx; x= xmaxendelse begin//xminy= y0+ dy* (xmin–x0) /dx; x= xminendif code= code0thenbegin x0= x; y0= y; endelsebegin x1= x; y1= y; end(x1, y1)(400, 300)Code (1010)(x0, y0)(150, 150)Code (0000)ymax=200ymin=100Xmin= 100xmax = 300Code1010dx250yxdy150Cohen-Sutherland: Line Clipping ExampleIntersectionalgorithm:if code00000 then code= code0else code= code1dx= x1–x0; dy= y1–y0if codeAND1000 then begin//ymaxx= x0+ dx* (ymax–y0) /dy; y= ymaxendelse if codeAND0100 then begin//yminx= x0+ dx* (ymin–y0) /dy; y= yminendelse if codeAND0010 then begin//xmaxy= y0+ dy* (xmax–x0) /dx; x= xmaxendelse begin//xminy= y0+ dy* (xmin–x0) /dx; x= xminendif code= code0thenbegin x0= x; y0= y; endelsebegin x1= x; y1= y; end(x1, y1)(400, 300)Code (1010)(x0, y0)(150, 150)Code (0000)ymax=200ymin=100Xmin= 100xmax = 300Code1010dx250y200x233dy150Cohen-Sutherland: Line Clipping ExampleIntersectionalgorithm:if code00000 then code= code0else code= code1dx= x1–x0; dy= y1–y0if codeAND1000 then begin//ymaxx= x0+ dx* (ymax–y0) /dy; y= ymaxendelse if codeAND0100 then begin//yminx= x0+ dx* (ymin–y0) /dy; y= yminendelse if codeAND0010 then begin//xmaxy= y0+ dy* (xmax–x0) /dx; x= xmaxendelse begin//xminy= y0+ dy* (xmin–x0) /dx; x= xminendif code= code0thenbegin x0= x; y0= y; endelsebegin x1= x; y1= y; end(x1, y1)(400, 300)Code (1010)(x0, y0)(150, 150)Code (0000)ymax=200ymin=100Xmin= 100xmax = 300Code1010dx250y200x233dy150Cohen-Sutherland: Line Clipping ExampleIntersectionalgorithm:if code00000 then code= code0else code= code1dx= x1–x0; dy= y1–y0if codeAND1000 then begin//ymaxx= x0+ dx* (ymax–y0) /dy; y= ymaxendelse if codeAND0100 then begin//yminx= x0+ dx* (ymin–y0) /dy; y= yminendelse if codeAND0010 then begin//xmaxy= y0+ dy* (xmax–x0) /dx; x= xmaxendelse begin//xminy= y0+ dy* (xmin–x0) /dx; x= xminendif code= code0thenbegin x0= x; y0= y; endelsebegin x1= x; y1= y; end(x1, y1)(400, 300)Code (1010)(x0, y0)(150, 150)Code (0000)ymax=200ymin=100Xmin= 100xmax = 300Code1010dx250y200x233dy150Cohen-Sutherland: Line Clipping Summary1.Chooseanendpointoutsidetheclippingregion2.Usingaconsistentordering(toptobottom,lefttoright)findaclippingborderthelineintersects3.Discardtheportionofthelinefromtheendpointtotheintersectionpoint4.Setthenewlinetohaveasendpointsthenewintersectionpointandtheotheroriginalendpoint5.Youmayneedtorunthisseveraltimesonasingleline(e.g.,alinethatcrossesmultipleclipboundaries)Cohen-Sutherland Line Clip ExamplesABEFGHCDIJA 0001B 0100OR 0101AND 0000subdivideC 0000D 0010OR 0010AND 0000subdivideE 0000F 0000OR 0000AND 0000acceptG 0000H 1010OR 1010AND 0000subdivideI 0110J 0010OR 0110AND 0010reject010001010110100010011010000100100000Cohen-Sutherland Line Clip ExamplesABGHCDA 0001A’ 0001removeA’G’C’A’ 0001B 0100OR 0101AND 0000subdivideC 0000C’ 0000OR 0000AND 0000acceptC’ 0000D 1010removeG 0000G’ 0000OR 0000AND 0000acceptG’ 0000H 1010remove010001010110100010011010000100100000Cohen-Sutherland Line Clip ExamplesB’BA’ 0001B’ 0100removeA’B’ 0100B 0100OR 0100AND 0100reject010001010110100010011010000100100000Polygon ClippingWhat about polygons?For concave polygons, the intersectionwith the clipping region may be complexPolygon Clipping: Algorithm•Clip polygon to yminand ymax:Create empty output vertex list (listout= empty)Process input list (listin= (v0, v1, …, vn) where v0= vn)in orderFor each input vertex (viwhere 0 in–1):-If viis inside region Add vito end oflistout-If the line between viand vi+1intersects specified boundaries Add intersection point(s) to end oflistout•Repeat: clipping to xminand xmax•Post-process:Find “degenerate” sections where both sides of polygon has collapsed to region boundaryRemove those sections Create new polygonPolygon Clipping: ExampleClip first to yminand ymaxyminymaxv0v1v2v3v4v5v6v7v8v9Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)vertex:v0inside region: noadd p0to output listOutput vertex list:p0line intersect boundary: yes(p0)Polygon Clipping: ExampleClip first to yminand ymaxyminymaxv0v1v2v3v4v5v6v7v8v9Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)vertex:v1inside region: yesadd v1to output listOutput vertex list:p0line intersect boundary: no(p0, v1)Polygon Clipping: ExampleClip first to yminand ymaxyminymaxv0v1v2v3v4v5v6v7v8v9Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)vertex:v2inside region: yesadd v2, p1to output listOutput vertex list:p0(p0, v1, v2, p1)line intersect boundary: yesp1Polygon Clipping: ExampleClip first to yminand ymaxyminymaxv0v1v2v3v4v5v6v7v8v9Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)vertex:v3inside region: noOutput vertex list:p0(p0, v1, v2, p1)line intersect boundary: nop1Polygon Clipping: ExampleClip first to yminand ymaxyminymaxv0v1v2v3v4v5v6v7v8v9Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)vertex:v4inside region: noadd p2to output listOutput vertex list:p0(p0, v1, v2, p1, p2)p1line intersect boundary: yesp2Polygon Clipping: ExampleClip first to yminand ymaxyminymaxv0v1v2v3v4v5v6v7v8v9Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)vertex:v5inside region: yesadd v5, p3to output listOutput vertex list:p0(p0, v1, v2, p1, p2, v5, p3)p1p2line intersect boundary: yesp3Polygon Clipping: ExampleClip first to yminand ymaxyminymaxv0v1v2v3v4v5v6v7v8v9Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)vertex:v6inside region: noOutput vertex list:p0(p0, v1, v2, p1, p2, v5, p3)p1p2line intersect boundary: nop3Polygon Clipping: ExampleClip first to yminand ymaxyminymaxv0v1v2v3v4v5v6v7v8v9Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)vertex:v7inside region: noOutput vertex list:p0(p0, v1, v2, p1, p2, v5, p3)p1p2line intersect boundary: nop3Polygon Clipping: ExampleClip first to yminand ymaxyminymaxv0v1v2v3v4v5v6v7v8v9Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)vertex:v8inside region: noadd p4to output listOutput vertex list:p0(p0, v1, v2, p1, p2, v5, p3, p4)p1p2p3line intersect boundary: yesp4Polygon Clipping: ExampleClip first to yminand ymaxyminymaxv0v1v2v3v4v5v6v7v8v9Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)vertex:v9inside region: yesadd v9, p5to output listOutput vertex list:p0(p0, v1, v2, p1, p2, v5, p3, p4, v9, p5)p1p2p3p4line intersect boundary: yesp5Polygon Clipping: ExampleThis gives us a new polygonyminymaxv1v2v5v9with vertices:p0(p0, v1, v2, p1, p2, v5, p3, p4, v9, p5)p1p2p3p4p5Polygon Clipping: Example (cont.)Now clip to xminand xmaxxminxmaxInput vertex list:= (p0, v1, v2, p1, p2, v5, p3, p4, v9, p5)Output vertex list:(p0, p6, p7, v2, p1, p8, p9, p3, p4, v9, p5)v1v2v5v9p0p1p2p3p4p5p6p7p8p9Polygon Clipping: Example (cont.)Now post-processxminxmaxv9v3Output vertex list:(p0, p6, p7, v2, p1, p8, p9, p3, p4, v9, p5)Post-process:(p0, p6, p9, p3,) and (p7, v2, p1, p8) and (v4, v9, p5)p8p6v2p7p0p5p3p4p9p1Polygon OrientationpositiveNegativeABCDELRAEDCBLRLeft o RightLet A(x1,y1) and B(x2,y2) end points of a line. A point P(x,y) will be to the leftof the line segment if the expresion C= (x2-x1)*(y-y1)-(y2-y1)*(x-x1)is positive. We say the point is to the rightif C is negative.If P is to the right, it is outside the PolygonIf P is to the left, it is inside the Polygon