Top Banner
Chapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING
62

Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Aug 08, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Chapter 5GRAPHIC PROCEDURE - CLIPPING AND FILLING

Page 2: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Window and View port

Displaying an image of a picture involves mapping the coordinates of the points and lines that from the picture into the appropriate coordinates on the device or workstation where the image is to be displayed. This is done through the use of coordinate transformations known as viewing transformation. To perform a viewing transformation, we deal with a finite region in the world-coordinate system called a window. The window can be mapped directly on to the display area of the device or on to a sub region of the display called a viewport. The window defines what is to be viewed; the viewport defines where it is to be displayed. In computer graphics terminology the term window originally referred to an area of picture that is selected for viewing. By changing the position of the viewport, we can view objects at different positions on the display area of an output device. Also by varying the size of viewport we can change the size and proportions of displayed objects. Window and viewport can be general polygon shaped or circular. For simplicity here we will consider rectangular window and viewport with edges of the rectangles parallel to the coordinate axes.

Page 3: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Clipping

Clipping is the process to determine which part of a scene or especially which points, lines (or curves) or portions of the lines (or curves) of a scene lie inside the clipping window. For view transformation [mapping of a part of world coordinate scene to device coordinate] only these elements only are retained for display and everything outside the window is discarded. The algorithm for clipping is different for different shape of clipping window and for different type of objects (line, curve etc)

Page 4: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Line clipping-Visibility test

Figure shows a 2D scene and a rectangular clipping window. It is defined by left (L), right (R), top (T) and bottom (B) and the edges are parallel to the axes of the display surface.

Any point (X,Y) is inside the clipping window provided that

XL≤X≤XR and YB≤Y≤YT

Where XL, XR, YB, YT are the X and Y coordinates respectively of the left, right, bottom and top edges of the window. The equal sign indicates that the points over the window boundary are included within the window. To determine the visibility of the lines with respect to the clipping boundary we have to deal with three types of line.

Lines totally visible

Page 5: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Lines totally invisible Lines partially visible.

Lines totally inside the clipping window i.e. if both the end points of the line are inside the clipping window is totally visible e.g. line ef. However lines are totally invisible if both the end points are exterior to the window e.g. line ab. If both the end points of a line are to the right of the window or to the left or above or below the window the line is completely exterior to the window and hence totally invisible. Lines which don’t fall to these two categories are partially visible e.g. line cd.

Line clipping Algorithm

Line clipping algorithm uses a 4 digit code to indicate which of the nine regions around a clipping window contain the end point of a line. The process of assigning the 4 bit code to the line end points is as follows…..

We consider XL & XR is the x coordinates of the left and right edges of the window and YT & YB is the y coordinates of the top and bottom edges of the window respectively. Let X,Y are the coordinates of any end point of the test line. The rightmost bit is taken as 1

Page 6: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

If X<XL then set bit 1 to 1 else 0 If X>XR then set bit 2 to 1 else 0 If Y<YB then set bit 3 to 1 else 0 If Y>YT then set bit 4 to 1 else 0

The figure shows the nine region code with respect to clipping window. It is to be noted that the codes for the diagonal regions of the window are obtained by adding the codes for the two adjacent regions.

Cohen-Sutherland Algorithm

This is one of the most popular lines clipping algorithm. The concept of assigning 4-bit region codes to the end points of a line and subsequent checking and AND operation of the endpoint codes to determine totally visible lines and totally invisible lines was originally introduced by Cohen and Sutherland in this algorithm. For clipping other totally invisible lines and partially

Page 7: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

visible lines the algorithm breaks the line segments into smaller sub segments by finding intersection with appropriate window edges. The process is repeated till two visible intersections are found or no intersections with any of the four visible window edges are found.

Step1: Input XL, XR, YT, YB, P1(X1,Y1), P2(X2,Y2) Initialize i=1 While i<=2 If Xi<XL then bit 1 of code – Pi=1 else 0 If Xi>XR then bit 2 of code – Pi=1 else 0 If Yi<YB then bit 3 of code – Pi=1 else 0 If Yi>YT then bit 4 of code – Pi=1 else 0

