36
© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 1 5. 2D Geometry In order to design and render complex scenes we require techniques for transforming points and vectors. Points are used to represent OpenGL primitives (glVertex) and vectors are used to represent surface normals (necessary for computing the illumination at a point). 5.1 Points and Vectors 5.2 Applications of the Scalar Product (Dot Product) 5.3 Convex and Concave Objects 5.4 Implicit Curves 5.5 Parametric Curves 5.6 2D Affine Transformations 5.7 2D Homogeneous Coordinates 5.8 Notes & Examples

5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 1

5. 2D GeometryIn order to design and render complex scenes we require techniques for transforming points and vectors. Points are used to represent OpenGL primitives (glVertex) and vectors are used to represent surface normals (necessary for computing the illumination at a point).

5.1 Points and Vectors5.2 Applications of the Scalar Product (Dot Product)5.3 Convex and Concave Objects5.4 Implicit Curves5.5 Parametric Curves5.6 2D Affine Transformations5.7 2D Homogeneous Coordinates5.8 Notes & Examples

Page 2: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 2

5.1. Points and VectorsA point is a position in space, e.g. AucklandA vector represents a displacement – a difference between two points. The only way to represent a point is with reference to the origin of a coordinate system. The vector from the origin of the coordinate system to the point is the position vector of the point.

Example: Describe where Hamilton is!

120km to the south-south-west of Auckland37.43S Latitude, 175.19E Longitude

Page 3: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 3

Points and Vectors (cont’d)Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system.We denote the components of a vector v in 2D with v1and v2 and of a vector u in 3D with u1, u2 and u3:We denote vectors with small bold letters and points with capital letters, e.g. p is the position vector of the point P.

3

2

1

2

1

⎟⎟⎟

⎜⎜⎜

⎛=⎟⎟

⎞⎜⎜⎝

⎛=

uuu

vv

uv

Page 4: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 4

Points and Vectors (cont’d)

Operations on vectorsCan add, subtract and scale vectors

Operations on pointsSubtracting one point from another gives a vector (the displacement between these points)Can NOT add two points (what is Auckland + Hamilton??)But we can add and subtract their position vectors w.r.t. some origin.

a

b

a+b

O

P

Q

P-Q=p-q

O

P

Q

p+q

Page 5: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 5

Basic Operations on VectorsAddition

Represents the combined displacementImplement by adding components

Scalingi.e. multiplication by a scalarDefined such that v + v = 2vImplement by multiplying all components by the scalar.

SubtractionAddition of a negated vector, i.e. one in opposite direction.Implement by subtracting components.

The magnitude of a vectori.e. its "length" (2-norm).

NormalisationThe process of creating a unit vector (length 1)Scale by reciprocal of magnitude:

1 1 1 1

2 2 2 2

u v u vu v u v

+⎛ ⎞ ⎛ ⎞ ⎛ ⎞+ = + =⎜ ⎟ ⎜ ⎟ ⎜ ⎟+⎝ ⎠ ⎝ ⎠ ⎝ ⎠

u v

1 1

2 2

u s u

s su s u⎛ ⎞ ⎛ ⎞

= =⎜ ⎟ ⎜ ⎟⎝ ⎠ ⎝ ⎠

u

1 1 1 1

2 2 2 2

u v u vu v u v

−⎛ ⎞ ⎛ ⎞ ⎛ ⎞− = − =⎜ ⎟ ⎜ ⎟ ⎜ ⎟−⎝ ⎠ ⎝ ⎠ ⎝ ⎠

u v

2 21 2 , u u s s= + =u u u

ˆ = uuu

Page 6: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 6

Basic Operations on MatricesDimension of a matrix

A m×n matrix is a matrix has m rows and n columns.

Addition/SubtractionImplement by adding/subtracting components.

ScalingImplement by multiplying all components by the scalar.

11 12 11 12 11 11 12 12

21 22 21 22 21 21 22 22

m m n n m n m nm m n n m n m n

