Basic 2D math (zz)

Source: Internet
Author: User
Tags cos square root

Guidance:
   Introduction
   Definition of a point
A point can be represented in 2D space as a pair of numbers, one for each of the X and Y axis:
   Distance between two points
The distance between two points <ax, ay> and can be found using the pythagorus theorem:
   DX = Ax-bx DY = Ay- Distance = SQRT (dx * dx + dy * Dy)
   Definition of a vector
A vector can be thought of in two ways: either a point at or a line going from the origin <0, 0> to the point, in most cases they're best thought of as the latter. in either case a 2D vector can be represented with 2 scalar values X and Y.
Vectors are often used to describe direction. If we take a 2D point at <5> and add a 2D vector <4,-4> to it then we get a new point:
  
  
Note that we 've simply MovedThe point at <5, 5> in Direction<4,-4>.
Vectors themselves can be added by adding each of their components, or they can be multiplied (scaled) by multiplying each component by some constant K (where k <> 0 ). scaling a vector by 2 (say) will still cause the vector to point in the same direction, but it will now be twice as long. of course you can also divide the vector by K (where k <> 0) to get a similar result.
To calculate the length of a vector we simply calculate the distance between the origin and the point:
   Length = |-<0, 0> | = SQRT (x-0) * (x-0) + (y-0) * (y-0 )) = SQRT (x * x + y * Y)The square root function is horrendously slow, so try to avoid calculating vector lengths whenever you can. A common problem in computer graphics is to find the shortest vector in a list, in this case you only need to calculate (x * x + y * Y) for each of them and find the smallest value from that (since the vector with the shortest length will also have the smallest squared length ).
Often in computer graphics you need to convert a vector to a unit vector, ie a vector that points in the same direction but has a length of 1. this is done by simply dividing each component by the length:
   Let be our vector, length = SQRT (x * x + y * Y) Unit vector = = | X , Y | Length | length |
(Where length = |)
Note that if the vector is already a unit vector then the length will be 1, and the new values will be the same as the old.
   Definition of a 2D line
There are a few different ways of storing a line, the method to use depends on your application.
First we can imagine a line as having 2 endpoints in 2D space:
   P1 = and P2 =.Alternatively we can imagine the line as having an origin (starting point) and a direction (vector ): Origin = Direction =It's simple to convert between these two formats, if we have a line defined by two endpoints then we can assume that the line's origin is at one endpoint and it's ction is the difference between both endpoints: Origin = p1 = Direction = P2-P1 =Calculating the length of the line can be done by simply calculating the length of the direction vector, ie the distance between the two endpoints.
The origin/direction method (also known as the parametric representation) is Fig when you need to find the coordinates of a given point on a line. The equation to do this is:
   = + K *Where k is some scalar value. setting K to 0 will return the first endpoint, setting it to 1 will return the second endpoint. if you want to find a point halfway along the line then simply set K to 0.5, similarly setting K to 2 will return a point on the line that is twice as far from P1 as P2 is. in computer graphics you often have to find intersections between lines and other objects (such as other lines, planes, cubes etc ). these intersection equations often return a K value for the point of intersection, and you have to plug this value into the abve equation to get the actual 3D coordinates for the point of intersection. if these functions return a K value where 0 <= k <= 1 then you know the intersection occurred on the line somewhere between the endpoints P1 and P2 (or on them ).
   The 2D dot product
The 2D dot product between two 2D vectors and> A * B = AX * bx + Ay *
If A and B are unit vectors then the dot product is the cosine of the angle between them, so the angle itself (theta) can be calculated by taking the inverse cosine of the dot product:
   Theta = invcos (A * B)
Fortunately you'll often only need the cosine of the angle between 2 vectors and not the angle itself, so the expensive step of calculating the inverse cosine can be skipped.
   2d Rotation
A point can be rotated around the origin <0, 0> by running it through the following equations to get the new point:
   X' = cos (theta) * X-sin (theta) * y Y' = sin (theta) * x + cos (theta) * yWhere Theta is the angle by which to rotate the point.
   Clockwise/anti-clockwise
The dot product and rotation equations give us a handy way of determining whether the points in a given polygon are stored in clockwise or anti-clockwise fashion.
If we are dealing with a concave polygon then we only need the first 3 points to determine the Polygon's orientation. Consider the polygon below:
  
  
By looking at it we can see that the points are stored clockwise, but we need a method for an application to determine this mathematically. To do this we take the first 3 points:
  
  
The next step is to calculate the directional value of each edge (the actual position of the polygon doesn't matter). The two edges will thus be:
   E1 = P1-P2 E2 = P3-P2We can now test the angle between these two edges, ie the angle at P2. if this angle is between 0 and 180 degrees then we know the points appear clockwise.
However, if we rotate E2 anti-clockwise by 90 degrees then we can test whether the angle is between-90 and + 90 degrees. the cosine of all angles between this range is between 0 and 1, and the cosine can easily be obtained by taking the dot product.
We know from above that we can rotate a point by-90 degrees with the following formula:
   X' = (COS (-90) * X)-(sin (-90) * y) = y Y' = (sin (-90) * x) + (COS (-90) * y) =-xThus we can use the following formula to determine if the 3 points are in clockwise order: E1x = P1x-P2x E1y = P1y-P2y E2x = P3x-P2x E2y = P3y-P2y If (e1x * e2y-e1y * e2x)> = 0) clockwise = true; Else clockwise = false;This technique (ie rotate one vector anti-clockwise by 90 degrees and take the dot product between both vectors) is known as the 2D cross product. the 3D cross product also has some useful properties and is disccused further in the article on basic 3D math.
   Circles
If the circle is centered on the Origin <0, 0> and R is the radius then the circle is represented by the following equation:
   X * x + y * Y-R * r = 0
Thus any point lies on the circle if it satisfies this equation. if X * x + y * Y-R * r <0 then we know the point is inside the circle. similarly, if X * x + y * Y-R * r> 0 we know it is outside.

This article is transferred from
Http://www.geocities.com/SiliconValley/2151/math2d.html? 200721

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.