i=i+1 end while set i=1

Page 8: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Step2: while i<=2If bit 1 of code Pi=1 then Cileft=1 else Cileft=0If bit 2 of code Pi=1 then Ciright=1 else Ciright=0If bit 3 of code Pi=1 then Cibottom=1 else Cibottom=0If bit 4 of code Pi=1 then Citop=1 else Citop=0

i=i+1end whileset i=1

Step3: if the codes of P1 and P2 are both equal to zero then draw P1P2 (totally visible)

Step4: if logical intersection or AND operation of code –P1 and code –P2is not equal to zero then ignore P1P2 (totally invisible)

Step5: if code –P1=0 then swap P1 and P2 along with their flags and set i=1

Step6: if code –P1 < >0 then For i=1 { If C1left=1 then Find the intersection (XL,YL

′) with the left edge Assign code to (XL,YL

′) P1= (XL,YL

′) End if

i=i+1 go to step3 } For i=2 {

Page 9: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

If C1right=1 then Find the intersection (XR,YR

′) with the right edge Assign code to (XR,YR

′) P1= (XR,YR

′) End if

i=i+1 go to step3 }

For i=3 { If C1bottom=1 then Find the intersection (XB

′,YB) with the bottom edge Assign code to (XB

′,YB) P1= (XB

′,YB) End if i=i+1 go to step3 } For i=4 { If C1top=1 then Find the intersection (XT

′,YT) with the top edge Assign code to (XT

′,YT) P1= (XT

′,YT) End if

i=i+1 go to step3 }

End

Page 10: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Example : A clipping window ABCD is located as follows A(100,10), B(160,10), C(160,40), D(100,40). Using cohen-sutherland clipping algorithm find the visible portion of the line segment P1(120,5), P2(180,30)

At first considering the line P1P2

We have P1(120,5) and P2(180,30)X1=120 Y1=5 X2=180 Y2=30 XL=100 XR=160 YB=10 YT=40

Assigning code:

X1>XL then bit 1 of code – P1=0 C1 left=0 X1<XR then bit 2 of code – P1=0 C1 right=0 Y1<YB then bit 3 of code – P1=1 C1 bottom=1 Y1<YT then bit 4 of code – P1=0 C1 top=0

Code –P1=0100

X2>XL then bit 1 of code – P2=0 C2 left=0 X2>XR then bit 2 of code – P2=1 C2 right=1

Page 11: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Y2>YB then bit 3 of code – P2=0 C2 bottom=0 Y2<YT then bit 4 of code – P2=0 C2 top=0

Code -P2=0010

Code- P1< > 0 and code P2< >0 Then P1P2 not totally visible

Code- P1 AND code P2 =0 Hence the lineP1P2 not totally

invisible

As code –P1< > 0

For i=1{ C1 left=0 then nothing is done i=i+1=1+1=2}

Code- P1< > 0 and code P2< >0 Then P1P2 not totally visible

Code- P1 AND code P2 =0 Hence the lineP1P2 not totally

invisible

For i=2{ C1 right=0 then nothing is donei=2+1=3}

Code- P1< > 0 and code P2< >0

Page 12: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Then P1P2 not totally visible

Code- P1 AND code P2 =0 Hence the lineP1P2 not totally

invisibleFor i=3{ C1 bottom= 1 then find the intersection with bottom edge

YB=10

XB= +120= 132 then P1=(132,10)

X1>XL then bit 1 of code – P1=0 C1 left=0 X1<XR then bit 2 of code – P1=0 C1 right=0 Y1=YB then bit 3 of code – P1=0 C1 bottom=0 Y1<YT then bit 4 of code – P1=0 C1 top=0

Code –P1=0000i=i+3=4

}Code- P1= 0 and code P2< >0 Then P1P2 not totally visible

Code- P1 AND code P2 =0 Hence the lineP1P2 not totally

invisible

As code of P1=0Then swap P1 and P2 along with their flag

Page 13: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

P1=(180,30) P2=(132,10)

