32
\$\begingroup\$

I want to move one object (dot) in a circular path. How should I change the X and Y coordinates to accomplish this?

\$\endgroup\$
0

3 Answers 3

70
\$\begingroup\$

You can do that using simple math:

X := originX + cos(angle)*radius;
Y := originY + sin(angle)*radius;

(originX, originY) is the center of your circle. radius is its radius. That's it.

This works because the sine and cosine are mathematically related to the unit circle.

relationship of sine and cosine to the unit circle
Image credit: LucasVB (Own work) [Public domain], via Wikimedia Commons. (Scaled down to 70%.)

\$\endgroup\$
4
  • \$\begingroup\$ What if it's an oval? I.e. no set radius. \$\endgroup\$
    – test
    Jan 12, 2015 at 19:12
  • 3
    \$\begingroup\$ @test: If the oval is X or Y oriented, you can multiply corresponding axis position by additional factor. If you need more details, you should ask a separate question. \$\endgroup\$
    – Kromster
    Jan 13, 2015 at 5:10
  • \$\begingroup\$ @Anko: I don't think that the animation explains it better, but let it be, for those who need it. Converted to CW. \$\endgroup\$
    – Kromster
    May 27, 2015 at 13:33
  • \$\begingroup\$ @Kromster what about achieving the same result in 3d space? \$\endgroup\$
    – Tomas
    Apr 13, 2017 at 20:44
17
\$\begingroup\$

You can use the parametric equation as marked by Krom. To understand why we used this formula you have to understand what the equation is. This equation is derived from the Parametric equation of circle.

Considering the circle is drawn with the center on the origin (O) as shown in the diagram below Circle

If we take a point "p" on the circumference of the circle, having a radius r.

Let the angle made by OP (Origin to p) be θ. Let the distance of p from x-axis be y Let the distance of p from y-axis be x

Using the above assumptions we get the triangle as shown below : triangle

Now we know that cos θ = base/hypotenuse and sin θ = perpendicular/hypotenuse

which gives us cos θ = x/r and sin θ = y/r

:: x=r*cos θ and y=r*sin θ

But if the circle is not at the origin and rather at (a,b) then we can say that the center of the circle is shifted

a units in x axis
b units in y axis
So for such a circle we can change the parametric equation accordingly by adding the shift on the x and y axis giving us the following equations :

x=a+(r*cos θ)
y=b+(r*sin θ)


Where a & b are the x,y co-ordinates of the center of the circle.

Hence we found x and y the co-ordinates of the point on the circumference of the circle with radius r

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Thanks, really nice and brief answer for this problem, thumbs up \$\endgroup\$ Jun 18, 2018 at 20:27
7
\$\begingroup\$

There's another trick, where you use the sin(x+a) and cos(x+a) formulas, and that allows you to compute sin(a) and cos(a) -- a being the angle by which you want to move from your current position -- only once and do simply multiplication and additions at each step.

sin(x+a) = sin(x)*cos(a) + cos(x)*sin(a), iirc.

Of course, that assumes constant angular velocity.

Beware of limited arithmetic precision, though. I've observed in the past "circular" motion implemented that way that would draw a spiral as a result of occasional rounding down repeated over time. It might be necessary to reset position to (x0, y0) after each revolution.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .