vector - Clemson University

advertisement
Basic elements of 3-D coordinate systems and linear algebra
• Coordinatate systems are used to assign numeric values
to locations with respect to a particular frame of
reference commonly referred to as the origin.
• The number of dimensions in the coordinate system is
equal to the number of perpendicular (orthogonal) axes
and is also the number of values needed to specify a
location with respect to the origin
Basic elements of 3-D coordinate systems and linear algebra
One dimension: the line
Two dimensions: the plane
Basic elements of 3-D coordinate systems and linear algebra
Three dimensions: the universe as we perceive it
(A right handed coordinate system is shown. In a left handed
system the direction of the positive z axis is reversed. )
Points in 3-D space
The location of a point P in 3-D Euclidean space is given by a
triple (px, py, pz)
The x, y, and z coordinates specify the distance you must
travel in directions parallel to the the x, y, and z axes starting
from the origin (0, 0, 0) to arrive at the point (px, py, pz)
Vectors in 3-D space
• Some physical properties, such as temperature and
area, are given completely by their magnitude, and only
need a number to represent them (scalar).
• But, for other physical quantities such as force, velocity,
or acceleration, we must know direction as well as size
(magnitude).
• We represent these physical properties as direction lines
or vectors. Vectors contain both magnitude and
direction.
Vectors in 3-D space
A vector in 3-D space is sometimes called a directed
distance because it represents both
– a direction and
– a magnitude or distance
In this context, the triple (px, py, pz) can also be considered
to represent
– the direction from the origin (0, 0, 0) to (px, py, pz)
and
– its length sqrt(px2 + py2 + pz2) is the Euclidean (straight
line) distance from the origin to (px, py, pz)
Properties of Vectors
1.
2.
3.
Two vectors are equal if
and only if they are equal in
both magnitude and
direction. a != b (length is
equal but not direction).
Vectors c are equal!
If c is a vector, then –c is
defined as having the same
magnitude but the reverse
direction to c.
Multiplying a vector by a
number (or scalar) just has
the same effect of changing
its scale. Eg. 2a would be
twice as long as a but the
same direction as a.
• Vectors:
Free vs. Position Vectors
• Free Vectors: Two Vectors
labelled c are defined to be equal
although we need a shift to move • Vectors:
them exactly on top of each
other. Vectors that can be
shifted about are called free
vectors.
• Position Vector: A vector that
indicates the position of a point
in a coordinate system.
– Eg: Vector r is called a
position vector of the point P
from the fixed origin O, and
describes the displacement of
P from O – must run from O
to P and can’t be shifted.
Points and Vectors
• Two points in 3D space
implicitly determine a
vector pointing from one to
the other. Given to point P
and Q, the vector R:
Example:
Let Q = (8, 6, 5) and P = (3, 2, 0)
Then:
1. Vector direction from Q to P is:
(3-8, 2-6, 0-5) = (-5, -4, -5)
R=P-Q=
(px - qx, py - qy, pz - qz )
• R represents the direction
from Q to P. Note that the
direction is a signed
quantity. The direction from
P to Q is the negative of
the direction from Q to P.
2. Vector direction from P to Q is:
(8-3, 6-2, 5-0) = (5, 4, 5)
Points and Vectors
• The length is defined as
the distance between P
and Q. The distance
from P to Q is always the
same as the distance
from Q to P.
Example:
Let Q = (8, 6, 5) and P = (3, 2, 0)
Then:
2. Distance between Q and P is:
Sqrt(25 + 16 + 25) = Sqrt(66) =
8.12.
Geometric Interpretation of Vector Arithmetic
• We illustrate vector
addition in 2D, but in 3D
the principles are same.
• Since we can think of
vectors as displacements
or journeys, to add two
vectors, we just need to
find the single
displacement which gives
the same result as doing
the two displacements
separately.
Example: If a boat sets a course to move
with velocity P in water flowing with velocity
Q, then it will actually have a Resultant
velocity of P + Q.
Useful operations on vectors:
We define the sum of two vectors P and Q as the
componentwise sums:
R = P + Q = (px + qx, py + qy, pz + qz)
(3, 4, 5) + (1, 2, 6) = (4, 6, 11)
The difference of two vectors is computed as the
componentwise differences:
R = P - Q = (px - qx, py - qy, pz - qz)
(3, 4, 5) - (1, 2, 6) = (2, 2, -1)
Useful operations on vectors (cont’d):
We also define multiplication (or scaling) of a vector by a
scalar number a
S = aP = (apx, apy, apz)
3 * (1, 2, 3) = (3, 6, 9)
The length of a vector P is a scalar whose value is denoted:
|| P || = sqrt(px2 + py2 + pz2)
|| (3, 4, 5) || = sqrt(9 + 16 + 25) = sqrt(50)
Useful operations on vectors (cont’d):
A unit vector is a vector whose length is 1. Therefore an
arbitrary vector P may be converted to a unit vector by
scaling it by 1 / (its own length). Here U is a unit vector in
the same direction as P.
U = ( 1 / || P || ) P
The inner product or dot product of two vectors P and Q is
a scalar number. It is a value expressing the angular
relationship between two vectors. The dot product is
computed by taking the sum of the componentwise
products of the two vectors.
x = P dot Q = (px qx + py qy, + pz qz)
(2, 3, 4) dot (3, 2, 1) = 6 + 6 + 4 = 16
Thus || P || = sqrt(P dot P)
Useful operations on vectors (cont’d):
The dot product has the following relationship with the
lengths of two vectors and the angle between them. If U
and V are vectors and q is the angle between them, then
U dot V = V dot U = || U ||* || V || * cos(q)
If U and V are unit vectors and q is the angle beween them
then:
cos (q) = U dot V = V dot U
In this course, we will refer to vectors as vectors.
Representing vectors in C
There are at least two easy ways to represent a vector:
Array based representation:
We can use the typedef facility to create a user defined
type called vector_t. An instance of vector_t is a three
element double precision array:
typedef double vector_t[3];
An instance of vector_t is a three element double
precision array and can be created as shown.
vector_t vec;
It is understood that
vec[0] is the x-component (coordinate)
vec[1] is the y-component
vec[2] is the z-component
Representing vectors in C
Array based representation (cont’d):
To make this association explicit we use the #define
facility
#define X 0
#define Y 1
#define Z 2
We can then create an instance of the vector (11, 2, -4)
as shown:
vector_t v;
v[X] = 11;
v[Y] = 2;
v[Z] = -4;
Representing vectors in C
Structure based representation
We can also define a structured type in which the
elements are explicitly named
typedef struct vector_type
{
double x;
double y;
double z;
} vector_t;
Representing vectors in C
Structure based representation
vector_t v;
In this representation, it is explicit that
v.x is the x-component
v.y is the y-component
v.z is the z-component
Representing vectors in C
The vector.h file contains the following definition:
typedef struct vector_type
{
double x;
double y;
double z;
} vector_t;
Vector functions
vector_t ray(vector_t v1, vector_t v2);
Compute a non-unitized vector from point t1 to point t2.
This is just computing the componentwise difference t2 t1 and returning the new vector value. NOTE: The
vector from t1 to t2 is not the same as the vector from t2
to t1!!!
double length(vector_t v);
Computes the length of the vector. The length is the
square root of the sum of the squares of the
components of the vector:
|| v || = sqrt(vx2 + vy2 + vz2)
Vector functions
vector_t scale(vector_t v, double factor);
This function multiplies each component of the input
vector “v" by a scaling factor "factor" and returns a new
vector that is scaled.
vector_t add(vector_t v1, vector_t v2);
Compute the componentwise sum v1 + v2 and return a
new vector.
vector_t diff(vector_t v1, vector_t v2);
Compute the componentwise difference v2 – v1 and
return a new vector.
Vector functions
vector_t unitize(vector_t v);
Construct a unit length vector in the direction of the input
vector v. A unit vector is a vector having length 1. To
obtain the unit vector in the direction of a vector "v",
scale "v" by (1 / ||v||). Hint: you can use length() and
scale() to do much of the work for you in this function.
If this function is called with the zero vector (0 = (0, 0,
0)) it should return zero vector (i.e. (0, 0, 0)). To avoid
crashing on a divide-by-zero exception, check for a
zero-length vector before calling scale().
Vector functions
double dot(vector_t v1, vector_t v2);
The inner product or dot product of two vectors P and Q is
a scalar number:
x = P dot Q = (px qx + py qy + pz qz).
Thus || P || = sqrt(P dot P)
We will find that the dot product operation is very useful in
the construction of the ray tracer because the dot product
of two UNIT vectors (a vector is just a type of vector)
yields the cosine of the angle between the two vectors.
That is, if P and Q are unit vectors then P dot Q is the
cosine of the angle between the vectors P and Q. If they
are not unit vectors then (P dot Q) / (|| P || || Q || ) yields
the cosine.
Vector functions
void printVector(FILE *outFile, char *label, vector_t t);
Print the 3 elements of a vector on a line prefixed by a label
to the output stream "outfile". Each value should be printed
in a field that is a minimum of 8 characters wide and with 4
decimal places (e.g. " 23.1200").
If t=(2.2, 0, -4.34567) the call: printVector(stderr, "Result: ",
t); should result in the output: Result: 2.2000 0.0000 -4.3456
Vector functions
vector_t readVector(FILE *inFile, char *errmsg)
Try to read three double values from the input stream
"inFile" and store the result in the vector pointed to by t.
The return value from readVector() should be the
vector_t value if no errors occur. On error readvector()
should print the error message pointed to by "errmsg"
and exit the program.
Representing other data
We will also find other data types that are readily
represented as 3 fields, in particular a point, a pixel, or
color.
Download