C1 left=0 C1 right=1 C1 bottom=0 C1 top=0

C2 left=0 C2 right=0 C2 bottom=0 C2 top=0

Reset i=1For i=1 { C1 left=0 then nothing is done i=i+1=1+1=2 }

Code- P1< > 0 and code P2< >0 Then P1P2 not totally visible

Code- P1 AND code P2 =0 Hence the lineP1P2 not totally

invisible

For i=2{ C1 right= 1 then find the intersection with right edge

XR=10

YR= +5= 21.667=22 then

P1=(160,22)

Page 14: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

X1>XL then bit 1 of code – P1=0 C1 left=0 X1=XR then bit 2 of code – P1=0 C1 right=0 Y1>YB then bit 3 of code – P1=0 C1 bottom=0 Y1<YT then bit 4 of code – P1=0 C1 top=0

Code –P1=0000i=2+1=3

}As both code-P1=0 and code –P2=0 then the line segment P1P2 is totally visible and the visible portion of the line is (160, 22) to (132, 10)

Midpoint Subdivision Algorithm

Partially visible and totally invisible lines which cannot be identified by checking and operating end point codes are subdivided into two equal segments by finding the midpoint. Each half is then separately considered and tested with endpoint codes for immediate identification of totally visible and totally invisible state. Segments which cannot be identified even then are further subdivided at midpoint and each subdivision is subsequently tested. This bisection and testing procedure is continued until the intersection with an edge is found with some specific accuracy. The steps of the algorithm are as follows.

Page 15: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Input: XL, XR, YT, YB, P1(X1,Y1), P2(X2,Y2) Assign code to P1 and P2 (4 bit code)

Step1: if the codes of P1 and P2 are both equal to zero then draw P1P2 (totally visible)

Step2: if logical intersection or AND operation of code –P1 and code –P2is not equal to zero then ignore P1P2 (totally invisible)

Step3: Pm= , assign code to (Pm)

Step4: if code –Pm < >0 then if the code of P1 AND code –Pm < > 0 then P1=Pm, go to step 1. else if the code of P2 AND code –Pm < > 0 then P2=Pm, go to step 1. end if. end if.

Page 16: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Step5: if the code of Pm=0 then if the code P1 and Pm both are 0 then consider P2Pm

else if code P2 and Pm are 0 then P1Pm

end if end if. Step6: considering P1Pm

do { Pm1= ,assign code to (Pm1)

if the code of Pm1< > 0 then P1= Pm1 else Pm=Pm1

} while (Pm.x < > xl or Pm.x < > xr or Pm.y < > yt or Pm.y < > yb) P1=Pm

Step7: considering P2Pm

do { Pm2= ,assign code to (Pm2)

if the code of Pm2< > 0 then P2= Pm2 else Pm=Pm2

} while (Pm.x < > xl or Pm.x < > xr or Pm.y < > yt or Pm.y < > yb) P2=Pm

Step8: draw P1P2 .

In the above algorithm a calculated midpoint Pm

is considered to be lying on any of the boundary lines of the window say boundary line x=x l if Pm.x=xl tolerence where tolerance is a very small number depending on the precision of the display.The midpoint subdivision algorithm works faster than Cohaen-Sutherland algo due to division by 2.

Page 17: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Example : A clipping window ABCD is located as follows A(100,10), B(160,10), C(160,40), D(100,40). Using midpoint subdivision algorithm find the visible portion of the line segment P1(120,5), P2(180,30)

At first considering the line P1P2

We have P1(120,5) and P2(180,30)X1=120 Y1=5 X2=180 Y2=30 XL=100 XR=160 YB=10 YT=40

Assigning code:

X1>XL then bit 1 of code – P1=0 X1<XR then bit 2 of code – P1=0 Y1<YB then bit 3 of code – P1=1 Y1<YT then bit 4 of code – P1=0

Code –P1=0100

Page 18: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

X2>XL then bit 1 of code – P2=0 X2>XR then bit 2 of code – P2=1 Y2>YB then bit 3 of code – P2=0 Y2<YT then bit 4 of code – P2=0

Code -P2=0010

Code- P1< > 0 and code P2< >0 Then P1P2 not totally visible

Code- P1 AND code P2 =0 Hence the lineP1P2 not totally

invisible

Pm= =( , )=(150,17.5)

Xm>XL then bit 1 of code – Pm=0 Xm<XR then bit 2 of code – Pm=0 Ym>YB then bit 3 of code – Pm=0 Ym<YT then bit 4 of code – Pm=0

As the y coordinate of Pm almost equal to 10 we take the point as target point. So the intersection point has been found at (132, 10).For the second intersection point

Page 19: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

As the x coordinate of Pm almost equal to 160 we take the point as target point. So the intersection point has been found at (160, 22).

Hence the visible portion of the line P1 P2 is (132,10) to (160,22)

Polygon Clipping

Using line clipping techniques we cannot attempt to clip a polygon although a polygon is simply a set of connected straight lines. The problem is that each edge when clipped separately using line clipping algorithm will not produce a truncated polygon rather it would produce a set of unconnected line segments as if the polygon is exploded. Therefore we need a separate algorithm to clip a polygon. Sutherland-Hodgman algorithm is one such standard method for clipping arbitrary shaped polygons with a rectangular clipping window.

Page 20: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Display of a polygon processed by a line-clipping algorithm

This is what the clipped polygon should look like.

To clip a polygon properly the polygon must be tested against each edge of the clip rectangle, new edge must be added and existing edges must be discarded, retained or divided. Even multiple polygons may result from clipping a single polygon. Line clipping algorithm which tests the line-endpoint out code to see which

Page 21: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

edge is to be clipped does not fulfill our intention.

Sutherland and Hodgman's polygon-clipping algorithm uses a divide-and-conquer strategy: It solves a series of simple and identical problems that, when combined, solve the overall problem. The simple problem is to clip a polygon against a single infinite clip edge. Four clip edges, each defining one boundary of the clip rectangle, successively clip a polygon against a clip rectangle.

It is to be noted that the difference between this strategy for a polygon and the Cohen-Sutherland algorithm for clipping a line: The polygon clipper clips against four edges in succession, whereas the line clipper tests the out code to see which edge is crossed, and clips only when necessary.

Steps of Sutherland-Hodgman's polygon-clipping algorithm

Polygons can be clipped against each edge of the window one at a time. Windows/edge intersections, if any, are easy to find since the X or Y coordinates are already known.

Vertices which are kept after clipping against one window edge are saved for clipping against the remaining edges.

Page 22: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Note that the number of vertices usually changes and will often increases.

We are using the Divide and Conquer approach.

Four cases of polygon clipping against one edge

When clipping polygon edges against a window edge we move from one vertex (V i) to the next vertex (Vi+1) and decide the output vertex according to the four simple rules as stated below.

Page 23: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Simply Case 1 : Wholly inside visible region -

save endpoint Case 2 : Exit visible region - save the

intersection Case 3 : Wholly outside visible region -

save nothing Case 4 : Enter visible region - save

intersection and endpoint

Because clipping against one edge is independent of all others, it is possible to arrange the clipping stages in a pipeline. The input polygon is clipped against one edge and any points that are kept are passed on as input to the next stage of the pipeline. These way four polygons can be at different stages of the clipping process simultaneously.

Now let us see the steps to clip an arrow shaped quadrilateral against a rectangular clip window flowing the above rules.

Page 24: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

STEP 1- Clip against Left edge

Input vertex list [V1, V2, V3, V4]

Edge V1 V2: output V1′Edge V2 V3: output V2

′, V3Edge V3 V4: output V4

Edge V4 V1:output V1

Output vertex list [ V1′,V2

′,V3,V4,V1]

Page 25: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

STEP 2- Clip against bottom edge

Input vertex list [V1′,V2

′,V3,V4,V1]

Edge V1′ V2

′: output V2′Edge V2

′ V3: output V2′′Edge V3 V4: output V3 , V′ 4

Edge V4 V1: output V1

