Computational Geometry Vectors, Translation, Rotation George Georgiev Telerik Corporation www.telerik.com
Table of Contents Analytic geometry Vector math Programming it Vectors and Points Vector math Addition / Translation Subtraction Multiplication / Scaling 2D Rotation Vector length Programming it
Analytic geometry The math
Analytic geometry Also called Cartesian geometry Coordinate system Applies algebraic principles to geometry Algebra Analysis
Analytic geometry Points (vertices) Represented by coordinates 2D – (X, Y); 3D – (X, Y, Z); Etc.. Points are locations in space
Analytic geometry Points Example – A (3, 2); H (-1.5, 3);
Analytic geometry Vectors Represented exactly the same way Different only by concept Have Direction Magnitude (length) Computers understand vectors Points – vectors added to the beginning of the coordinate system (0, 0)
Analytic geometry A Point A vector to the point
Vector math It's easy
Vector math Addition Subtraction Vectors can be added just like numbers Called translation when applied to a point Example A(5, 10) + B(-3, 7) = C(2, 17) Subtraction Subtracting two points gives the vector from the second to the first Example: A(5, 10) - B(-3, 7) = V(8, 3)
Vector math Multiplication By a number Called uniform scaling Multiple each coordinate by the number V(5, 7) * 3 = V’(15, 21) Called uniform scaling Division works the same way You can always multiply by 1/3 (that’s 0.33) V(15, 21) / 3 = V’(5, 7) V(15, 21) * 0.33 = V’(5, 7)
Vector math 2D Rotation A little more complicated Multiply two of the vector’s coordinates according to a formula The formula The vector V(x, y) rotated by the angle ALPHA x’ = x*cos(ALPHA) - y*sin(ALPHA) y’ = x*sin(ALPHA) + y*cos(ALPHA) The resulting vector is V’(x’, y’)
Vector math 2D Rotation Positive angle, counter-clockwise rotation Negative angle, clockwise rotation
Vector math Vector length Calculated with the Pythagorean theorem The vector’s x and y components are perpendicular The vector’s length is: length = sqrt (x*x + y*y) Where sqrt is the square root
Vector math Unit vector A unit vector is a vector with a length of 1 A unit vector multiplied by a number has length equal to the number ‘Converting’ a vector to a unit vector Called Normalization Get the length of the vector Divide the vector by it’s length
* Vector math Live Demo (c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
Point classes, functions, operators Programming geometry Point classes, functions, operators
Programming geometry We need A class describing a point/vector Operators for vector addition, multiplication A rotation method A normalization method A length calculation method
Programming geometry Live Demo * (c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
Computational geometry ? ? ? ? ? Questions? ? ? ? ? ? ?