420 likes | 439 Views
Explore the essential principles of computer graphics through vectors and transformations, including magnitude, direction, normalization, addition, subtraction, dot product, cross product, and basic transformations like translation, rotation, and scaling.
E N D
Computer GraphicsLecture 112D Transformations ITaqdees A. Siddiqics602@vu.edu.pk
Vectors • Another important mathematical concept used in graphics is the Vector • If P1 = (x1, y1, z1) is the starting point and P2 = (x2, y2, z2) is the ending point, then the vector V = (x2 – x1, y2 – y1, z2 – z1)
and if P2 = (x2, y2, z2) is the starting point and P1 = (x1, y1, z1) is the ending point, then the vector V = (x1 – x2, y1 – y2, z1 – z2)
y (5, 2) v x v' (5, 0) Vector Projections • Projection of v onto the x-axis
y (5, 3, 2) v x v' (5, 0, 2) z • Projection of v onto the xz plane
y |v| (5, 2) a x 2D Magnitude and Direction • The magnitude (length) of a vector: |V| = sqrt( Vx2 + Vy2 ) • Derived from the Pythagorean theorem • The direction of a vector: a = tan-1 (Vy / Vx) • a is angular displacementfrom the x-axis
3D Magnitude and Direction • 3D magnitude is a simple extension of 2D |V| = sqrt( Vx2 + Vy2 + Vz2 ) • 3D direction is a bit harder than in 2D • Need 2 angles to fully describe direction • Latitude/longitude is a real-world example
Direction Cosines are often used: • a, b, and g are the positive angles that the vector makes with each of the positive coordinate axes x, y, and z, respectively cos a = Vx / |V| cos b = Vy / |V| cos g = Vz / |V|
Vector Normalization • “Normalizing” a vector means shrinking or stretching it so its magnitude is 1 • creating unit vectors • Note that this does not change the direction • Normalize by dividing by its magnitude; V = (1, 2, 3) |V| = sqrt( 12 + 22 + 32) = sqrt(14) = 3.74
Vector Normalization Vnorm = V / |V| = (1, 2, 3) / 3.74 = (1 / 3.74, 2 / 3.74, 3 / 3.74) = (.27, .53, .80) |Vnorm| = sqrt( .272 + .532 + .802) = sqrt( .9 ) = .95 • Note that the last calculation doesn’t come out to exactly 1. This is because of the error introduced by using only 2 decimal places in the calculations above.
Vector Addition • Equation: V3 = V1 + V2 = (V1x+V2x , V1y+V2y , V1z+V2z) • Visually: y y V2 V3 V2 V1 V1 x x
y y V3 V2 V2 V1 V1 x x Vector Subtraction • Equation: V3 = V1 - V2 = (V1x-V2x, V1y-V2y, V1z-V2z) • Visually:
Dot Product • The dot product of 2 vectors is a scalar V1. V2 = (V1x V2x) + (V1y V2y )+ (V1z V2z ) • Or, perhaps more importantly for graphics: V1. V2 = |V1| |V2| cos(q) where q is the angle between the 2 vectors and q is in the range 0 q y V2 q V1 x
y V2 V1 x • Why is dot product important for graphics? • It is zero iff the 2 vectors are perpendicular • cos(90) = 0
The Dot Product computation can be simplified when it is known that the vectors are unit vectors V1. V2 = cos(q) because |V1| and |V2| are both 1 • Saves 6 squares, 4 additions, and 2 square roots
Cross Product • The cross product of 2 vectors is a vector V1x V2 = ( V1y V2z - V1z V2y , V1z V2x - V1x V2z , V1x V2y - V1y V2x ) • Note that if you are into linear algebra there is also a way to do the cross product calculation using matrices and determinants
Again, just as with the dot product, there is a more graphical definition: V1x V2 = u |V1| |V2| sin(q) where q is the angle between the 2 vectors and q is in the range 0 q and u is the unit vector that is perpendicular to both vectors • Why u? • |V1| |V2| sin(q) produces a scalar and the result needs to be a vector
V2 u V1 • The direction of u is determined by the right hand rule • The perpendicular definition leaves an ambiguity in terms of the direction of u • Note that you can’t take the cross product of 2 vectors that are parallel to each other • sin(0) = sin(180) = 0 produces the vector (0, 0, 0)
Forming Coordinate Systems • Cross products are great for forming coordinate system frames (3 vectors that are perpendicular to each other) from 2 random vectors • Cross V1 and V2 to form V3 • V3 is now perpendicular to both V1 and V2 • Cross V2 and V3 to form V4 • V4 is now perpendicular to both V2 and V3 • Then V2, V4, and V3 form your new frame
V1 and V2 are in the new xy plane V2 V2 V1 V3 V1 V2 x V3 z V1 V1 V4 y
Basic Transformations • Translation • Rotation • Scaling
Translation • Straight line movement • Rotation • Circular movement w.r.t pivot • Scaling • Change of size
6 5 4 Y 3 2 1 0 1 2 3 4 5 6 7 8 9 10 X Translation tx = 2 ty = 3
Translation Distances tx, ty • For point P(x,y) after translation we have P′(x′,y′) • x′ = x + tx , y′ = y + ty • (tx, ty) is Translation vector
Now x′ = x + tx , y′ = y + ty can be expressed as a single matrix equation: P′ = P + T Where P = P′ = T =
Rigid Body Transformation • Objects are moved without deformation • Every point on the object is translated by the same amount • Typically all the endpoints are translated and object is redrawn using new endpoint positions
Rotation Y 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 10 X
Repositions an object along a circular path in xy-plane • Rotation Angle θ • Rotation Point (xr, yr) • θ is +ve counterclockwise rotation • θ is -ve clockwise rotation • θ is zero ?
For simplicity, consider the pivot at origin and rotate point P (x,y)where x = r cosФ and y = r sinФ • If rotated by θ then: • x′ = r cos(Ф + θ) = r cosФ cosθ – r sinФ sinθand • y′ = r sin(Ф + θ) = r cosФ sinθ + r sinФ cosθ
Replacing r cosФ with x and r sinФ with y, we have: • x′ = x cosθ – y sinθand • y′ = x sinθ + y cosθ
For Row vector representation of P and P′, in order to get the same results: • Which is actually transpose of original R, therefore: P′T = (R . P)T = PT . RT
When rotating about (xr,yr) • x’ = xr + (x - xr) cosθ – (y - yr) sinθ • y’ = yr + (x - xr) sinθ – (y - yr) cosθ • A better way of expressing these as matrix operations would be discussed later
Rigid Body Transformation • Objects are rotated without deformation • Every point on the object is rotated by the same angle • Typically all the endpoints are rotated and object is redrawn using new endpoint positions
6 5 4 Y 3 2 1 0 1 2 3 4 5 6 7 8 9 10 X Scaling
Scaling • Scaling alters the size of an object • Scaling factors Sx and Sy are used • For polygons, coordinates of each vertex may be multiplied by Sx & Sy to produce the transformed coordinates: x′ = x.Sx y′ = y.Sy
Scaling factor > 1 Scaling factor < 1 Scaling factor = 1 Same valued scaling factors uniform scaling in x and y Different valued scaling factors differential scaling in x and y
When scaling w.r.t. origin, both the size and distance form origin get scaled. Size and distance fro origin: for Scaling factor 2 (double the size and double the distance from origin.) for Scaling factor 0.5 (reduce the size to 50% and also the distance from origin to 50%.
This can be controlled by scaling w.r.t. a fixed point (xf,yf) • x’ = xf + (x - xf)Sx • y’ = yf + (y - yf)Sy • These can be rewritten as: • x’ = x. Sx + xf (1 – Sx) • y’ = y. Sy + yf (1 – Sy) • Where the terms xf (1 – Sx) and yf (1 – Sy) are constant for all points in the object
Computer GraphicsLecture 112D Transformations ITaqdees A. Siddiqics602@vu.edu.pk