Edge V1 V1 : output V′ 1′Output vertex list [V2 , V′ 2 , V′′ 3 , V′ 4, V1 ,V1 ]′

V3′

Page 26: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

STEP 3- Clip against bottom edge

Input vertex list [V2 , V′ 2 , V′′ 3 , V′ 4, V1 ,V1 ]′

Edge V2′ V2

′′: output V2′′Edge V2

′′ V3′: output V2′′′

Edge V3′ V4: output V3 ,V′′ 4

Edge V4 V1: output V4′Edge V1 V1 : output V′ 1 , V′′ 1′Edge V1

′ V2′: output V2′

Output vertex list [V2 , V′′ 2 , V′′′ 3 , V′′ 4, V4 , V′ 1 ,′′ V1 ,V′ 2 ]′

Page 27: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

STEP 4- Clip against bottom edge

Input vertex list [V2 , V′′ 2 , V′′′ 3 , V′′ 4, V4 , V′ 1 ,′′ V1 ,V′ 2 ]′

Edge V2′′ V2

′′′: output V2′′′Edge V2

′′′ V3′′: output V3′′

Edge V3′′ V4: output V4

Edge V4 V4′: output V4′

Edge V4′ V1 : output V′′ 4′′Edge V1′′ V1 : output V′ 1 , V′′′ 1′Edge V1

′ V2′: output V2′

Edge V2′ V2

′′: output V2′′Output vertex list [ V2 , V′′′ 3 , V′′ 4, V4 , V′ 4 , V′′ 1 ,′′′ V1 ,V′ 2 ,V′ 2 ]′′

Page 28: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

The final clipped polygon

Output vertex list [ V2 , V′′′ 3 , V′′ 4, V4 , V′ 4 , V′′ 1 ,′′′ V1 ,V′ 2 ,V′ 2 ]′′

We started with 4 vertices and ended with 9 vertices except V4 which remains after 4-step revision.

Because clipping against one clip-edge is independent of all others, it is possible to arrange the clipping stages in a pipeline. The input polygon is clipped against one edge and any points that are kept are passed on as input to the next stage of the pipeline. These way four

Page 29: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

polygons can be at different stages of the clipping process simultaneously for a rectangular clipping window. In another way we can clip a single polygon edge against all the window edge.

The Sutherland-Hodgman algorithm clips any polygon convex or concave against any convex polygon clipping window. In such a case to determine whether any point is inside or outside the clip edge we may use a simple rule. If the successive edges of the clipping polygon are considered anticlockwise the inside of the polygon is always to the left otherwise it is in the left.

If the position of any polygon vertex V i is to be determined with respect to the clipping edge PiPi+1 in general then the sign of the cross product of the vectors PiVi and PiPi+1 is tested.

If the sign of cross product of PiVi and PiPi+1 is positive then Vi is to the right of the line PiPi+1

Page 30: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

If the sign of cross product of PiVi and PiPi+1 is negetive then Vi is to the left of the line PiPi+1

If the cross product is zero then V i is on the line PiPi+1

Algorithm

Define variables

inVertexArray is the array of input polygon verticesoutVertexArray is the array of output polygon verticesNin is the number of entries in inVertexArrayNout is the number of entries in outVertex ArrayN is the number of edges of the clip polygonClipEdge[x] is the xth edge of clip polygon defined by a pair of verticess, p are the start and end point respectively of current polygon edgei is the intersection point with a clip boundaryj is the vertex loop counter

Define Functions

AddNewVertex(newVertex, Nout, outVertexArray) :Adds new Vertex to outVertexArray and then updates NoutInsideTest(testVertex, clipEdge[x])

Page 31: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

: Checks wheather the vertex lies inside the clip edge or not; return TRUE if inside

else returns FALSE Intersect(first,second,clipEdge[x]) :clips polygon edge(first,second) against clipEdge[x], outputs the intersection point

