Professional Documents
Culture Documents
\
|
|
|
|
.
|
\
|
=
|
|
|
.
|
\
|
= x x
31
Direct interpretation
( )
|
|
|
.
|
\
|
|
|
|
.
|
\
|
=
|
|
|
.
|
\
|
=
=
1 1 0 0 1
'
'
or , '
or , M '
y
x
t b a
t b a
y
x
y y y
x x x
x t b a x
x x
x
y
a
b
t
(x,y)
32
Translation matrix
|
|
|
.
|
\
|
=
=
1 0 0
1 0
0 1
) , ( T
with , ) , ( T '
: n Translatio
y
x
y x
y x
t
t
t t
t t x x
33
Scaling matrix
|
|
|
.
|
\
|
=
=
1 0 0
0 0
0 0
) , ( S
with , ) , ( S '
: Scaling
y
x
y x
y x
s
s
s s
s s x x
34
Rotation matrix
|
|
|
.
|
\
|
=
=
1 0 0
0 cos sin
0 sin cos
) R(
with , ) R( '
: Rotation
o o
o o
o
o x x
35
Sequences of transformations
) 2 / 2)R( T(5,4)S(1/ M
with , M ' ' '
or
' ' T(5,4) ' ' '
' S(1/2) ' '
) 2 / ( R '
t
t
=
=
=
=
=
x x
x x
x x
x x
x
y
x
y
x
y
y
x
Sequences of transformations can be described with a single
transformation matrix, which is the result of concatenation of
all transformations.
36
Order of transformations
x
y
x
y
x
y
x x ) T(2,3)R(30 ' ' =
x x ) R(30)T(2,3 ' ' =
Matrix multiplication is not commutative. Different
orders of multiplication give different results.
37
Order of transformations
Pre-multiplication:
x = M
n
M
n-1
M
2
M
1
x
Transformation M
n
in global coordinates
Post-multiplication:
x = M
1
M
2
M
n-1
M
n
x
Transformation M
n
in local coordinates, i.e., the
coordinate system that results from application of
M
1
M
2
M
n-1
38
Window and viewport
Viewport:
Area on screen to be used for drawing.
Unit: pixels (screen coordinates)
Note: y-axis often points down
Window:
Virtual area to be used by application
Unit: km, mm, (world coordinates)
(0,0)
(800,600)
(200,200)
(600,400)
(-2,-1)
(2,1)
39
Window/viewport transform
Determine a matrix M, such that the
window (w
x1
, w
x2
, w
y1
, w
y2
) is mapped on
the viewport (v
x1
, v
x2
, v
y1
, v
y2
):
A = T(-w
x1
, -w
y1
)
B = S(1/(w
x2
-w
x1
), 1/(w
y2
-w
y1
)) A
C = S(v
x2
-v
x1
,v
y2
-v
y1
)B
M = T(v
x1
, v
y1
) C
40
Forward and backward
Drawing: (meters to pixels)
Use x = Mx
Picking:(pixels to meters)
Use x = M
-1
x
(w
x1
, w
y1
)
(w
x2
, w
y2
)
Window:
x: user coordinates
(v
x1
, v
y1
)
(v
x2
, v
y2
)
Viewport
x:screen coordinates
Drawing Picking
41
Implementation example
Suppose, basic library supports two
functions:
MoveTo(x, y: integer);
LineTo(x, y: integer);
x and y in pixels.
How to make life easier?
42
State variables
Define state variables:
Viewport: array[1..2, 1..2] of integer;
Window: array:[1..2, 1..2] of real;
Mwv, Mobject: array[1..3, 1..3] of real;
Mwv: transformation from world to view
Mobject: extra object transformation
43
Procedures
Define coordinate system:
SetViewPort(x1, x2, y1, y2):
Update Viewport and Mwv
SetWindow(x1, x2, y1, y2):
Update Window and Mwv
44
Procedures (continued)
Define object transformation:
ResetTrans:
Mobject := IdentityMatrix
Translate(tx, ty):
Mobject := T(tx,ty)* Mobject
Rotate(alpha):
Mobject := R(tx,ty)* Mobject
Scale(sx, sy):
Mobject := S(sx, sy)* Mobject
45
Procedures (continued)
Handling hierarchical models:
PushMatrix();
Push an object transformation on a stack;
PopMatrix()
Pop an object transformation from the stack.
Or:
GetMatrix(M);
SetMatrix(M);
46
Procedures (continued)
Drawing procedures:
MyMoveTo(x, y):
(x, y) = Mwv*Mobject*(x,y);
MoveTo(x, y)
MyLineTo(x,y):
(x, y) = Mwv*Mobject*(x,y);
LineTo(x, y)
47
Application
DrawUnitSquare:
MyMoveTo(0, 0);
MyLineTo(1, 0);
MyLineTo(1, 1);
MyLineTo(0, 1);
MyLineTo(0, 0);
Initialize:
SetViewPort(0, 100, 0, 100);
SetWindow(0, 1, 0, 1);
Main program:
Initialize;
Translate(-0.5, -0.5);
for i := 1 to 10 do
begin
Rotate(pi/20);
Scale(0.9, 0.9);
DrawUnitSquare;
end;
48
Puzzles
Modify the window/viewport transform for a
display y-axis pointing downwards.
How to maintain aspect-ratio world->view?
Which state variables?
Define a transformation that transforms a unit
square into a wybertje, centred around the origin
with width w and height h.
49
Geometry
Dot product, determinant
Representations
Line
Ellipse
Polygon
50
Good and bad
Good: symmetric in x and y
Good: matrices, vectors
Bad: y = f(x)
Good: dot product, determinant
Bad: arcsin, arccos
51
Dot product
v v
w v
w v w v
w v
w v
) s s =
+ =
\
|
+
|
.
|
\
|
=
b
y
a
x
b a y x o o
x
y
a
b
61
Generic ellipse
( )
1
with , 1 | |
: Implicit
sin cos ) (
: Parametric
= =
+ + =
c b a x
b a c x
M M
o o o
x
y
62
Some standard puzzles
Conversion of line representation
Projection of point on line
Line/Line intersection
Position points/line
Line/Circle intersection
63
Conversion line representations
a n
a
n
u n
n
x n
u a p
=
=
= +
+ =
c
u u
c
s s
x y
: hence line, on the be must
) , (
: set we hence , on be must
. normal determine First,
. 0
: tion representa implicit Find
; ) (
: line Given
a
u
s
64
Projection point on line
u
u u
u a q
a q
u
u u
u a q
a q
u a q u a q
u
u
a q a q
u a p q
+ =
+ =
=
+ =
+ =
) (
'
or ,
| | | |
) (
'
: cos | | | | ) ( Use
| |
| | cos '
: ) ( line on point Project
u
u
s s
a
u
s
q
q-a
q
u
u
u
u
a q
along r unit vecto :
| |
length |: | cos w u
w
65
Intersection of line segments
a
u
b
v
t
s
). ( is on intersecti so, If
1; 0 and 1 0 if Check
sheet); (next and for Solve
) ( ) (
: on intersecti At
. 1 0 , ) (
and 1 s 0 , ) (
: segments line of on intersecti Find
s
t s
t s
t s
t t t
s s
p
q p
v b q
u a p
s s s s
=
s s + =
s s + =
66
Solving for s and t
a
u
b
v
t
s
( )
( )
|
|
.
|
\
|
|
|
.
|
\
|
=
|
|
.
|
\
|
=
|
|
.
|
\
|
=
|
|
.
|
\
|
+ = +
=
y y
x x
x y
x y
x y y x
a b
a b
u u
v v
v u v u t
s
t
s
t
s
t s
t s
1
or , ) (
or ,
or ,
or ), ( ) (
1
a b v u
a b v u
v b u a
q p
67
Position points/line
sign. same the
have ) , ( and ) , (
if side same on the are Points
: sin | | | | ) , ( Use
) ( line of side same
on the are and points if Check
c b u c a u
v u v u
u c p
b a
=
+ =
Det Det
Det
s s
u
c
u
b
s a
68
Line/circle intersection
solutions. 2 or 1, 0,
: for equation quadratic Solve
. 0
or , ) ( ) (
or , ) ( ) (
: on intersecti At
. : circle
and 1 t 0 , ) ( : line
: of ons intersecti Find
2 2
2
2
2
t
r t t
r t t
r t t
r
t t
= + +
= + +
=
=
s s + =
a a u a u u
u a u a
p p
x x
u a p
a
u
t
x
y
r
69
Polygons
Sequence of points p
i
, i = 1,, N,
connected by straight lines
Index arithmetic:
modulo N
p
0
= p
N
, p
N+1
= p
1
, etc.
p
1
p
2
p
N
p
i
70
Regular N-gon
2 / / ) 2 / 1 ( 2
) sin , cos (
t t o
o o
+ =
=
N i
r r
i
i i i
p
triangle square pentagon hexagon octagon
71
Convex and concave
Convex:
each line between two
arbitrary points inside
the polygon does not
cross its boundary
Concave:
not convex
72
Convexity test
i all for 0 ) , Det(
if concave, is Polygon
ckwise. counterclo oriented is polygon Assume
i 1 i 1 - i i
>
+
p p p p
p
i-1
p
i
p
i+1
p
i-1
p
i
p
i+1
Convex Concave
73
Polygon area and orientation
p
i
p
i+1
c
n orientatio clockwise : 0
n orientatio ckwise counterclo : 0
| |
point is , 2 / ) , (
i
1 i i
<
>
=
=
+
a
a
a area
arbitrary Det a
N
c c p c p
74
Point/polygon test
outside. is it else inside,
is point : odd is If polygon. with the
L of crossings of number the be Let
0). (1, f.i. y, arbitraril chosen be can
. 0 , L line a Define
: Solution
outside. or inside is point a if Test polygon. a Given
n
n
t t
v
v c
c
> + =
2
3
1
75
Point/polygon test (cntd.)
Beware of special cases:
Point at boundary
v parallel to edge
c + vt through vertex
76
Puzzles
Define a procedure to clip a line segment against a
rectangle.
Define a procedure to calculate the intersection of
two polygons.
Define a procedure to draw a star.
Same, with the constraint that the edges p
i-1
p
i
and
p
i+2
p
i+3
are parallel.