750 likes | 981 Views
29/07/2012. Computer Graphics CSC3406. 2. This Week. Vector ArithmeticGeometric ConceptsPoints, Lines and PlanesExploiting Dot Products. 29/07/2012. Computer Graphics CSC3406. 3. Introduction. Why do we need to learn about vectors?we need to know where objects are positioned in the worldthe siz
E N D
1. 30/07/2012 Computer Graphics CSC3406 1 Computer Graphics CSC3406
Lecture Four
2. 30/07/2012 Computer Graphics CSC3406 2 This Week Vector Arithmetic
Geometric Concepts
Points, Lines and Planes
Exploiting Dot Products
3. 30/07/2012 Computer Graphics CSC3406 3 Introduction Why do we need to learn about vectors?
we need to know where objects are positioned in the world
the size and orientation of the objects
how far the objects are from one another
how reflections work
how physics works
how light falls on the objects
4. 30/07/2012 Computer Graphics CSC3406 4 Introduction Coordinates
2D
3D left-handed
3D right-handed
5. 30/07/2012 Computer Graphics CSC3406 5 Graphics Maths by Example
6. 30/07/2012 Computer Graphics CSC3406 6 Graphics Maths by Example
7. 30/07/2012 Computer Graphics CSC3406 7 Graphics Maths by Example
8. 30/07/2012 Computer Graphics CSC3406 8 Graphics Maths by Example
9. 30/07/2012 Computer Graphics CSC3406 9 Vectors, a closer look. A vector has a length and a direction.
Vectors are expressed in the same way as point coordinates.
Point (5,10)
Vector (5,10)
But how are they different?
10. 30/07/2012 Computer Graphics CSC3406 10 Vectors
11. 30/07/2012 Computer Graphics CSC3406 11 Vectors
12. 30/07/2012 Computer Graphics CSC3406 12 Vectors
13. 30/07/2012 Computer Graphics CSC3406 13 Vectors Your Turn
What is the vector that goes from P = (9,10) to Q = (15,7) ?
v = (6, -3)
What is the resulting point from adding vector v = (9,-20) to point P = (1,2) ?
Q = (10, -18)
14. 30/07/2012 Computer Graphics CSC3406 14 Graphics Maths by Example
15. 30/07/2012 Computer Graphics CSC3406 15 Graphics Maths by Example
16. 30/07/2012 Computer Graphics CSC3406 16 Graphics Maths by Example
17. 30/07/2012 Computer Graphics CSC3406 17 Vector Operations There are two fundamental vector operations:
you can scale them
8v
if v = (1,2) then 8v = (8,16)
you can add them
v + a
v = (3,4), a = (8,1) then v+a = (11,5)
18. 30/07/2012 Computer Graphics CSC3406 18 Vector Operations Scaling a Vector
19. 30/07/2012 Computer Graphics CSC3406 19 Vector Operations Adding Vectors
20. 30/07/2012 Computer Graphics CSC3406 20 Vector Operations Your Turn
Given vector v = (10,20,5) what is:
2v, 0.5v and -0.2v?
2v = (20,40,10)
0.5v = (5,10,2.5)
-0.2v = (-2, -4, -1)
Given vector v = (1,1,1) and a = (8,4,2), what is:
v + a, v a and a v
v + a = (9,5,3)
v a = (-7, -3, -1)
a v = (7, 3, 1)
21. 30/07/2012 Computer Graphics CSC3406 21 Vector Operations Linear Combinations
adding scaled vectors together
8v + 2a
Definition
a linear combination of m vectors v1, v2,,vm is a vector of the form:
w = a1v1 + a2v2 + + amvm
22. 30/07/2012 Computer Graphics CSC3406 22 Vector Operations Linear Combinations
Example
v = (1,2,3) and a = (1,1,1)
2v + 3a = (2,4,6) + (3,3,3) = (5,7,9)
23. 30/07/2012 Computer Graphics CSC3406 23 Vector Operations Linear Combinations
Affine Combinations
Coefficients add up to unity
in other words the scalars add up to 1
a1 + a2 + + am = 1
e.g. 3a + 2b 4c (3+2-4=1)
Forcing an affine combination
(1-t)a + (t)b
24. 30/07/2012 Computer Graphics CSC3406 24 Vector Operations Challenge Question
The follow is an affine transformation:
ia + jb + ?c
what is the coefficient of c?
i + j + ? = 1
? = 1 i j therefore
ia + jb + (1-i-j)c
25. 30/07/2012 Computer Graphics CSC3406 25 Vector Operations Linear Combinations
Convex Combinations
Coefficients add up to unity. but
all coefficients must be between 0 and 1
i.e.
a1 + a2 + + am = 1 and
1 >= ai >= 0 for all 1,,m
e.g
.9v + .1w
.25v + .75w
26. 30/07/2012 Computer Graphics CSC3406 26 Vector Operations Linear Combinations
Convex Combinations
The set of all convex combinations of two vectors, v1 and v2 is:
v = (1-a)v1 + av2
as a varies from 0 to 1
Why?
every possible scalar version of v1 and v2 is covered!
27. 30/07/2012 Computer Graphics CSC3406 27 Vector Operations Linear Combinations
Convex Combinations
v = (1-a)v1 + av2 can be rearranged to give:
v = v1 + a(v2-v1)
This shows that the vector v will be v1 plus some scaled version of the vector joining v1 with v2
28. 30/07/2012 Computer Graphics CSC3406 28 Vector Operations Linear Combinations
Convex Combinations
Given 3 vectors v1, v2 and v3 the set of linear combinations will be:
v = a1v1 + a2v2 + (1-a1-a2)v3
v = 0.2v1 + 0.3v2 + 0.5v3
v = 0.5v1 + 0.5v2 + 0v3
29. 30/07/2012 Computer Graphics CSC3406 29 Vector Operations Linear Combinations
Convex Combinations
Given 3 vectors v1, v2 and v3 the set of linear combinations will be:
v = a1v1 + a2v2 + (1-a1-a2)v3
v = 0.2v1 + 0.3v2 + 0.5v3
v = 0.5v1 + 0.5v2 + 0v3
30. 30/07/2012 Computer Graphics CSC3406 30 Graphics Maths by Example
31. 30/07/2012 Computer Graphics CSC3406 31 Vector Length Magnitude
is the length of the vector
determined using Pythagoras Theorem
can you remember what this is?
32. 30/07/2012 Computer Graphics CSC3406 32 Vector Operations Magnitude
Pythagoras Theorem
The length of the hypotenuse of a right-angled triangle is equal to the square root of the sum of the squares of the other two sides.
33. 30/07/2012 Computer Graphics CSC3406 33 Vector Operations Magnitude
Pythagoras Theorem
Think of a vector as the hypotenuse of a right-angled triangle.
The length of a vector is denoted using |v|
34. 30/07/2012 Computer Graphics CSC3406 34 Vector Operations Magnitude
Pythagoras Theorem
Example: What is the magnitude of v = (5,10)?
|v| = sqrt(52+102) = sqrt(25+100) = sqrt(125)
11.18
35. 30/07/2012 Computer Graphics CSC3406 35 Vector Operations Your Turn
Find |v| for:
v=(1,-2,5), w=(10,3,1) and t=(1,1,1)
|v| = 5.5677
|w| = 10.488
|t| = 1.732
36. 30/07/2012 Computer Graphics CSC3406 36 Vector Operations Magnitude
Since a vector is the path between two points, then the length of the vector must be the distance between the two!!
37. 30/07/2012 Computer Graphics CSC3406 37 Vector Operations Magnitude
Sometimes it is useful (as you will see soon) to scale a vector so that its length is unity (1).
A normal vector is denoted by a hat: .
This means dividing the vector coordinates by the length of the vector.
= a/|a|
38. 30/07/2012 Computer Graphics CSC3406 38 Vector Operations Magnitude
Example:
What is the normal version of a = (1,5,3) ?
|a| = sqrt(12 + 52 + 32) = 5.916
= (1/5.916, 5/5.916, 3/5.916)
= (0.169, 0.845, 0.5)
39. 30/07/2012 Computer Graphics CSC3406 39 Vector Operations Your turn
Normalise the following:
a = (2,4,6)
g = (1,1,1)
h = (0,5,1)
Answers with grosse rounding errors ?
= (0.26,0.53,0.8)
g = (0.6,0.6,0.6)
h = (0,1,0.2)
40. 30/07/2012 Computer Graphics CSC3406 40 Important Vectors 3 Vectors you should become good friends with:
Normalised vectors parallel to:
x axis (1,0,0); (called i)
y axis (0,1,0); (called j)
z axis (0,0,1); (called k)
These vectors are called:
the unit vectors
41. 30/07/2012 Computer Graphics CSC3406 41 Graphics Maths by Example
42. 30/07/2012 Computer Graphics CSC3406 42 Graphics Maths by Example
43. 30/07/2012 Computer Graphics CSC3406 43 Graphics Maths by Example
44. 30/07/2012 Computer Graphics CSC3406 44 Graphics Maths by Example
45. 30/07/2012 Computer Graphics CSC3406 45 Graphics Maths by Example
46. 30/07/2012 Computer Graphics CSC3406 46 Vector Operations The DOT Product
Used to solve geometrical problems in computer graphics.
Useful in determining the way in which lines and vectors intersect.
47. 30/07/2012 Computer Graphics CSC3406 47 Vector Operations The DOT Product
Calculated by multiplying and adding row values with column values.
Definition
The dot product of two vectors is denoted v?w and has the value:
48. 30/07/2012 Computer Graphics CSC3406 48 Vector Operations The DOT Product
or where v = (v1,v2) and w = (w1,w2)
the dot product, v ? w will be:
(v1w1+v2w2)
Example, v = (2,1) and w = (3,5) then v ? w will be:
2*3 + 1*5 = 11
Example, v = (2,2,2,2) and w = (4,1,2,1.1), v ? w will be:
2*4 + 2*1 + 2*2 + 2 * 1.1 = 16.2
49. 30/07/2012 Computer Graphics CSC3406 49 Vector Operations The DOT Product
The angle between two vectors.
The dot product can be used to find the angle between two vectors or intersecting lines.
Given 2 vectors e and c, the angle between the vectors can be calculated.
e = (|e|cos ?e,|e|sin ?e)
c = (|c|cos ?c,|c|sin ?c)
The dot product e ? c is
|e||c|cos(?c - ?e)
or e ? c =|e||c|cos(?)
where ? is the angle between the vectors!
50. 30/07/2012 Computer Graphics CSC3406 50 Vector Operations The DOT Product
e ? c =|e||c|cos(?)
Divide both sides by |e||c| for a more compact form:
(e ? c)/|e||c| =|e||c|cos(?)/|e||c|
c ? = cos(? )
Therefore:
The angle between two vectors is the dot product of the normalised vectors.
51. 30/07/2012 Computer Graphics CSC3406 51 Vector Operations The DOT Product
Example: Find the angle between (5,6) and (8,2)
cos(? ) = c ?
c = c/|c| = (5,6) / sqrt(52+62)
= (5,6) / 7.8
= (0.64,0.77)
= e/|e| = (8,2) / sqrt(82+22)
= (8,2) / 8.25
= (0.8,0.24)
c ? = 0.8248
? = cos-1(0.8248) = 34.43
52. 30/07/2012 Computer Graphics CSC3406 52 Graphics Maths by Example
53. 30/07/2012 Computer Graphics CSC3406 53 Graphics Maths by Example
54. 30/07/2012 Computer Graphics CSC3406 54 Enter the Cross Product Cross Products
The cross product of two vectors is another vector.
The resulting vector is perpendicular to both vectors.
55. 30/07/2012 Computer Graphics CSC3406 55 Vector Operations Cross Products
Given a = (ax,ay,az) and e = (ex,ey,ez) the cross product would be (in terms of the unit vectors):
a x e = i(ayez-azey) + j(axez-azex) + k(axey-ayex)
or if you find it easier you can form a determinant, thus:
56. 30/07/2012 Computer Graphics CSC3406 56 Vector Operations Cross Products
How do you use this to calculate the dot product?
Take each item in the top row and multiply by the difference of the products of the items in the other columns.
57. 30/07/2012 Computer Graphics CSC3406 57 Vector Operations Cross Products
i(ayez-azey)
j(azex-axez)
k(axey-ayex)
58. 30/07/2012 Computer Graphics CSC3406 58 Vector Operations Cross Products
i(ayez-azey) + j(azex-axez) + k(axey-ayex)
is just another way of writing a 3d coordinate e.g.
(x, y, z)
( (ayez-azey), (azex-axez), (axey-ayex) )
59. 30/07/2012 Computer Graphics CSC3406 59 Vector Operations Cross Product
Now we have the cross product what are we going to do with it?
We can use it to find perpendicular vectors and areas.
60. 30/07/2012 Computer Graphics CSC3406 60 Vector Operations Cross Product
a x e is perpendicular to both a and e
the length of a x e equals the area of the parallelogram bounded by a and e
Use the right hand thumb rule to determine the direction of a x e
61. 30/07/2012 Computer Graphics CSC3406 61 Vector Operations Cross Product
Finding the Normal to a Plane
Given three points on the plane we can determine the normal to the plane.
P1, P2, P3 -> v = P2-P1, w = P3-P1
Find v x w to calculate the normal, n.
Any scalar multiple of n is also normal to the plane.
62. 30/07/2012 Computer Graphics CSC3406 62 Vector Operations Cross Product
We can also.....
Determine if you need to turn right or left to go from facing one vector to another.
63. 30/07/2012 Computer Graphics CSC3406 63 Vector Operations Cross Product
We can also.....
Determine if you need to turn right or left to go from facing one vector to another.
64. 30/07/2012 Computer Graphics CSC3406 64 Vector Operations Cross Product
We can also.....
Determine if you need to turn right or left to go from facing one vector to another.
65. 30/07/2012 Computer Graphics CSC3406 65 Vector Operations Cross Product
We can also.....
Determine if you need to turn right or left to go from facing one vector to another.
66. 30/07/2012 Computer Graphics CSC3406 66 Vector Operations Cross Product
We can also.....
Determine if you need to turn right or left to go from facing one vector to another.
67. 30/07/2012 Computer Graphics CSC3406 67 Vector Operations Cross Product
Therefore:
if the z value of the cross product is negative then we turn clockwise
if the z value of the cross product is positive then we turn counterclockwise
68. 30/07/2012 Computer Graphics CSC3406 68 Vector Operations Cross Product
Therefore:
double turnAngle(vector v1, vector v2)
{
double angle = acos(cat.velocity.x*v.x + cat.velocity.y*v.y)
* 180/3.14; //convert to degrees
//if z of cross product is negative
if( (v1.x*v2.y - v1.y*v2.x) < 0)
return -angle;
//if z of cross product is positive
else if ((v1.x*v2.y - v1.y*v2.x) > 0)
return angle;
//if z is 0 then no turn angle
return 0;
}
-mouseChase7.exe
69. 30/07/2012 Computer Graphics CSC3406 69 Lets move the Rat If we program a mouse click to change the location of the rat, we would assume the cat will change directions and move towards it.
This is a great test for our program.
70. 30/07/2012 Computer Graphics CSC3406 70 Lets move the Rat void myMouse(int button, int state, int x, int y)
{
//if left button clicked move the rat
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
rat.x = x;
rat.y = SCREENHEIGHT - y;
}
}
71. 30/07/2012 Computer Graphics CSC3406 71 Homogeneous Coordinates Some graphics systems and OpenGL represent points and vectors as homogeneous coordinates.
This means that in 2D a coordinate has 3 values (x, y, v)
and in 3D, 4 values (x, y, z, v)
72. 30/07/2012 Computer Graphics CSC3406 72 Homogeneous Coordinates For a point v = 1
For a vector v = 0
e.g. The point (2,4) becomes (2,4,1).
e.g. The vector (3,5) becomes (3,5,0).
e.g. The point (3,4,1) becomes (3,4,1,1).
e.g. The vector (3,6,7) becomes (3,6,7,0).
When it comes to working with the point or vector the 1 and 0 can just be dropped.
The reason for this will become clear in later chapters..
73. 30/07/2012 Computer Graphics CSC3406 73 Example Tweening between shapes
- tween.exe
74. 30/07/2012 Computer Graphics CSC3406 74 The End Next week.
Rotations
Scalings and
Translations