{ :begin mainx=1while(x n) :loop through all the n clip edges{Nout=0 : flush the outVertex Arrays=inVertexArray[Nin] : strat with the last vertex in inVertexArrayfor j=1 to Nin do : loop through Nin number polygon vertices(edges) {p=inVertexArray[j] if InsideTest(p,clipEdge[x])==TRUE then :cases 1 and 4 if InsideTest(s,clipEdge[x])==TRUE then AddNewVertex(p, Nout, outVertexArray) :case 1 else i=Intersects(s,p,clipEdge[x]) :case 4 AddNewVertex(i, Nout, outVertexArray) AddNewVertex(p, Nout, outVertexArray)end ifelse : if InsideTest(p,clipEdge[x])==FALSE cases 2 and 3

Page 32: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

if InsideTest(s,clipEdge[x])==TRUE then :case 2{ Intersects(s,p,clipEdge[x]) AddNewVertex(i, Nout, outVertexArray)end if :no action for case 3s=p :advance to next pair of verticesj=j+1end if :end {for} } x=x+1 :proceed to the next ClipEdge[x+1]Nin=Nout

inVertexArray=outVertexArray : the ouput vertex array for the current clip edge

becomes the input vertex array for the next clip

edge} : end while

} :end main

Page 33: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Cyrus-Beck line clipping algorithm

The Cyrus-Beck technique can be used to clip a 2D line against a rectangle or an convex polygon in the plane or a 3D line against an arbitrary convex polyhedron in 3D space. The parametric line algorithm finds the value of the parameter t in the parametric representation of the line segment for the point at which that segment intersects the infinite line on which the clip edge lies. Because all clip edges are in general intersected by the line four values of t are calculated.

You might wonder as to what a convex window? In general, on the basis of the shapes the windows are classified into two categories:

i) Convex shaped windows: Windows of a shape such that if we take any two points inside the window then the line joining them will never cross the window boundary, such shaped windows are referred as convex shaped windows.

ii) Concave or non Convex shaped windows: Windows of a shape such that if we can choose a pair of points inside the window such that

Page 34: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

the line joining them may cross the window boundary or some section of the line segment may lie outside the window. Such shaped windows are referred to as non-convex or concave shaped windows.

The line is represented parametrically as

P(t)=P0 + (P1 –P0) t where at t=0 P(t)=P0 and P(t)=P1 at t=1Let us consider an edge Ei of the clip rectangle and the outward normal to the Ni . Either the edge or the line segment may have to be extended to find the intersection point.Now let us consider an arbitrary point PEi on the edge Ei and consider the three vectors P(t)-PEi

from PEi to three designated points on the line from P0 to P1 ,at the intersection, inside or outside the clipping window. We can distinguish in which region a point lies by looking at the value of the dot product Ni.[P(t)-PEi]. This value is negative for a point lies inside the region, zero for a point on the line containing the edge and positive for a point that lies outside the region. The definitions of inside and outside of an edge

Page 35: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

correspond to a counterclockwise enumeration of the edges of the clip region.

Now we can solve for the value of t at the intersection of P0P1 with the edge Ni.[P(t)-PEi]=0

Substituting the value P(t) Ni.[P0 + (P1-P0)t-PEi]=0

Ni.[P0-PEi] +Ni.[P1 – P0]t=0Let D=[P1 – P0] be the vector from P0 to P1 and solve for t

t=

Page 36: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

It is to be noted that a valid value of t is obtained only if the denominator of the expression is nonzero. This is true only for N i 0

(the normal should not be zero) or D 0 (that is

P1 P0) or Ni.D 0 (that is the edge Ei and the line P0 to P1 are not parallel. If they are parallel no intersection is obtained.)

Considering each clipping edge to be infinite four values of t for a line segment is obtained. In the next step we determine the values correspond to internal intersections of the line segment with edges of the clip rectangle. As a first step any value of t outside the interval [0,1] can be discarded, since it lie outside P0P1. Next we need to determine whether the intersection lies on the clip boundary. To do this the intersections are characterized by ‘potentially entering’ (PE) or ‘potentially leaving’ (PL) the clip rectangle. If moving from P0 to P1 causes to cross a particular edge to enter the clipping region the intersection is PE or if it causes us to leave the region it is PL. formally intersections can be classified as PE or PL on the basis of the angle between P0P1 and Ni. if the angle is less than 90° the intersection is PL and if it is greater than 90° it is PE. The information is available in the sign of the dot product of Ni and P0P1

