简体   繁体   中英

How to make a circle move in circles?

Circle c1 = new Circle(20);
c1.relocate(200,200); //Set X and Y

What I want to do is make the circle move in circles around an invisible center of rotation. How can that be achieved?

Thanks.

Edit: I have extremely poor trigonometry skills.

You can use equations for a point on a circle using polar coordinates:

circle_x = rot_center_x + radius * cos(angle)
circle_y = rot_center_y + radius * sin(angle)

Using this you'll get a center point for your new circle. Then you just need to increase (counter clock wise) or decrease (clockwise) your angle, blank screen and draw the circle again.

The angle for trigonometric function is in radians, you have 2*pi radians in a full circle. So if you want angle zero degrees, put in 0 . For 90 degrees, put in pi/2.0 . For any other angle use this conversion formula:

angle_rad = pi/180.0 * angle_degrees

If you want to time your rotation you have to choose the angular speed of rotation omega.

omega = 2*pi*f

where f is frequency of rotation, for example f=1Hz means your object will rotate in full circle after one seecond. Omega is in radians per second, so if you have omega 10 radians, then your object will rotate 10 radians during one second, or 100 radians during 10 seconds.

Now you have to determine how much angle you need to add each frame of animation:

ang_inc = omega / fps_avg;
ang += ang_inc;

where fps_avg is measured average of frames per second.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM