CG-06-Viewing and Cliping (6)
CG-06-Viewing and Cliping (6)
Lecture 06
2. Clipping
• It is removing lines or portions of lines outside
an area
– Line
• Cohen– Algorithm
– Polygon
• Sutherland Hodgman - Algorithm
• Light Camera
Object
Viewing and clipping
• Example
Viewing and Clipping
• The viewing is the position of points
relative to the viewing
– (XWmin , YWmin)
– (XWmax, YWmax).
• We will use 4-bits to divide the entire
region. These 4 bits represent the
• Top,
• Bottom,
• Right, and
• Left of the region.
There are 3 possibilities for the line
• Line can be completely inside the window
This line will be completely inside the region.
• Line can be completely outside of the
window
This line will be completely removed from the
region.
• Line can be partially inside the window
We will find intersection points and draw only that
portion of line that is inside region.
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 regioncode.
• 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.
Visible line
Accept line
No clipping
Case 2
• P3→1000 non zero
• P4→1000 non zero
-------------------
And 1000 non zero
accept
Case 4
• P7→1000 nonzero
• P8→0100 nonzero
-------------------
And 0000 zero
Need clipping
Case 4
• P7’→0000 zero
• P8→0100 nonzero
-------------------
And 0000 zero
Need clipping
Case 4
• P7’→0000 zero
• P8’→0000 zero
-------------------
And 0000 zero
accept
Intersection point
• Calculate intersection point
• Line end points (x1,y1) and (x2,y2)
• Y’ of intersection
– Y’=y1+m(xboundary – x1)
– Xboundary is x window max or min
• x’ of intersection
– x’=x1+1/m(yboundary – y1)
– yboundary is y window max or min
Example
• using Cohen line clipping algorithm to find
intersection points with the window where
the Window’s coordinates are: (2,3) ,
(12,3) , (2,6) and (12,6). The Line
endpoints (1,8) and (7,1)
the Window’s coordinates are: (2,3) , (12,3) , (2,6) and (12,6).
The Line endpoints (1,8) and (7,1)
1,8
2,6 12,6
2,3
12,3
7,1
1001
1000
0000
0100
P1→1001
P2→ 0100
----------------------
And 0000 clipping required
P1’→ 0000
P2→ 0100
------------
And 0000 clipping required
P1’→ 0000
P2’→ 0000
------------
And 0000 accept
Intersection points
• y’ of intersection is y’=y1+m(xboundary – x1)
– Line
• Cohen– Algorithm
– Polygon
• Sutherland Hodgman Algorithm