Page 37: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Ni.D<0 PE (angle greater than 90°)Ni.D >0 PL(angle less than 90°)

We must choose a (PE, PL) pair that defines the clipped line. The portion of the infinite line through P0 P1 that is within the clipping region is bounded by the PE intersection with the largest value of t which we call tE and the PL intersection with the smallest t value tL. the intersecting line segment is then defined by the range (tE,tL). As we are interested in intersecting P0P1 not the infinite line the range must be modified so that t=0 is the lower bound for tE

and t=1 is an upper bound for tL. But if tE>tL as in the case of line 2 we simply discard the line. Values of tE and tL that correspond to actual intersection are used to calculate the corresponding x and y coordinates.

Pre calculate Ni and select a PEi for each edge

For (each line segment to be clipped) {

Page 38: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

if (P1==P0) line is degenerate so clip as a point: else { tE=0 ; tL=1; for(each candidate intersection with a clip edge) { if (Ni.D !=0) { calculate t; use sign of Ni.D to categorize as PE or PL; if(PE) tE=max(tE,t); if(PL) tL=min(tL,t); } }

if (tE>tL) Return NULL;else return P(tE) and P(tL) as true clip intersection; }} Pseudo code for Cyrus-Beck parametric line clip intersection

Area Filling

Filling an area means finding coordinates of interior points and assigning values calculated by using one of the incremental shading schemes. It is also sometimes known as polygon filling. We can define two basic types of areas or regions- Interior-defined and Boundary-defined.

Page 39: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

An interior-defined region is a collection or patch of same color contiguous pixels. Pixels exterior to the region have different colors. In case of boundary defined region pixels that mark the boundary or outline of the region have a unique color that is not same as the color of the interior pixels.

The algorithms used for filling boundary-defined regions are known as boundary-fill algorithms. To fill a boundary –defined area with this approach we start with a point (pixel) inside the area and paint the area progressively towards the boundary. The input to the procedure is the coordinate of a sample pixel or seed pixel from the area, a fill color value and a boundary color value. The seed pixel is first set to fill color while its adjacent pixels are checked for boundary colors. If boundary color is not found then these pixels are set to fill color. Similarly the adjacent to the adjacent pixels are examined and filled. The process continues to set the pixels in an increasing area until we face the boundary pixels. When we consider neighbors to the left-right, top and bottom only the method is called four-connected method whereas when we consider the diagonal pixels as neighbor as well the method is known as eight-connected method.

Page 40: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

4-connected and 8-connected it’s about the neighbor

Boundary Fill Algorithm

The following is a procedure to fill up a boundary defined region using 8-connected fill method. The procedure Boundary Fill is a recursive procedure with fill color specified by a parameter ‘fill’ and boundary color specified by a parameter ‘boundary’

Page 41: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Input: the seed pixel (x,y), the fill color and the ‘boundary’ color

Procedure BoundaryFill (x, y, fill, boundary)If (Getpixel(x, y) < > fill and Getpixel (x, y) < > boundary) then

Setpixel(x, y, fill);BoundaryFill (x+1, y, fill, boundary);BoundaryFill (x-1, y, fill, boundary);BoundaryFill (x, y+1, fill, boundary);BoundaryFill (x, y-1, fill, boundary);BoundaryFill (x+1, y+1, fill, boundary);BoundaryFill (x-1, y+1, fill, boundary); BoundaryFill (x-1, y-1, fill, boundary);BoundaryFill (x+1, y-1, fill, boundary);

endif

Another stack-based approach is also possible instead of recursive approach using 4-connected method

Input: the seed pixel (x,y) the ‘fill’ color and the ‘boundary’ color

Push pixel(x, y) : Initialization of the stack with the seed pixel within the boundary-

defined region and not having fill color

While (stack not empty) Pop pixel(x, y) : get a pixel from the stackSetpixel(x, y, fill);

Page 42: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

If (Getpixel(x+1, y) < > fill and Getpixel(x+1, y) < > boundary ) thenPush pixel(x+1,y);If (Getpixel(x-1, y) < > fill and Getpixel(x-1, y) < > boundary ) thenPush pixel(x-1,y);If (Getpixel(x, y+1) < > fill and Getpixel(x, y+1) < > boundary ) thenPush pixel(x,y+1);If (Getpixel(x, y-1) < > fill and Getpixel(x, y-1) < > boundary ) thenPush pixel(x,y-1);

End while.

The above algorithm can be easily extended for 8-connected method by adding four more if statements for four diagonal pixels at (x+1, y+1), (x-1, y+1), (x+1, y-1), (x-1, y-1).

Algorithms used for filling interior-defined regions are generally known as Flood-Fill algorithms. The algorithm starts with a seed pixel by replacing its existing color with fill color. Then using the 4-connected or 8-connected method the algorithm sets the fill color to the other interior pixels. Assuming that all the pixels in an interior-defined region have same color, the flood fill algorithm terminates when no more connected pixel have the old color.

Scan line Seed Fill Algorithm

The seed fill algorithm makes the stack become quite larger because in every loop the algorithm

Page 43: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

pushes almost 4 to 8 pixels into the stack. The stack frequently contains duplicates pixels. A scan line seed fill algorithm minimizes stack size by pushing only one pixel into the stack in any uninterrupted unfilled span of pixels in a single scan line or row of pixels in a boundary defined region. Instead of following the 4-connect or 8-connect method the algorithm process the pixel in raster scan fashion i.e. from left to

Step1: A seed pixel on a scan line within the area is popped from a stack containing the seed pixel.Step2: The line or span containing the seed pixel is filled to the right and left of the seed pixel including the seed pixel itself until a boundary is found.

Step3: The extreme left and extreme right unprocessed pixel in the span is saved as x left and xright respectively.

Step4: The scan lines above and below the current scan line are examined in the range x left

to xright for any contiguous span of either boundary pixels or previously filled pixels. If any such span is found it is simply crossed over. The extreme right pixel in all the unfilled spans on these scan lines within the same range is marked as a seed pixel and pushed into the stack.

The pseudo code is as follows.

Page 44: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

Input: Pixel (x, y) as the seed pixel, fill as fill color and boundary as boundary color.

Push pixel(x, y) : Initialize stack with seed pixel

While (stack not empty)Pop pixel(x, y) : Get the seed pixelSetpixel (x, y, fill) : Set it to fill colorSave x= x : save the x and y coordinates of seed pixel Save y=y

x=x+1 While (Get pixel (x, y) < > boundary)Setpixel (x, y, fill) : fill the span to the right of the seed pixelx=x+1end while.xright = x-1 : save the extreme right pixel of the span in the

current scan linex= save xx=x-1

while (Getpixel (x, y) < > boundary)setpixel (x, y, fill) :fill the span to the left of the seed pixelx=x-1

end while.xleft= x+1 : save the extreme left pixel of the span in the

Page 45: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

current scan line

x=xleft, y=save y +1

while ( Getpixel(x, y)= boundary or Getpixel (x, y) =fill) : check the scan line immediately above the

current scan line for filled pixel span or boundary

filled span

x=x+1 : simply cross over such span if found

elsewhile (Getpixel(x, y)< > boundary and Getpixel(x, y)< > fill and x<= xright)x=x+1 : find the extreme right pixel of each unfilled span

and push it into the stack

end while.Push pixel (x-1, y)x=x+1end ifend whilex= xleft y= save y -1 : move to the scan line immediately below the

scan line containing the seed pixel

Page 46: Chapter 5 - aditipaulsite.files.wordpress.com€¦  · Web viewChapter 5 GRAPHIC PROCEDURE - CLIPPING AND FILLING Window and View port. Displaying an image of a picture involves

while (x<=xright)check the scan line below and stack pixels accordingly

end while

end while