103

It's been 10 years since I did any math like this... I am programming a game in 2D and moving a player around. As I move the player around I am trying to calculate the point on a circle 200 pixels away from the player position given a positive OR negative angle(degree) between -360 to 360. The screen is 1280x720 with 0,0 being the center point of the screen. The player moves around this entire Cartesian coordinate system. The point I am trying trying to find can be off screen.

I tried the formulas on article Find the point with radius and angle but I don't believe I am understanding what "Angle" is because I am getting weird results when I pass Angle as -360 to 360 into a Cos(angle) or Sin(angle).

So for example I have...

  • 1280x720 on a Cartesian plane
  • Center Point (the position of player):
    • let x = a number between minimum -640 to maximum 640
    • let y = a number between minimum -360 to maximum 360
  • Radius of Circle around the player: let r always = 200
  • Angle: let a = a number given between -360 to 360 (allow negative to point downward or positive to point upward so -10 and 350 would give same answer)

What is the formula to return X on the circle?

What is the formula to return Y on the circle?

enter image description here enter image description here

Community
  • 1
  • 1
Kyle Anderson
  • 1,820
  • 5
  • 21
  • 29
  • 1
    Question: Don't most games have there cordinates in the top left to 0,0? and y axis goes down, not up? – Persijn May 28 '16 at 19:57
  • 2
    For anyone finding this via a search: it varies by game engine. Not only that but in some engines, Y is Up, while in others Z is Up. – Sven Viking Mar 02 '22 at 00:29

9 Answers9

92

The simple equations from your link give the X and Y coordinates of the point on the circle relative to the center of the circle.

X = r * cosine(angle)  
Y = r * sine(angle)

This tells you how far the point is offset from the center of the circle. Since you have the coordinates of the center (Cx, Cy), simply add the calculated offset.

The coordinates of the point on the circle are:

X = Cx + (r * cosine(angle))  
Y = Cy + (r * sine(angle))
yoozer8
  • 7,361
  • 7
  • 58
  • 93
  • 1
    My confusion was first in the difference between ANGLE and DEGREE. I thought they were the same thing. Then I thought I was getting the point (x,y) on the plane but I was actually getting the length of the sides of x and y. I drew it out on paper then plopped it in excel to cover the range of degrees to check the formulas. It works now in my code. – Kyle Anderson Dec 31 '12 at 07:55
  • 4
    Shouldn't `X = xcircle + (r * sine(angle))` be `X = xcircle + (r * cosine(angle))` (and vice versa for the `Y`)? – txtechhelp Jun 22 '16 at 08:33
  • 6
    Notice that angle should be a value in radians! – Roman M Jul 17 '18 at 13:19
21

You should post the code you are using. That would help identify the problem exactly.

However, since you mentioned measuring your angle in terms of -360 to 360, you are probably using the incorrect units for your math library. Most implementations of trigonometry functions use radians for their input. And if you use degrees instead...your answers will be weirdly wrong.

x_oncircle = x_origin + 200 * cos (degrees * pi / 180)
y_oncircle = y_origin + 200 * sin (degrees * pi / 180)

Note that you might also run into circumstance where the quadrant is not what you'd expect. This can fixed by carefully selecting where angle zero is, or by manually checking the quadrant you expect and applying your own signs to the result values.

Seth Battin
  • 2,811
  • 22
  • 36
  • 1
    This should really be a comment rather than an answer. However, nice catch on radians vs. degrees. – yoozer8 Dec 31 '12 at 00:29
  • Zombie post question: in parens, is that `(deg * (pi / 180))` or the other way `((deg * pi) / 180)`? Also thanks for specifying the diff between rad vs deg. – monsto Dec 03 '17 at 20:11
  • @monsto zombies still send notifications. :). The inner parens don't matter because multiplication and division are commutative http://demonstrations.wolfram.com/TheCommutativePropertyOfMultiplication/. I am long guilty of putting excessive parens in my code. I pretend it's for clarity but clearly that's not strictly true, or you wouldn't have been bothered by it. – Seth Battin Dec 03 '17 at 20:29
