Tuesday 15 January 2013

Finding the X and Y projections of a line

The traditional screen is like a sheet of paper.
You can draw things in perspective and give the illusion of depth, but ultimately everything is represented in a 2D plane. If you tried to move your head around to see behind an object on screen, the image would remain the same.

The computer uses Cartesian coordinates to describe the positions of the objects along an X and Y axis, like in this example (the direction of the Y-axis is reversed though).

But what if we have a line of a specific length and at a certain angle? How do we know the X and Y coordinates of both ends of the line?
That's possible with trigonometric functions. They relate the angle with the length of the sides of the triangle, which is the shape formed between the line and its X and Y projections.
The equations are basically like this:

sin α  y    <=>  y = h * sin α
             h
cos α  x    <=>  x = h * cos α
              h



The h (hypotenuse) is the length of the line, while α is the angle.

And so having calculated the X and Y projections of the line, all you have to do to draw it on the screen is to tell the computer the coordinates of the starting point and add the projections to obtain the coordinates of the ending point.

0 comments:

Post a Comment

 

Break-a-Loop Copyright © 2013 | Template design by O Pregador | Powered by Blogger Templates