± ±⎛ ⎞ ⎛ ⎞ ⎛ ⎞± = ± =⎜ ⎟ ⎜ ⎟ ⎜ ⎟± ±⎝ ⎠ ⎝ ⎠ ⎝ ⎠

M N

11 12 13

21 22 23

r r rr r r⎛ ⎞

= ⎜ ⎟⎝ ⎠

R

Example of a 2 ×3 matrix

11 12

21 22

s m s ms

s m s m⎛ ⎞

= ⎜ ⎟⎝ ⎠

M

row

column

Page 7: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 7

Basic Operations on Matrices (cont’d)

Transpose (indicated by T) of a matrix MSwap mij and mji for all i,j.

Algebraic rules for transposition:

(MT)T=M(sM)T=s(MT)(M+N)T=MT+NT

(MN)T=NTMT

11 2111 12 13

12 2221 22 23

13 23

T

m mm m m

m mm m m

m m

⎛ ⎞⎛ ⎞ ⎜ ⎟= ⇒ =⎜ ⎟ ⎜ ⎟⎝ ⎠ ⎜ ⎟

⎝ ⎠

M M

Page 8: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 8

Basic Operations on Matrices (cont’d)

Determinant det M (also written | M |) of a matrix MFor a 2x2 matrix:

Inverse M-1 of a matrix MFor a 2x2 matrix:

211222112221

1211 mmmmmmmm

−=

111 12 22 12

21 22 21 1111 22 12 21

1m m m mm m m mm m m m

− −⎛ ⎞ ⎛ ⎞=⎜ ⎟ ⎜ ⎟−−⎝ ⎠ ⎝ ⎠

Exercise: Prove that M-1 is the inverse of M, i.e. show M-1M=MM-1=I

Page 9: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 9

Basic Operations on Vectors and MatricesThe transpose of a vector

Transpose of a row vector is a column vector and vice versa

The dot product (scalar product)Symmetry: a•b=b•aLinearity: (a+b)•c=a•c+b•cHomogeneity: (sa)•b=s(a•b)|b|2= b•b

Matrix multiplicationMultiplying an l× m and m × n matrix gives an l × n matrix with

the elements [Note: aij= rowi•columnj]

( )11 2

2

Tuu u

u⎛ ⎞

= ⇒ =⎜ ⎟⎝ ⎠

u u

1 11 1 2 2

2 2

Tu vu v u v

u v⎛ ⎞ ⎛ ⎞

= = + =⎜ ⎟ ⎜ ⎟⎝ ⎠ ⎝ ⎠

u v u v• •

11 12 11 12 11 11 12 21 11 12 12 22

21 22 21 22 21 11 22 21 21 12 22 22

b b c c b c b c b c b cb b c c b c b c b c b c

+ +⎛ ⎞⎛ ⎞ ⎛ ⎞= = =⎜ ⎟⎜ ⎟ ⎜ ⎟+ +⎝ ⎠⎝ ⎠ ⎝ ⎠

A B C

1 11

...m

ij i j im mj ik kjk

a b c b c b c=

= + + =∑

Page 10: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 10

5.2 Applications of the Scalar Product (Dot Product)

Angle between two vectorsProjection of a vectorDistance of a point to a lineReflectionsArea of a triangle

Page 11: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 11

The Angle between Two VectorsThe most important application of the dot product is to find the angle between two vectors (or two intersecting lines).

Two non-zero vectors b and c with common start point areless than 90o apart if b•c>0exactly 90o apart if b•c=0 [b and c are orthogonal (perpendicular)]more than 90o apart if b•c<0

cφ bφ

φ b

c

x

y

( )ϕ

φφφφφφ

φφ

φφ

cos

cos

sinsincoscoshence

sincos

,sincos

cb

cb

cbcb

cc

bb

bc

cbcb

c

c

b

b

=

−=

+=•

⎟⎟⎠

⎞⎜⎜⎝

⎛=⎟⎟

⎞⎜⎜⎝

⎛=

cb

cb

Page 12: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 12

