views:

378

answers:

2

I have the start point (x1,y1) and the desired length and angle of the line.

If the angles were directions, 0 degrees is W, 90 is N, 180 is E and 270 is S. I can modify this if needed.

How can I use the start point, length and angle to determine the end point(x2, y2)?

+6  A: 

x2 = x1 + length*cos(angle)
y2 = y1 + length*sin(angle)

In this case angle is counter-clockwise increasing with 0 pointing towards positive x. The x axis is increasing to the right, and the y axis up.

abc
+1 for specifying the axes.
Mark Byers
+2  A: 

For a screen:

For W = 0, N = 90, E = 180, S = 270:

x2 = x1 - length * cos(angle)
y2 = y1 - length * sin(angle)

For E = 0, N = 90, W = 180, S = 270:

x2 = x1 + length * cos(angle)
y2 = y1 - length * sin(angle)

Note that you need to make sure your implementation of cos works in degrees not radians otherwise you will get lines at strange angles.

Mark Byers
I changed my angles to start at 0 being E and increasing counter-clockwise as specified above. Is that how it should work with this equation for the screen?
knuckfubuck
I've added an answer for both ways.
Mark Byers
If you define E as 0 and go clockwise, then you can use `abc`s answer.
Mark Byers
Thank you for the tip on checking into radians vs angles. That was one of the major problems with my code.
knuckfubuck
@knuckfubuck: You're welcome. I'm glad the little bit of extra info helped you. :)
Mark Byers