Clipping Clipping Weve been assuming that all primitives
Clipping
Clipping We’ve been assuming that all primitives (lines, triangles, polygons) lie entirely within the viewport 2
Clipping Calculating the portions of primitives within the viewport is Clipping In line clipping, we will cut the portion of line which is outside of window and keep only the portion that is inside the window. 3
Clipping How can we quickly determine whether a line segment is entirely inside the viewport? Ans: By testing both endpoints. xmin xmax ymin 4
Clipping How can we know a line is outside viewport? Ans: if both endpoints on wrong side of same edge, then reject line xmin xmax ymin 5
Cohen-Sutherland Line Clipping Divide viewplane into Nine regions defined by viewport edges Assign each region a 4 -bit outcode: xmin 1001 xmax 1000 1010 0000 0010 0100 0110 ymax 0001 ymin 0101 6
Clipping We will use 4 -bits to divide the entire region. These 4 bits represent the Top, Bottom, Right, and Left of the region as shown in the following figure. Here, the TOP and LEFT bit is set to 1 because it is the TOP-LEFT corner. 7
Algorithm Step 1: Assign a region code for each endpoints. Step 2: If both endpoints have a region code 0000 then accept this line. Step 3: Else, perform the logical AND operation for both region codes. Step 3. 1: If the result is not 0000, then reject the line. Step 3. 2: Else you need clipping. Step 3. 2. 1: Choose an endpoint of the line that is outside the window. Step 3. 2. 2: Find the intersection point at the window boundary (base on region code). Step 3. 2. 3: Replace endpoint with the intersection point and update the region code. Step 3. 2. 4: Repeat step 2 until we find a clipped line either trivially accepted or trivially rejected. Step-4: Repeat step 1 for other lines. 8
9
10
11
12
Mid Point Subdivision Algorithm The Cohen-Sutherland algorithm requires the calculation of the intersection of the line with window edge. This direct calculation can be avoided by performing a binary search for the intersection by always dividing the line at its mid point.
Mid Point Subdivision Algorithm Lines which can not be immediately identified using there test are subdivided into 2 equal parts. The test are then applied to each half until the intersection with the window edge is found or the length of the divided segment becomes 0 The visibility of the point is then determined.
Mid Point Subdivision Algorithm P 1 Pm 2 Pm 1 Pm 2 P 2
Algorithm 1) Read two end points of line P 1 (x 1, y 1) and P 2 (x 2, y 2). 2) Read corners of window (Wx 1, Wy 1) and (Wx 2, Wy 2). 3) Assign region codes for P 1 and P 2. A region code is a 4 digit bit code which indicates one of nine regions having the end point of line. Initialize code with bits 0000 Set Bit 1 if (x<Wx 1) Set Bit 2 if (x>Wx 2) Set Bit 3 if (y<Wy 2) Set Bit 4 if (y>Wy 1) 16
Algorithm… 4) Check visibility of line P 1 P 2 a) If region codes for both end points P 1 and P 2 are 0 = > Line is completely visible. Draw Line and Go to Step 3. b) If region codes for both end points P 1 and P 2 are not 0 and logical ANDing of them is also not zero = > Line is completely invisible. Reject line and Go to Step 6. c) If a) and b) both are not satisfied, line is partially visible. 17
Algorithm… 5) Divide partially visible line segments to equal parts and repeat steps 3 through 5 for both sub divided line segments until you get completely visible and completely invisible line segments. 6) Stop. 18
Polygon Clipping 19
Sutherland-Hodgman Clipping Basic idea: ◦ Consider each edge of the viewport individually ◦ Clip the polygon against the edge equation ◦ After doing all planes, the polygon is fully clipped 20
Sutherland-Hodgman Clipping Basic idea: ◦ Consider each edge of the viewport individually ◦ Clip the polygon against the edge equation ◦ After doing all planes, the polygon is fully clipped 21
Sutherland-Hodgman Clipping Basic idea: ◦ Consider each edge of the viewport individually ◦ Clip the polygon against the edge equation ◦ After doing all planes, the polygon is fully clipped 22
Sutherland-Hodgman Clipping Basic idea: ◦ Consider each edge of the viewport individually ◦ Clip the polygon against the edge equation ◦ After doing all planes, the polygon is fully clipped 23
Sutherland-Hodgman Clipping Basic idea: ◦ Consider each edge of the viewport individually ◦ Clip the polygon against the edge equation ◦ After doing all planes, the polygon is fully clipped 24
Sutherland-Hodgman Clipping Basic idea: ◦ Consider each edge of the viewport individually ◦ Clip the polygon against the edge equation ◦ After doing all planes, the polygon is fully clipped 25
Sutherland-Hodgman Clipping Basic idea: ◦ Consider each edge of the viewport individually ◦ Clip the polygon against the edge equation ◦ After doing all planes, the polygon is fully clipped 26
Sutherland-Hodgman Clipping Basic idea: ◦ Consider each edge of the viewport individually ◦ Clip the polygon against the edge equation ◦ After doing all planes, the polygon is fully clipped 27
Sutherland-Hodgman Clipping Basic idea: ◦ Consider each edge of the viewport individually ◦ Clip the polygon against the edge equation ◦ After doing all planes, the polygon is fully clipped 28
A polygon can also be clipped by specifying the clipping window. Sutherland Hodgeman polygon clipping algorithm is used for polygon clipping. In this algorithm, all the vertices of the polygon are clipped against each edge of the clipping window. First the polygon is clipped against the left edge of the polygon window to get new vertices of the polygon. These new vertices are used to clip the polygon against right edge, top edge, bottom edge, of the clipping window as shown in the following figure. Figure: Polygon before Clipping 29
Figure: Clipping Left Edge Figure: Clipping Right Edge Figure: Clipping Top Edge Figure: Clipping Bottom Edge 30
Algorithm 1. start 2. Read co-ordinates of all vertices of polygon 3. Read points for clipping window 4. Process vertices against left edge of window to get output vertices set 5. Reapeat step 4 for right, top, and bottom edges of polygon respectively to get final output vertices 6. Display polygon connecting all vertices from output vertices list 7. Stop 31
- Slides: 31