Sie sind auf Seite 1von 35

Course Website: http://www.comp.dit.

ie/bmacnamee
Computer Graphics 2:
Maths Preliminaries
2
of
30
Introduction
Computer graphics is all about maths!
None of the maths is hard, but we need to
understand it well in order to be able to
understand certain techniques
Today well look at the following:
Coordinate reference frames
Points & lines
Vectors
Matrices
3
of
30
Big Idea
I
m
a
g
e
s

t
a
k
e
n

f
r
o
m

H
e
a
r
n

&

B
a
k
e
r
,

C
o
m
p
u
t
e
r

G
r
a
p
h
i
c
s

w
i
t
h

O
p
e
n
G
L


(
2
0
0
4
)

4
of
30
Coordinate Reference Frames 2D
When setting up a scene in computer
graphics we define the scene using simple
geometry
For 2D scenes we use
simple two dimensional
Cartesian coordinates
All objects are defined
using simple coordinate
pairs
x axis
y axis
P
y
x
5
of
30
Coordinate Reference Frames 2D
(cont)
x
y
(2, 3)
(2, 7) (7, 7)
(7, 3)
2 7
3
7
6
of
30
Coordinate Reference Frames 3D
For three dimensional scenes we simply add
an extra coordinate
x axis
y axis
z axis
P
y
z
x
7
of
30
Left Handed Or Right Handed?
There are two different ways in which we
can do 3D coordinates left handed or right
handed
Right-Hand
Reference System
Left-Hand
Reference System
We will mostly use
the right-handed
system
I
m
a
g
e
s

t
a
k
e
n

f
r
o
m

H
e
a
r
n

&

B
a
k
e
r
,

C
o
m
p
u
t
e
r

G
r
a
p
h
i
c
s

w
i
t
h

O
p
e
n
G
L


(
2
0
0
4
)

8
of
30
Points & Lines
Points:
A point in two dimensional space is given as
an ordered pair (x, y)
In three dimensions a point is given as an
ordered triple (x, y, z)
Lines:
A line is defined using a start point and
an end-point
In 2d: (x
start
, y
start
) to (x
end
, y
end
)
In 3d: (x
start
, y
start
, z
start
) to (x
end
, y
end
, z
end
)
9
of
30
Points & Lines (cont)
x
y
(2, 3)
(6, 7)
(7, 1)
(7, 3)
(2, 7)
The line from
(2, 7) to (7, 3)
10
of
30
The Equation of A Line
The slope-intercept
equation of a line is:

where:




The equation of the line gives us the
corresponding y point for every x point
x
y
y
0

y
end

x
end
x
0

b x m y + =
0
0
x x
y y
m
end
end

=
0 0
x m y b =
11
of
30
A Simple Example
Lets draw a portion of the line given by the
equation:


