Two-Dimensional Viewing: Virendra Singh Kushwah Department of Computer Science

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 86

Virendra Singh Kushwah

Department of Computer Science


Two-Dimensional Viewing
4/28/2012
Designed By Virendra Singh Kushwah
Viewing Pipeline
4/28/2012
Designed By Virendra Singh Kushwah
Two-Dimensional Viewing
Two dimensional viewing transformation
From world coordinate scene description to device
(screen) coordinates
4/28/2012
Designed By Virendra Singh Kushwah
Normalization and Viewport
Transformation
World coordinate clipping window
Normalization square: usually [-1,1]x[-1,1]
Device coordinate viewport
4/28/2012
Designed By Virendra Singh Kushwah
Virendra Singh
Kushwah
Clipping Lines
4/28/2012
Designed By Virendra Singh Kushwah
Clipping Lines
1
P
2
P
3
P
4
P
5
P
6
P
7
P
8
P
9
P
10
P
4/28/2012
Designed By Virendra Singh Kushwah
Clipping Lines
5
P
6

P
7
P
8
P
9

P
10

P
4/28/2012
Designed By Virendra Singh Kushwah
Clipping Lines
Given a line with end-points (x
0
, y
0
), (x
1
, y
1
)
and clipping window (x
min
, y
min
), (x
max
, y
max
),
determine if line should be drawn and clipped
end-points of line to draw.
4/28/2012
Designed By Virendra Singh Kushwah
Clipping Lines
1
P
2
P
3
P
4
P
5
P
6
P
7
P
8
P
9
P
10
P
4/28/2012
Designed By Virendra Singh Kushwah
Clipping Lines Simple Algorithm
If both end-points inside rectangle, draw line
If one end-point outside,
intersect line with all edges of rectangle
clip that point and repeat test

