Driving Behavioral Change for Information Management through Data-Driven Gree...
201707 SER332 Lecture 13
1. SER332
Introduction to Graphics and
Game Development
Lecture 13:
Linear Algebra
Javier Gonzalez-Sanchez
javiergs@asu.edu
PERALTA 230U
Office Hours: By appointment
6. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 5
Points and Vectors
Points specify locations in
space (or in the plane)
Vectors have a
magnitude and direction
x
)4,2(y
)2,4(
q
v
1v
2v
7. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 6
Vectors
2
2
1
R
v
v
v Îú
û
ù
ê
ë
é
=
2
2
2
1 vvv +=
Vector
Magnitude or
Length
v
÷÷
ø
ö
çç
è
æ
= -
1
21
tan
v
v
qOrientation
(angle)
8. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 7
Vectors
ú
û
ù
ê
ë
é
=
0
0
vZero Vector
Unit Vector or
Normalized Vector
1=v
11. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 10
Test yourself
§ Point + Vector = ?
§ Vector + Vector = ?
§ Point – Point = ?
§ Point + Point = ?
12. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 11
Point or Vector
Point + Vector = Point Vector + Vector = Vector
Point - Point = Vector
v
u
u-v
v
u
v-u Point + Point = Not Defined
16. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 15
Dot Product
• The dot product or inner product measures to what
degree two vectors are aligned
• Notation:
• Computation:
wvwvwv T
×==>< ,
[ ] 332211
3
2
1
321, wvwvwv
w
w
w
vvvwv ×+×+×=
ú
ú
ú
û
ù
ê
ê
ê
ë
é
=><
17. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 16
Dot Product
q
v=[5,5]
u=[7,3]
v * u = (5*7) + (5*3) = 50
v * w = (5*5) + (5*0) = 25
u * w = (7*5) + (3*0) = 35
u * u = (7*7) + (3*3) = 58
t * w = (0*5) + (5*0) = 0
w=[5,0]
t=[0,5]
18. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 17
Dot Product
• Two vectors v and w are perpendicular or normal iff
0, =>< wv
19. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 18
Dot Product
• Geometric Interpretation:
w
v
a
l
wv
wv
w
l
COS
×
==
,
)(a
20. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 19
Dot Product
q
v=[5,5]
u=[7,3]
v * u = (5*7) + (5*3) = 50
v * w = (5*5) + (5*0) = 25
u * w = (7*5) + (3*0) = 35
u * u = (7*7) + (3*3) = 58
t * w = (0*5) + (5*0) = 0
w=[5,0]
t=[0,5]
|t | = 5.00
|v| = 7.07
|u| = 7.61
|w| = 5.00
q = cos-1 (25/7.07*5) = 45º
21. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 20
Dot Product Calculation
Length = Magnitude
vvvvv T
== ,
25. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 24
Cross Product
• Notation: u = v x w
• The cross product is a vector!
• Magnitude of u: proportional to
the sine of the angle between v
and w
• u is perpendicular to v and w
• The direction of u follows the
right hand rule
w
v
a
u
asin|||||||||||||u|| wvwv =´=
26. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 25
Cross Product Computation
Computation:
ú
ú
ú
û
ù
ê
ê
ê
ë
é
-
-
-
==
÷
÷
÷
ø
ö
ç
ç
ç
è
æ
´
÷
÷
÷
ø
ö
ç
ç
ç
è
æ
=´=
2121
1313
3232
321
321
3
2
1
3
2
1
vwwv
vwwv
vwwv
www
vvv
kji
w
w
w
v
v
v
wvu
28. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 27
Cross Product and Triangle Area
The cross product is related to the area of a triangle
2
PRPQ
area
´
=
Q
R
P
asin|||||||||||||u|| wvwv =´=(parallelogram) area =
(triangle)
v
w
29. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 28
Test Yourself
Given a Triangle with
P[0,0,0], Q[0,5,0] and R[5,5,0]
What is the area?
Q R
P
30. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 29
Test Yourself
Given a Triangle with
P[0,0,0], Q[0,5,0] and R[5,5,0]
What is the area?
Q R
P
area (triangle) = [0,5,0] x [5,5,0] = |[0, 0, -25]|/2 = 25/2 = 12.5
area (parallelogram) = 25
area (parallelogram) = (5)(7.07) sin (45) = (5)(7.07...)(0.70...) = 25
31. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 30
Distance point to line
n
0p
p
//Compute the distance from AB to C
double LineToPointDistance2D
(double[] pA, double[] pB, double[] pointC) {
double dist = CrossProduct(pA, pB, pointC) / distance(pA, pB);
return Math.Abs(dist);
}
35. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 34
Matrix
• A matrix is a set of elements, organized into rows
and columns
ú
û
ù
ê
ë
é
dc
ba
rows
columns
36. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 35
Matrix Addition
• Addition
• Example
• Substraction
ú
û
ù
ê
ë
é
++
++
=ú
û
ù
ê
ë
é
+ú
û
ù
ê
ë
é
hdgc
fbea
hg
fe
dc
ba
ú
û
ù
ê
ë
é
=ú
û
ù
ê
ë
é
+ú
û
ù
ê
ë
é
64
78
51
26
13
52
ú
û
ù
ê
ë
é
--
--
=ú
û
ù
ê
ë
é
-ú
û
ù
ê
ë
é
hdgc
fbea
hg
fe
dc
ba
37. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 36
Matrix Multiplication
• Multiplication
ú
û
ù
ê
ë
é
++
++
=ú
û
ù
ê
ë
é
ú
û
ù
ê
ë
é
dhcfdgce
bhafbgae
hg
fe
dc
ba Multiply each row by
each column
38. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 37
Matrix Multiplication
pmmnpn BAC ´´´ =
Multiplication:
ú
û
ù
ê
ë
é
=ú
û
ù
ê
ë
é
ú
û
ù
ê
ë
é
1119
2917
51
26
.
13
52
Examples:
ú
û
ù
ê
ë
é
=ú
û
ù
ê
ë
é
ú
û
ù
ê
ë
é
1017
3218
13
52
.
51
26
A and B must have
compatible dimensions
39. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 38
Transpose
• Notation:
• Definition:
• Examples: )()( ji
T
ij AA =
ú
û
ù
ê
ë
é
=ú
û
ù
ê
ë
é
52
16
51
26
T
ú
û
ù
ê
ë
é
=
ú
ú
ú
û
ù
ê
ê
ê
ë
é
852
316
83
51
26
T
T
A
42. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 41
Determinants
• Determinant (Note A must be square)
• Example
13152
13
52
det -=-=ú
û
ù
ê
ë
é
3231
2221
13
3331
2321
12
3332
2322
11
333231
232221
131211
det
aa
aa
a
aa
aa
a
aa
aa
a
aaa
aaa
aaa
+-=
ú
ú
ú
û
ù
ê
ê
ê
ë
é
12212211
2221
1211
2221
1211
det aaaa
aa
aa
aa
aa
-==ú
û
ù
ê
ë
é
43. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 42
Inverse Matrices
• Notation
• I is the Identity (1s on the diagonal, 0s elsewhere)
• Inverse Computation:
IAAAA == -- 11
ú
û
ù
ê
ë
é
-
-
-
=ú
û
ù
ê
ë
é
-
1121
1222
12212211
1
2221
1211 1
aa
aa
aaaaaa
aa
44. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 43
Test Yourself
• Example
ú
û
ù
ê
ë
é
-
-
=ú
û
ù
ê
ë
é
-
61
25
28
1
51
26
1
ú
û
ù
ê
ë
é
=ú
û
ù
ê
ë
é
=ú
û
ù
ê
ë
é
ú
û
ù
ê
ë
é
-
-
=ú
û
ù
ê
ë
é
ú
û
ù
ê
ë
é
-
10
01
280
028
28
1
51
26
.
61
25
28
1
51
26
.
51
26
1
49. SER332 Advanced Graphics
Javier Gonzalez-Sanchez
javiergs@asu.edu
Spring 2017
Disclaimer. These slides can only be used as study material for the class SER332 at ASU. They cannot be distributed or used for another purpose.