Just work out the y coordinate for each x
coordinate
5
4
5
3
+ = x y
12
of
30
A Simple Example (cont)
x
y
0
1
1
2
2
3 4 5 6 7 8 9 10
3
4
5
6
13
of
30
A Simple Example (cont)
For each x value just work out the y value:
5
3
2
5
4
3
5
3
) 3 ( = + = y
5
1
3
5
4
4
5
3
) 4 ( = + = y
5
4
3
5
4
5
5
3
) 5 ( = + = y
5
2
4
5
4
6
5
3
) 6 ( = + = y
5
5
4
7
5
3
) 7 ( = + = y
2
5
4
2
5
3
) 2 ( = + = y
x
y
2 3 4 5 6 7
2
5
14
of
30
Vectors
Vectors:
A vector is defined as the difference between
two points
The important thing is that a vector has a
direction and a length
What are vectors for?
A vector shows how to move from one point
to another
Vectors are very important in graphics -
especially for transformations
15
of
30
Vectors (2D)
To determine the vector between two points
simply subtract them
1 2
P P V =
) , (
1 2 1 2
y y x x =
) 3 7 , 1 6 ( =
) 4 , 5 ( =
x axis
y axis
P
2
(6, 7)
P
1
(1, 3)
V
P
2
(10, 7)
P
1
(5, 3)
V
WATCH OUT: Lots of pairs of points share the same
vector between them
P
2
(7, 10)
P
1
(2, 6)
V
16
of
30
Vectors (3D)
In three dimensions a vector is calculated in
much the same way
1 2
P P V =
) , , (
1 2 1 2 1 2
z z y y x x =
) , , (
z y x
V V V =
x axis
y axis
z axis
P
1
P
2
So for (2, 1, 3) to (7, 10, 5)
we get
) 2 , 9 , 5 (
) 3 5 , 1 10 , 2 7 (
=
=
17
of
30
Vector Operations
There are a number of important operations
we need to know how to perform with
vectors:
Calculation of vector length
Vector addition
Scalar multiplication of vectors
Scalar product
Vector product
18
of
30
Vector Operations: Vector Length
Vector lengths are easily calculated in two
dimensions:

and in three dimensions:
2 2
| |
y x
V V V + =
2 2 2
| |
z y x
V V V V + + =
19
of
30
Vector Operations: Vector Addition
The sum of two vectors is calculated by
simply adding corresponding components







Performed similarly in three dimensions
) , (
2 1 2 1 2 1 y y x x
V V V V V V + + = +
x axis
y axis
V
1

V
2

x axis
y axis
V
1

V
2

V
1
+ V
2
20
of
30
Vector Operations: Scalar Multiplication
Multiplication of a vector by a scalar proceeds
by multiplying each of the components of the
vector by the scalar
) , (
y x
sV sV sV =
x axis
y axis
sV
(sV
x
, sV
y
)

x axis
y axis
V
(V
x
, V
y
)

21
of
30
Other Vector Operations
There are other important vector operations
that we will cover as we come to them
These include:
Scalar product (dot product)
Vector product (cross product)

22
of
30
Matrices
A matrix is simply a grid of numbers



However, by using matrix operations we can
perform a lot of the maths operations
required in graphics extremely quickly
(
(
(

6 0 2
3 4 10
13 11 1
| | 4 3 2 1
(
(
(

2 . 1
7 . 6
3 . 4
(

42
15
23
8
16
4
23
of
30
Matrix Operations
The important matrix operations for this
course are:
Scalar multiplication
Matrix addition
Matrix multiplication
Matrix transpose
Determinant of a matrix
Matrix inverse
24
of
30
Matrix Operations: Scalar Multiplication
To multiply the elements of a matrix by a
scalar simply multiply each one by the scalar



Example:
(
(
(

=
(
(
(

i s h s g s
f s e s d s
c s b s a s
i h g
f e d
c b a
s
* * *
* * *
* * *
*
(
(
(

=
(
(
(

54 48 42
36 30 24
18 12 6
18 16 14
12 10 8
6 4 2
* 3
25
of
30
Matrix Operations: Addition
To add two matrices simply add together all
corresponding elements



Example:
(
(
(

+ + +
+ + +
+ + +
=
(
(
(

+
(
(
(

z i y h x g
w f v e u d
t c s b r a
z y x
w v u
t s r
i h g
f e d
c b a
(
(
(

=
(
(
(

+
(
(
(

37 33 29
25 21 17
13 9 5
19 17 15
13 11 9
7 5 3
18 16 14
12 10 8
6 4 2
Both matrices have to be the same size
26
of
30
Matrix Operations: Matrix Multiplication
We can multiply two matrices A and B
together as long as the number of columns
in A is equal to the number of rows in B
So, if we have an m by n matrix A and a p
by q matrix B we get the multiplication:
C=AB
where C is a m by q matrix whose elements
are calculated as follows:

=
=
n
k
ki ik ij
b a c
1
27
of
30
Matrix Operations: Matrix Multiplication
(cont)
Examples:
(
(
(


=
(
(
(

+
+
+
+
+
+
=
(

(
(
(


28
38
4
22
26
3
4 * 8 2 * 2
4 * 7 2 * 5
4 * ) 1 ( 2 * 0
3 * 8 1 * 2
3 * 7 1 * 5
3 * ) 1 ( 1 * 0
4 3
2 1

8
7
1
2
5
0
| | | | | | 32 6 * 3 5 * 2 4 * 1
6
5
4
3 2 1 = + + =
(
(
(

| |
(
(
(

=
(
(
(

=
(
(
(

18 12 6
15 10 5
12 8 4
3 * 6 2 * 6 1 * 6
3 * 5 2 * 5 1 * 5
3 * 4 2 * 4 1 * 4
3 2 1
6
5
4
28
of
30
Matrix Operations: Matrix Multiplication
(cont)
Watch Out! Matrix multiplication is not
commutative, so:
BA AB=
29
of
30
Matrix Operations: Transpose
The transpose of a matrix M, written as M
T
is obtained by simply interchanging the rows
and columns of the matrix
For example:
(
(
(

=
(

6
5
4
3
2
1
6 5 4
3 2 1
T
30
of
30
Other Matrix Operations
There are some other important matrix
operations that we will explain as we need
them
These include:
Determinant of a matrix
Matrix inverse
31
of
30
Summary
In this lecture we have taken a brief tour
through the following:
Basic idea
The mathematics of points, lines and vectors
The mathematics of matrices
These tools will equip us to deal with the
computer graphics techniques that we will
begin to look at, starting next time
32
of
30
Exercises 1
x
y
0
1
1
2
2
3 4 5 6 7 8 9 10
3
4
5
6
Plot the line y = x + 2 from x = 1 to x = 9
33
of
30
Exercises 2
Perform the following matrix additions:
| | | | | | __ __ __ __ 1 5 14 1 5 15 19 11 = +
(
(
(
(

=
(
(
(
(





+
(
(
(
(





__ __ __ __
__ __ __ __
__ __ __ __
__ __ __ __
5 16 3 3
0 9 14 1
5 15 15 10
11 12 10 16
10 15 4 19
18 9 2 15
6 3 5 14
0 11 20 3
(

=
(

+
(


__
__
__
__
__
__
17
7
5
15
14
1
8
4
9
3
19
13
34
of
30
Exercises 3
Perform the following matrix multiplications:
(
(
(
(

=
(
(
(
(

=
(
(
(
(

(
(
(
(





__
__
__
__
_______ __________
_______ __________
_______ __________
_______ __________
3
6
11
4
*
5 16 3 3
0 9 14 1
5 15 15 10
11 12 10 16
(

=
(

=
(
(
(
(

__
__
__
__
__________
__________
__________
__________
7
13
19
19
10
0
12
15
*
3
4
12
19
4
15
7
8
35
of
30
Exercises 4
Perform the following multiplication of a
matrix by a scalar



Calculate the transpose of the following
matrix
(

=
(
(
(

__
__
__
__
__
__
7
4
11
23
6
3
T
(
(
(
(

=
(
(
(
(

__
__
__
__
__
__
__
__
7
1
5
19
1
0
2
15
* 6

Das könnte Ihnen auch gefallen