4/28/2012
Designed By Virendra Singh Kushwah
Clipping Lines Simple Algorithm
) , (
0 0
y x
) , (
min min
y x
) , (
max max
y x
) , (
1 1
y x
4/28/2012
Designed By Virendra Singh Kushwah
Clipping Lines Simple Algorithm
) , (
0 0
y x
) , (
min min
y x
) , (
max max
y x
) , (
1 1
y x
4/28/2012
Designed By Virendra Singh Kushwah
Clipping Lines Simple Algorithm
) , (
0 0
y x
) , (
min min
y x
) , (
max max
y x
) , (
1 1
y x
4/28/2012
Designed By Virendra Singh Kushwah
Clipping Lines Simple Algorithm
) , (
0 0
y x
) , (
min min
y x
) , (
max max
y x
) , (
1 1
y x
4/28/2012
Designed By Virendra Singh Kushwah
Intersecting Two Lines

) , (
0 0
y x
) , (
2 2
y x
) , (
1 1
y x
) , (
3 3
y x
4/28/2012
Designed By Virendra Singh Kushwah
Intersecting Two Lines

) , (
0 0
y x
) , (
2 2
y x
) , (
1 1
y x
t x x x t x ) ( ) (
0 1 0
+ =
) , (
3 3
y x
t y y y t y ) ( ) (
0 1 0
+ =
1 0 s st
4/28/2012
Designed By Virendra Singh Kushwah
Intersecting Two Lines

) , (
0 0
y x
) , (
2 2
y x
) , (
1 1
y x
t x x x t x ) ( ) (
0 1 0
+ =
) , (
3 3
y x
t y y y t y ) ( ) (
0 1 0
+ =
0
) 0 ( x x =
0
) 0 ( y y =
1
) 1 ( x x =
1
) 1 ( y y =
4/28/2012
Designed By Virendra Singh Kushwah
Intersecting Two Lines

) , (
0 0
y x
) , (
2 2
y x
) , (
1 1
y x
s x x x t x x x ) ( ) (
2 3 2 0 1 0
+ = +
) , (
3 3
y x
s y y y t y y y ) ( ) (
2 3 2 0 1 0
+ = +
4/28/2012
Designed By Virendra Singh Kushwah
Intersecting Two Lines

) , (
0 0
y x
) , (
2 2
y x
) , (
1 1
y x
|
|
.
|

\
|

=
|
|
.
|

\
|
|
|
.
|

\
|


0 2
0 2
3 2 0 1
3 2 0 1
y y
x x
s
t
y y y y
x x x x
) , (
3 3
y x
4/28/2012
Designed By Virendra Singh Kushwah
Intersecting Two Lines

) , (
0 0
y x
) , (
2 2
y x
) , (
1 1
y x
|
|
.
|

\
|

=
|
|
.
|

\
|
|
|
.
|

\
|


0 2
0 2
3 2 0 1
3 2 0 1
y y
x x
s
t
y y y y
x x x x
) , (
3 3
y x
Substitute t or s back into equation to
find intersection
4/28/2012
Designed By Virendra Singh Kushwah
Clipping Lines Simple Algorithm
) , (
0 0
y x
) , (
min min
y x
) , (
max max
y x
) , (
1 1
y x
4/28/2012
Designed By Virendra Singh Kushwah
Clipping Lines Simple Algorithm
) , (
0 0
y x
) , (
min min
y x
) , (
max max
y x
) , (
1 1
y x
4/28/2012
Designed By Virendra Singh Kushwah
Clipping Lines Simple Algorithm
) , (
0 0
y x
) , (
min min
y x
) , (
max max
y x
) , (
1 1
y x
4/28/2012
Designed By Virendra Singh Kushwah
Clipping Lines Simple Algorithm
) , (
0 0
y x
) , (
min min
y x
) , (
max max
y x
) , (
1 1
y x
4/28/2012
Designed By Virendra Singh Kushwah
Clipping Lines Simple Algorithm
) , (
0 0
y x
) , (
min min
y x
) , (
max max
y x
) , (
1 1
y x
4/28/2012
Designed By Virendra Singh Kushwah
Clipping Lines Simple Algorithm
Lots of intersection tests makes algorithm
expensive
Complicated tests to determine if intersecting
rectangle

Is there a better way?
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Algorithm
Classify p
0
, p
1
using region codes c
0
, c
1
If , trivially reject
If , trivially accept
Otherwise use codes to clip points and repeat
0
1 0
= .c c
0
1 0
= v c c
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Algorithm

0000 0001
0101
1001 1000 1010
0010
0110 0100
4/28/2012
Designed By Virendra Singh Kushwah
1000
0010
1010
0110 0100 0101
0001
1001
0000
left
right
below
above
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Algorithm

0000 0001
0101
1001 1000 1010
0010
0110 0100
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Algorithm

0000 0001
0101
1001 1000 1010
0010
0110 0100
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Algorithm

0000 0001
0101
1001 1000 1010
0010
0110 0100
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Algorithm

0000 0001
0101
1001 1000 1010
0010
0110 0100
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Algorithm

0000 0001
0101
1001 1000 1010
0010
0110 0100
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Algorithm

0000 0001
0101
1001 1000 1010
0010
0110 0100
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Algorithm

0000 0001
0101
1001 1000 1010
0010
0110 0100
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Algorithm

0000 0001
0101
1001 1000 1010
0010
0110 0100
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Algorithm

0000 0001
0101
1001 1000 1010
0010
0110 0100
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Algorithm

0000 0001
0101
1001 1000 1010
0010
0110 0100
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Algorithm

0000 0001
0101
1001 1000 1010
0010
0110 0100
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Algorithm

0000 0001
0101
1001 1000 1010
0010
0110 0100
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Algorithm

0000 0001
0101
1001 1000 1010
0010
0110 0100
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Algorithm

0000 0001
0101
1001 1000 1010
0010
0110 0100
4/28/2012
Designed By Virendra Singh Kushwah
Example1
a
b
c
d
e
f
g
h
i
j
k
l
1001
1000 1010
0001 0010
0101 0100 0110
T
B
L R
0000
4/28/2012
Designed By Virendra Singh Kushwah
Clipping example1
Line End Point
codes
Logical
Intersection
Comments
ab 0000 0000 0000 Totally
Visible
ij 1001 1000 1000 Totally
Invisible
cd 0000 0010 0000 Partially
visible
ef 0001 0000 0000 Partially
Visible
4/28/2012
Designed By Virendra Singh
Kushwah
Cohen-Sutherland Line Clipping
Example2
I
H
G
F
E
D
C
B
A
0000
1000
0010
1010
0110
0001
1001
0101 0100
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Line Clipping
Example2
I
H
G
F
E
D
C
B
A
0000
1000
0010
0110
0001
1001
0101 0100
1010
4/28/2012
Designed By Virendra Singh Kushwah
Initial Outcode Calculations
OC(D)=1000; OC(A)=0001


OC(E)=0100; OC(I)=1010
1000 0001 0000 = .
0000 1010 0100 = .
4/28/2012
Designed By Virendra Singh Kushwah
Clip and Continue
Clip against the top boundary

Calculate B. Keep AB
Calculate H . Keep EH

max
y y =
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Line Clipping
H
G
F
E
B
A
4/28/2012
Designed By Virendra Singh Kushwah
Clip and Continue
Now test and reject AB because
OC(A)=0001 and OC(B)=0001;

Reject AB on outcode basis

Clip against the bottom boundary
0 0001 0001 0001 = = .
min
y y =
4/28/2012
Designed By Virendra Singh Kushwah
Outcode Calculations
OC(H)=0010; OC(E)=0100

Since product is 0, process HE to get FH
0000 0100 0010 = .
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Line Clipping
H
G
F
0000
1000
0010
1010
0110
0001
1001
0101 0100
4/28/2012
Designed By Virendra Singh Kushwah
Outcode Calculations
OC(F)=0000; OC(H)=0010

Since product is 0, process HF to get GF
0000 0100 0010 = .
4/28/2012
Designed By Virendra Singh Kushwah
Clip and Continue
Clip against the right boundary


Get GF

Done
max
x
x
=
4/28/2012
Designed By Virendra Singh Kushwah
Cohen-Sutherland Line Clipping
G
F
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
Uses parametric form of line for clipping
Lines are oriented
Classify lines as moving inside to out or outside to
in
Dont find actual intersection points
Find parameter values on line to draw

Faster than Cohen-Sutherland
4/28/2012
Designed By Virendra Singh Kushwah
The LiangBarsky algorithm uses the parametric
equation of a line and inequalities describing the range of
the clipping box to determine the intersections between
the line and the clipping box. With these intersections it
knows which portion of the line should be drawn.

The idea of the Liang-Barsky clipping algorithm is to do
as much testing as possible before computing line
intersections. Consider first the usual parametric form of
a straight line:
x = x0 + u (x1 - x0) = x0 + u x
y = y0 + u (y1 - y0) = y0 + u y

A point is in the clip window if xmin x0 + u x xmax
ymin y0 + u y ymax
which can be expressed as the 4 inequalities
u pk qk k = 1, 2, 3, 4

4/28/2012
Designed By Virendra Singh Kushwah
where
p1 = -x q1 = x0 - xmin (left)
p2 = x q2 = xmax - x0 (right)
p3 = -y q3 = y0 - ymin (bottom)
p4 = y q4 = ymax - y0 (top)

4/28/2012
Designed By Virendra Singh Kushwah
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) (t p
) 0 ( p
) 1 ( p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) (t p
) 0 ( p
) 1 ( p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) (t p
) 0 ( p
) 1 ( p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) (t p
) 0 ( p
) 8 (. p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 0 ( p
) 1 ( p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 0 ( p
) 1 ( p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 0 ( p
) 1 ( p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 0 ( p
) 1 ( p
) 7 . ( p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 1 ( p
) 0 ( p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 1 ( p
) 2 (. p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 1 ( p
) 0 ( p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 1 ( p
) 0 ( p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 1 ( p
) 1 (. p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 1 ( p
) 1 (. p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 8 (. p
) 1 (. p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 8 (. p
) 1 (. p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 8 (. p
) 1 (. p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 1 ( p
) 0 ( p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 1 ( p
) 0 ( p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 1 ( p
) 0 ( p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 6 (. p
) 0 ( p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 6 (. p
) 0 ( p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 6 (. p
) 0 ( p
4/28/2012
Designed By Virendra Singh Kushwah
Liang-Barsky Algorithm
) 6 (. p
) 2 (. p
4/28/2012
Designed By Virendra Singh Kushwah
Line Clipping Considerations
Just clipping end-points does not produce the
correct results inside the window
Must also update sum in midpoint algorithm

Clipping against non-rectangular polygons is also
possible but seldom used
4/28/2012
Designed By Virendra Singh Kushwah
Thanking You
4/28/2012
Designed By Virendra Singh Kushwah

You might also like