Projection of a VectorIn many applications we must compute the projection of a vector onto another vector and the distance of a point from a line:

Let L be a line through A in the direction of a.Let b be the vector from A to a point B.

We want to find ba the orthogonal projection of b onto a.

ab ˆ

a

b

abL

B

A orthogonal projection of b onto a

( )

aaaabb

aaab

aaabaaabaabbabbb

a

aa

aaa

••=⇒

••=⇒

•=•+•=•+=•+=+=

)( somefor

ˆˆ

ˆˆ

k

kkkkk Note: at this point we don’t know

the value for but we know it is perpendicular to a.

aa bbb −=ˆ

Page 13: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 13

Distance of a Point to a Line

In the previous slide we computed

hence the distance of B to the line L is

ab ˆ

a

b

abL

B

A

aaaabba •

•=

aaaabbbbb aa •

•−=−=ˆ

Page 14: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 14

ReflectionsRay tracing is a popular rendering algorithm which displays a scene by tracing rays from the eye through each pixel of the screen into the scene (i.e. trace a light ray hitting the eye backwards → see 2nd part of this course!). If the scene contains reflective objects such as mirrors it is necessary to compute for a ray with direction a its reflection r. Let n be the surface normal at the point where the ray hits the object:

φ

na

nr

φ

a r

m −m

e e

( )

( )

2 ˆ ˆ

( ) 2ˆ ˆ 2

= = =

⇒ = − = − − = −= −

a n a nm n n a n nn n n

r e m a m m a ma a n n

• • ••

•reflective object

Page 15: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 15

Orthogonal Vectors in 2D

Let a=(a1 , a2)T then in 2D we can find two vectors perpendicular to it

a⊥=(-a2 , a1)T (note a⊥• a=0)

-a⊥=(a2 , -a1)T (note -a⊥• a=0)

a

x

y

⊥a90o

⊥a-

In the textbook a⊥ is called the “Perp” vector

Page 16: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 16

The Area of a Triangle (in 2D)The area of a parallelogram:

Area of a triangle:Area is half the area of the parallelogram formed by two of its edges

x

y

ˆ ⊥a

Ah

a

b

u

v12

A ⊥= u v•

NOTE: These formulas are only valid in 2D!!

baa

baaa

aba

aaabab

a

a

•=•

==

•=

••==

=

⊥⊥

⊥⊥

⊥⊥

hA

h

hA

Page 17: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 17

A Convex Polygon is a polygon where any line connecting any pair of vertices lies entirely within the polygon (this is equivalent with: all interior angles between neighbouring edges are smaller or equal to 180 degree). If a polygon is not convex it is called concave.

The Convex Hull of a set of points is the smallest convex set containing the points. [i.e. smallest convex polygon containing the points]

convex not convex>180o

Convex Hull

5.3 Convex and Concave Objects

Page 18: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 18

5.4 Implicit CurvesImplicit curves

A 2D curve can be defined as the set of points p=(x,y)T

fulfilling the mathematical equation f(x,y)=0.

Example: x2+y2-1=0

defines a unit circle centred at the origin

Disadvantages:Modelling is non-intuitive (e.g. how to draw a penguin?)Difficult to draw: have to find a set of points fulfilling the equation (hard!) and connect them by line segments.

Advantages:Easy to compute normal n at a point (x0,y0)T:

x

y

1

1

0( , )y

T

x y

f fx y

⎛ ⎞∂ ∂= ⎜ ⎟∂ ∂⎝ ⎠n

Page 19: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 19

Parametric curvesA 2D curve defined by a set of points p(t)=(x(t), y(t))T where x(t) and y(t) are functions of the parameter t (often called the “speed”).Have to specify the parameter interval [tmin, tmax] for t. It’s a good idea to specify curve such that [tmin, tmax]=[0,1].

Example:

defines a unit circle centred at the origin.

Disadvantages:Modelling is still non-intuitive (e.g. how to draw a penguin?)

AdvantagesCan compute tangent at a point by the derivative of the componentsEasy to “splice” curve segments together.Can draw curve by computing points on the curve and connecting them by line segments.

x

y1

1

cos 2( ) , [0,1]

sin 2t

p t tt

ππ

⎛ ⎞= ∈⎜ ⎟⎝ ⎠

'( )Tdx dyt

dt dt⎛ ⎞= ⎜ ⎟⎝ ⎠

p

5.5 Parametric Curves

Page 20: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 20

Parametric Curves (cont’d)Examples for parametric curves (in all cases t∈[0,1] ):

Curve with centre c and radius r:

Ellipsoid with centre c, long axis a and short axis b:

Logarithmic spiral with centre c and n revolutions:

1

2

cos(2 )( )

sin (2 )c r t

p tc r t

ππ

+⎛ ⎞= ⎜ ⎟+⎝ ⎠

rc

1

2

cos(2 )( )

sin(2 )c a t

p tc b t

ππ

+⎛ ⎞= ⎜ ⎟+⎝ ⎠

acb

1

2

( ) cos( )( ) where ( ) , 2

( )sin( )ac f

p t f Ke ntc f

θθ θθ θ π

θ θ+⎛ ⎞

= = =⎜ ⎟+⎝ ⎠

K=10, a=0.1, n=4

Page 21: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 21

How to draw a parametric curve?Compute (n+1) points on the curve

Connect the points with line segments

for 0,...,ii i nn

⎛ ⎞= =⎜ ⎟⎝ ⎠

v p0v

1v2v

3v

Parametric Curves (cont’d)

Page 22: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 22

void CParametricCurve::init(){ // compute (n+1) points on the curvefor(int i=0;i<=n;i++)

computePointOnCurve((float) i/(float) n,vertices[i][0],vertices[i][1]);}

void CParametricCurve::draw(){ // draw line segmentsglBegin(GL_LINE_STRIP);

for(int i=0;i<=n;i++) glVertex2fv(vertices[i]);glEnd();}

void CParametricCircle::computePointOnCurve(float t, float& x, float& y){x=cx+r*cos(t*2.0*PI);y=cy+r*sin(t*2.0*PI);}

Parametric Curves (cont’d)

Page 23: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 23

5.6 2D Affine Transformations

Examples of affine transformations:

Scaling about OriginFor any point p= (p1, p2)T, scale p1 by factor s1, p2 by factor s2.

Translation ("movement")Add a vector t to all points in the scene, i.e. q = p + t

Affine Transformations transform a pair of parallel straight lines to another pair of parallel straight lines and preserve ratios of distances. Assume for now that the transformations apply only to points but with an origin and an underlying vector space defined.

⎟⎟⎠

⎞⎜⎜⎝

⎛=

=

⎟⎟⎠

⎞⎜⎜⎝

⎛⎟⎟⎠

⎞⎜⎜⎝

⎛=⎟⎟

⎞⎜⎜⎝

2

1

2

1

2

1

2

1

00

where

i.e.0

0

ss

pp

ss

qq

scale

scale

M

pMq

Page 24: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 24

Note that "scaling" includes "reflection" if s1 and/or s2 is negative:

Reflection at the y-axis:

Reflection at the x-axis

Reflection at the origin

2D affine transformations (cont’d)

1 00 1−⎛ ⎞

= ⎜ ⎟⎝ ⎠

q p1

2

p

p

⎛ ⎞⎜ ⎟= ⎜ ⎟⎝ ⎠

p

x

y1

2

p

p

⎛ ⎞−⎜ ⎟=⎜ ⎟⎝ ⎠

q

1 00 1⎛ ⎞

= ⎜ ⎟−⎝ ⎠q p

1

2

p

p⎛ ⎞⎜ ⎟=⎜ ⎟⎝ ⎠

p

x

y

1

2

p

p

⎛ ⎞⎜ ⎟=⎜ ⎟−⎝ ⎠

q

1

2

p

p⎛ ⎞⎜ ⎟=⎜ ⎟⎝ ⎠

p

x

y

1

2

p

p

⎛ ⎞−⎜ ⎟=⎜ ⎟−⎝ ⎠q

1 00 1−⎛ ⎞

= ⎜ ⎟−⎝ ⎠q p

Page 25: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 25

Rotation about origin by an angle θ (right-handed i.e. anticlockwise)Proof: UDOO

Shearing

2D affine transformations (cont’d)

cos sinwhere

sin cos

rotate

rotate

θ θθ θ

=−⎛ ⎞

= ⎜ ⎟⎝ ⎠

q M p

M

1where

1

shear

shear

xShearyShear

=

⎛ ⎞= ⎜ ⎟⎝ ⎠

q M p

M

x

y

x

y

x

y

x

y

cos45 sin 45

sin 45 cos45

⎛ ⎞−⎜ ⎟⎜ ⎟⎝ ⎠

o o

o o

1 20 1⎛ ⎞⎜ ⎟⎝ ⎠

45o

Page 26: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 26

Some properties of affine transformations (both 2D & 3D)Straight lines are preservedParallel lines remain parallelProportional distances are preservedAny closed area in 2D or volume in 3D is multiplied by | det M | (unchanged by translation)Any arbitrary affine transformation can be represented as a sequence of shearing, scaling, rotation and translationAffine transformations do not in general commute (i.e. T1T2≠ T2T1)Transformations are associative, i.e. T1(T2T3) = (T1T2) T3

Page 27: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 27

5.7 2D Homogeneous Coordinates

Translation is a nuisance - don't have a matrix representation for it.So we introduce homogeneous coordinates as a way of "unifying" the representation of translation with the other transformations.

The ideaGeometric interpretationConverting from HC to ordinary coordinatesComposition of transformations

Page 28: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 28

The ideaRepresent the ordinary 2D point (x, y)T as a homogeneous coordinate point (x, y, 1)T.Then can do translation by:

and the other transformations by

1 00 1

1 0 0 1 1

x x x

y y y

q t pq t p⎛ ⎞ ⎛ ⎞⎛ ⎞⎜ ⎟ ⎜ ⎟⎜ ⎟=⎜ ⎟ ⎜ ⎟⎜ ⎟⎜ ⎟ ⎜ ⎟⎜ ⎟⎝ ⎠ ⎝ ⎠⎝ ⎠

00

1 0 0 1 1

x x

y y

q a b pq c d p⎛ ⎞ ⎛ ⎞⎛ ⎞⎜ ⎟ ⎜ ⎟⎜ ⎟=⎜ ⎟ ⎜ ⎟⎜ ⎟⎜ ⎟ ⎜ ⎟⎜ ⎟⎝ ⎠ ⎝ ⎠⎝ ⎠

Page 29: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 29

Geometric interpretation

Can see that the translation in 2D is implemented as a shear in 3D.

x

y

z

1

plane =1

PQ

t

Qactual

Pactual

Page 30: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 30

Converting from HC to Ordinary Coordinates

More generally, we regard all homogenous coordinate points (w p1, w p2, w)T, w ≠ 0, as representing the same ordinary coordinate point (p1, p2)T.Hence, in general, the homogeneous coordinate point (a,b,c)T converts to the ordinary coordinate point (a/c, b/c)T.With all the transformations so far, c will equal 1, but we will see a couple of examples later where this is not the case.

Page 31: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 31

5.8 Notes & ExamplesBe careful when transforming vectors. Doesn't work for position vectors or surface normals (i.e. vectors perpendicular to given surfaces).

' shear≠v M v

shearapply M

v

shear shear

1 1 2 1= , = '

0 0 1 0⎛ ⎞ ⎛ ⎞ ⎛ ⎞

⇒ = ≠⎜ ⎟ ⎜ ⎟ ⎜ ⎟⎝ ⎠ ⎝ ⎠ ⎝ ⎠

v M M v v

Page 32: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 32

Transforming Vectors (cont’d)How do we find the transformed surface normal v’ ?

( ) ( )1 1' for any surface normal and linear transf. TT − −= =v M v M v v M

'v = shearapply M M

v

a

b 'b

'a

( )( ) ( )1 1

is perpendicular to ( ) ( ) 0 ( ) 0' ( ' ') ( ' ') ' 0 ( ' ') ' 0

( ' ') ' ( ) ' ( ) ' ( ) '

Cho

must be

ose ' then ( ' ') '

p

( ) (

erpendicular to

T

T

TT T T T

T T T T T− −

− ⇔ − = ⇔ − =− ⇔ − = ⇔ − =

− = − = − = −

= − = − = −

v b a b a v b a vv b a b a v b a v

b a v Mb Ma v M b a v b a M v

v M v b a v b a M M v b a

••

) 0=v•

Page 33: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 33

Composition of transformationsWith homogeneous coordinates, it’s now much easier to compose multiple transformations into a single one.Consider for example the problem of rotating some object about its centre point C.

translate the whole object so that its centre is at the origin, rotate about the origin, and then translate back.Hence, transformation is

Can multiply the three component matrices to get the composite transformation matrix M, and then apply M to all points in the object.UDOO: Work out M – show that it is equivalent to a rotation of θ followed by a single (different) translation.

( ) ( )1 2 1 2

1 1

2 2

1 1where

1 0 cos sin 0 1 00 1 sin cos 0 0 10 0 1 0 0 1 0 0 1

T Tq q p p

c cc c

θ θθ θ

=

− −⎛ ⎞⎛ ⎞⎛ ⎞⎜ ⎟⎜ ⎟⎜ ⎟= −⎜ ⎟⎜ ⎟⎜ ⎟⎜ ⎟⎜ ⎟⎜ ⎟⎝ ⎠⎝ ⎠⎝ ⎠

M

Mθ p

Cx

y q

Page 34: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 34

Example 1In general affine transformations do not commute:

First scale by (1,2), then rotate 90o

First rotate 90o then scale by (1,2)

0 1 0 1 0 0 0 2 01 0 0 0 2 0 1 0 00 0 1 0 0 1 0 0 1

− −⎛ ⎞⎛ ⎞ ⎛ ⎞⎜ ⎟⎜ ⎟ ⎜ ⎟= =⎜ ⎟⎜ ⎟ ⎜ ⎟⎜ ⎟⎜ ⎟ ⎜ ⎟⎝ ⎠⎝ ⎠ ⎝ ⎠

M

1 0 0 0 1 0 0 1 00 2 0 1 0 0 2 0 00 0 1 0 0 1 0 0 1

− −⎛ ⎞⎛ ⎞ ⎛ ⎞⎜ ⎟⎜ ⎟ ⎜ ⎟= =⎜ ⎟⎜ ⎟ ⎜ ⎟⎜ ⎟⎜ ⎟ ⎜ ⎟⎝ ⎠⎝ ⎠ ⎝ ⎠

M

Page 35: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 35

Example 2Find the homogenous coordinate transformation matrix that transforms the figure on the left to the figure on the right

Often easier to do these backwards, then take inverse. In this case, starting with figure on right:

Rotate -30o , shift by (-3,1) , scale by (1/2, 1)Hence required transformation from right to left is:

R(30) T(3,-1) S(2,1)Easy to convert to HC matrix expression [UDOO]

0 1 00

1

x x

y

y

2

3

1

30o

Page 36: 5. 2D Geometry - Auckland · Vectors are represented as 2-tuples (2D) or 3-tuples (3D) in a coordinate system. We denote the components of a vector v in 2D with v 1and v 2 and of

© 2008 Burkhard Wuensche http://www.cs.auckland.ac.nz/~burkhard Slide 36

Example 3Given is the 2D scene in part (a) of the image below. Write down the homogeneous 2D transformation matrix M, which transforms the object shown in (a) into the object in part (b) of the image. You are allowed to write the transformation matrix as a product of simpler matrices (i.e. you are not required to multiply the matrices).