9

Let's say we have a 100x100 coordinate system, like the one below. 0,0 is its left-top corner, 50,50 is its center point, 100,100 is its bottom right corner, etc.

Now we need to draw a line from the center outwards. We know the angle of the line, but need to calculate the coordinates of its end point. What do you think would be the best way to do it?

For example, if the angle of the line is 45 degrees, its end point coordinates would be roughly 75,15.

enter image description here

Roger
  • 6,443
  • 20
  • 61
  • 88
  • 4
    A wild [Trigonometry](http://en.wikipedia.org/wiki/Trigonometry) appears! Trigonometry used [Sine and Cosine](http://en.wikipedia.org/wiki/Cosine#Sine.2C_cosine.2C_and_tangent)! It's Super-Effective! – Matt Ball Jun 08 '11 at 14:09
  • Albeit your question is pretty basic, you can ask math-related questions at www.mathoverflow.com. – Pedery Jun 08 '11 at 14:32
  • In this case getting a code implementation to do the math may warrant asking it here? – Wouter Simons Jun 08 '11 at 14:55

3 Answers3

13

You need to use the trigonometric functions sin and cos.

Something like this:

theta = 45
// theta = pi * theta / 180      // convert to radians.
radius = 50
centerX = 50
centerY = 50
p.x = centerX + radius * cos(theta)
p.y = centerY - radius * sin(theta)

Keep in mind that most implementations assume that you're working with radians and have positive y pointing upwards.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • @aioobe You edited your answer with a mistake there `centerY - radius * sin(theta)` shoud be `centerY + radius * sin(theta)` – thinklinux Mar 24 '16 at 11:30
  • @aioobe You are doing this only to change the origin for y so the y will start from top not from the center. But this should be done with the centerY variable which is for the origin point so you can keep the `+` sign in the equation because it's the right one and change `centerY` to `-50` instead :) You can see the accepted answer here http://stackoverflow.com/questions/839899/how-do-i-calculate-a-point-on-a-circle-s-circumference If you leave it like that is confusing how to change the origin for y. – thinklinux Mar 24 '16 at 13:25
  • I'm doing it because the y-axis is inverted. The answer below does the same. – aioobe Mar 24 '16 at 13:56
3

Use the unit circle to calculate X and Y, but because your radius is 50, multiply by 50

http://en.wikipedia.org/wiki/Unit_circle

Add the offset (50,50) and bob's your uncle

X = 50 + (cos(45) * 50) ~ 85,36
Y = 50 - (sin(45) * 50) ~ 14,65

The above happens to be 45 degrees.

EDIT: just saw the Y axis is inverted

Wouter Simons
  • 2,856
  • 1
  • 19
  • 15
  • PS: I see some comments regarding radians in implementations. The easiest way to convert degrees to radians is to look for the macro or whatever in your implementation. Often there is something like deg_to_rad or whatever. If not, use the math aioobe provided: `pi * x / 180` or `x * (pi/180)` – Wouter Simons Jun 08 '11 at 14:35
2

First you would want to calculate the X and Y coordinates as if the circle were the unit circle (radius 1). The X coordinate of a given angle is given by cos(angle), and the Y coordinate is given by sin(angle). Most implementations of sin and cos take their inputs in radians, so a conversion is necessary (1 degree = 0.0174532925 radians). Now, since your coordinate system is not in fact the unit circle, you need to multiply the resultant values by the radius of your circle. In this given instance, you would multiply by 50, since your circle extends 50 units in each direction. Finally, using a unit circle coorindate system assumes your circle is centered at the origin (0,0). To account for this, add (or subtract) the offset of your center from your calculated X and Y coordinates. In your scenario, the offset from (0,0) is 50 in the positive X direction, and 50 in the negative Y direction.

For example:

cos(45) = x ~= .707
sin(45) = y ~= .707

.707*50 = 35.35

35.35+50 = 85.35
abs(35.35-50) = 14.65

Thus the coordinates of the ending segment would be (85.35, 14.65).

Note, there is probably a built-in degrees-to-radians function in your language of choice, I provided the unit conversion for reference.

edit: oops, used degrees at first

Ben Siver
  • 2,758
  • 1
  • 25
  • 42