Line Clipping

advertisement
Computer Graphics
Clipping
•
•
•
•
Cohen Sutherland Algorithm (Line)
Cyrus-Back Algorithm (Line)
Sutherland-Hodgeman Algorithm (Polygon)
Cohen Sutherland Algorithm (3d)
Clipping-When we want to study the picture
but not whole picture, study some part,
enlarge the part make study(X-ray) Selecting
and enlarging the part known-Windowing,
We select the portion of image and making
visible and invisible-Clipping(cutting of the
line which is outside View port)
Image in object space
Image in View space
Transformation is required
(Translate and scale) Translate the origin
and Scale the object and translate it back to
original
Point Clipping
clip
rectangle
y = ymax
(xmax, ymax)
x = xmin
(xmin , ymin )
x = xmax
y = ymin
For a point (x,y) to be inside the clip rectangle:
xmin  x  xmax
ymin  y  ymax
Point Clipping
clip
rectangle
y = ymax
(xmax, ymax)
(x1, y1)
x = xmin
(xmin , ymin )
x = xmax
y = ymin
For a point (x,y) to be inside the clip rectangle:
xmin  x  xmax
ymin  y  ymax
(Wx-Wxmin)/(Wxmax-Wxmin)
=(Vx-Vxmin)/(Vxmax-Vxmin)
(Wy-Wymin)/(Wymax-Wymin)
=(Vy-Vymin)/(Vymax-Vymin)
Viewing Transformation (N)
V=N * W
Where N=Tv * S * T-v
Aspect ratio
Aw=(Wxmax-Wxmin)/(Wymax-Wymin)
Av=(Vxmax-Vxmin)/(Vymax-Vymin)
Line Clipping
clip
rectangle
Cases for clipping lines
Line Clipping
B
B
A
A
clip
rectangle
Cases for clipping lines
Line Clipping
D
D'
D'
C
C
B
B
A
A
clip
rectangle
Cases for clipping lines
Line Clipping
F
D
D'
D'
C
C
B
B
A
A
E
clip
rectangle
Cases for clipping lines
Line Clipping
F
D
D'
D'
C
C
B
H
B
E
H'
A
G'
clip
rectangle
H'
A
G
Cases for clipping lines
G'
Line Clipping
F
D
D'
D'
C
C
B
H
B
E
H'
J
A
G'
G'
J'
clip
rectangle
H'
A
G
I'
I
Cases for clipping lines
Line Clipping-Midpoint subdivision
- Cohen Sutherland Algo
Midpoint- Check the line
If it is entirely inside
It is entirely outside (discard)
It is partially inside and partially outside
then Find mid point of two endpoints
then divide line into two line segement
and check both segments and continue
Algo for Midpoint
1- Check line
Completely outside the window
• Completely inside the window
• Partially inside window (Line intersect
boundary, divide it from mid point)
2-check again for both segemnts
3- Repeat step-1 until whole part of line inside
the window
Line Clipping
Clipping Lines by Solving Simultaneous Equations
(x1, y1)
(x, y)
(x1, y1)
(xb, yb)
(xa, ya)
(xb, yb)
(x0, y0)
(x, y)
(xa, ya)
(x0, y0)
(xc, yc)
(xd, yd)
clip
rectangle
x  x0  tline  x1  x0 ,
x  xa  tedge  xb  xa ,
(xc, yc)
(xd, yd)
clip
rectangle
y  y0  tline  y1  y0 
y  ya  tedge  yb  ya 
Cohen-Sutherland Algorithm
The Cohen-Sutherland Line-Clipping
Algorithm performs initial tests on a line to
determine whether intersection calculations
can be avoided.
1.
2.
3.
First, end-point pairs are checked for Trivial
Acceptance.
If the line cannot be trivially accepted, region
checks are done for Trivial Rejection.
If the line segment can be neither trivially
accepted or rejected, it is divided into two
segments at a clip edge, so that one segment
can be trivially rejected.
These three steps are performed iteratively
until what remains can be trivially accepted or
rejected.
Cohen-Sutherland Algorithm
1001
bit 0 : y  ymax
bit 1 : y  ymin
1000
1010
0001
0000
0010
0101
0100
0110
clip
rectangle
Region outcodes
bit 2 : x  xmax
bit 3 : x  xmin
Cohen-Sutherland Algorithm
1. A line segment can be trivially
accepted if the outcodes of both the
endpoints are zero.
2. A line segment can be trivially
rejected if the logical AND of the
outcodes of the endpoints is not zero.
3. A key property of the outcode is that
bits that are set in nonzero outcode
correspond to edges crossed.
Algo
1-Compute the codes for the Two end points
2-Enter in loop, check if both outcodes are
zero(0000 and 0000) put line in display file
for display and exit loop, return
3-if both outcodes are not zero then take
logical AND of both codes, check result for
nonzero if so reject line exit loop, return
4-if result is zero then subdivide the line
from intersection point of line and clipping
boundary and repeat step 2,3,4
Cohen-Sutherland Algorithm
E
D
clip
rectangle
C
B
A
An Example
Cohen-Sutherland Algorithm
E
D
clip
rectangle
C
B
An Example
Cohen-Sutherland Algorithm
D
clip
rectangle
C
B
An Example
Cohen-Sutherland Algorithm
clip
rectangle
C
B
An Example
Polygon Clipping
Example
Polygon Clipping
Example
Polygon Clipping
Example
Sutherland-Hodgeman Algo.
The Clipped Polygon
Clipping of the polygon
-Clip Left --Clip right --Clip bottom --Clip top --- View transformation
Consider Any Edge (P1P2)
1.If both p1,p2 are left to edge, p2 is placed
vertex output list of clipped poly
2. If p1,p2 are right to edge discard
3. If p1,p2 cross edge and find intersection
point (I)
if p1 is left to edge then I will be in
output list of vertex of clipped poly
if p1 is right then both p1 and I in
output list
Determination of point if is left right to line
AB is line segement(x1,y1 and x2,y2) determine point
p (x,y)is left or right to AB
Find cross product of vectors AB and AP
then resulted vector has direction vector k then point
p is left to AB
If –k the point p is right to AB
AP=(x-x1) *I +(y-y1) * J
AB=(x2-x1) *I +(y2-y1) * J
AB X AP=[(x2-x1) *(y-y1)-(y2-y1)*(y-y1)]*k
d=(x2-x1) *(y-y1)-(y2-y1)*(y-y1)
If d=+ then p is left to AB
If d=-Ve then p is right to AB
4 Cases of Polygon Clipping
Inside
p:second output
s
Outside
i:first output
p
s
Polygon
being
clipped
p
s
s
p:output
(no output)
i:output
Clip
boundary
Case 4
2
1
3
Algorithm
Input vertex P
No
Close Polygon entry
First Point
Does SF
intersect E?
Yes
Yes
F=P
Does SP intersect
E?
Compute
Intersection I
No
Output
vertex I
Yes
Compute
Intersection Point
I
Exit
Output
vertex I
S=P
Yes
Is S on left
side of E?
Output
vertex S
NO
Exit
No
3D Clipping
• Both the Cohen-Sutherland and Cyrus-Beck clipping algorithm
readily extend to 3D.
• For Cohen-Sutherland algorithm use two extra-bit in outcode for
incorporating z < zmin and z > zmax regions
Thank You
Download