Find a point on the circle by degree?

Say we have a 100x100 coordinate system, as shown below. 0,0 - its upper left corner, 50,50 - its central point, 100 100 - lower right corner, etc.

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

For example, if the line angle is 45 degrees, its endpoint coordinates will be approximately 75.15.

enter image description here

+7
source share
3 answers

You need to use the trigonometric functions sin and cos .

Something like that:

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

Keep in mind that most implementations assume that you work with radians and have a positive y pointing up.

+9
source

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

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

Add an offset (50.50) and bob your uncle

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

The above value is 45 degrees.

EDIT: just saw that the y axis is inverted

+3
source

First you would like to calculate the X and Y coordinates, as if the circle were a unit circle (radius 1). The X coordinate of the given angle is determined by the expression cos(angle) , and the Y coordinate is specified as sin(angle) . Most implementations of sin and cos take their inputs in radians, so conversion is necessary (1 degree = 0.0174532925 radians). Now, since your coordinate system is not really a unit circle, you need to multiply the resulting values ​​by the radius of your circle. In this case, you multiply by 50, as your circle expands 50 units in each direction. Finally, using the coorindate system with a unit circle assumes your circle is centered at the origin (0,0). To account for this, add (or subtract) the center offset from the 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 final segment will be (85.35, 14.65).

Note that your chosen language probably has a built-in degree-radian function, I provided a unit conversion for reference.

edit: oops, used degrees first

+1
source

All Articles