7

I highly suggest using matrices for this type of manipulations. It is the most generic approach, see example below:

// The center point of rotation
var centerPoint = new Point(0, 0);
// Factory method creating the matrix                                        
var matrix = new RotateTransform(angleInDegrees, centerPoint.X, centerPoint.Y).Value;
// The point to rotate
var point = new Point(100, 0);
// Applying the transform that results in a rotated point                                      
Point rotated = Point.Multiply(point, matrix); 
  • Side note, the convention is to measure the angle counter clockwise starting form (positive) X-axis
Johan Larsson
  • 17,112
  • 9
  • 74
  • 88
6

I am getting weird results when I pass Angle as -360 to 360 into a Cos(angle) or Sin(angle).

I think the reason your attempt did not work is that you were passing angles in degrees. The sin and cos trigonometric functions expect angles expressed in radians, so the numbers should be from 0 to 2*M_PI. For d degrees you pass M_PI*d/180.0. M_PI is a constant defined in math.h header.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I figured angle and degree were probably not the same thing so am I correct in saying Angle = M_PI*d/180.0 where d can be a number from -360 to 360 or do I need another step? – Kyle Anderson Dec 31 '12 at 00:40
  • 2
    @Kyle `d` is from `0` to `360` or from `-180` to `180` (a complete circle), not from `-360` to `360` (two complete circles). – Sergey Kalinichenko Dec 31 '12 at 00:42
6

I also needed this to form the movement of the hands of a clock in code. I tried several formulas but they didn't work, so this is what I came up with:

  • motion - clockwise
  • points - every 6 degrees (because 360 degrees divided by 60 minuites is 6 degrees)
  • hand length - 65 pixels
  • center - x=75,y=75

So the formula would be

x=Cx+(r*cos(d/(180/PI))
y=Cy+(r*sin(d/(180/PI))

where x and y are the points on the circumference of a circle, Cx and Cy are the x,y coordinates of the center, r is the radius, and d is the amount of degrees.

Chait
  • 1,052
  • 2
  • 18
  • 30
Wraithious
  • 105
  • 1
  • 9
  • Thanks! That gave me the missing pieces. Applying `cos`|`sin` determines the direction, and the `180` does the whole circle. – AdamsTips Jun 19 '21 at 13:11
3

Here is the c# implementation. The method will return the circular points which takes radius, center and angle interval as parameter. Angle is passed as Radian.

public static List<PointF> getCircularPoints(double radius, PointF center, double angleInterval)
        {
            List<PointF> points = new List<PointF>();

            for (double interval = angleInterval; interval < 2 * Math.PI; interval += angleInterval)
            {
                double X = center.X + (radius * Math.Cos(interval));
                double Y = center.Y + (radius * Math.Sin(interval));

                points.Add(new PointF((float)X, (float)Y));
            }

            return points;
        }

and the calling example:

List<PointF> LEPoints = getCircularPoints(10.0f, new PointF(100.0f, 100.0f), Math.PI / 6.0f);
MD. Nazmul Kibria
  • 1,080
  • 10
  • 21
  • BEWARE that this could return 1 less item than expected due to rounding errors! i therefore added some maring so that i get the correct amount of items in the end (my example has floats instead of double); for (float interval = angleInterval; interval < 2 * Math.PI + 0.0000099f; interval += angleInterval) – sommmen Mar 27 '20 at 15:12
1

The answer should be exactly opposite.

X = Xc + rSin(angle)

Y = Yc + rCos(angle)

where Xc and Yc are circle's center coordinates and r is the radius.

-1

Recommend:

public static Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles)
{
    return Quaternion.Euler(angles) * (point - pivot) + pivot;
}
yoozer8
  • 7,361
  • 7
  • 58
  • 93
kid
  • 309
  • 3
  • 5
-5

You can use this:

Equation of circle where

(x-k)2+(y-v)2=R2

where k and v is constant and R is radius

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140