Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finding a dot on a circle by degree?

Tags:

math

geometry

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

like image 410
Roger Avatar asked Jun 08 '11 14:06

Roger


3 Answers

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.

like image 73
aioobe Avatar answered Nov 15 '22 09:11

aioobe


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

like image 27
Wouter Simons Avatar answered Nov 15 '22 08:11

Wouter Simons


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

like image 2
Ben Siver Avatar answered Nov 15 '22 09:11

Ben Siver