7

Everything seemed so plain and simple until I had to actually program it.

What I've got

I uploaded an image to explain it better.

  • I have a circle and I know

    • it's radius
    • center point coordinates
    • each button's initial coordinates (the red circles).

I want to be able, when I rotate the gray circle image, with 10 degrees, to calculate red buttons new coordinates (x1y1, x2y2).

This shouldn't be hard to achieve for someone who knows math, but I didn't manage to find a suitable solution. I've also searched around here and couldn't find a working solution. Any help is greatly appreciated. Thank you

enter image description here

The working solution, as Felice stated below is:

-first take care of rotation angle, on each redraw simply increment it

   angle = angle+mainRotationAngle;

    float x =  (float) (center.X + Math.cos(angle*Math.PI / 180F) * radius 
    float y =  (float) (center.Y + Math.sin(angle*Math.PI / 180F) * radius

    button.setX(x);
    button.setY(y);
Alin
  • 14,809
  • 40
  • 129
  • 218

2 Answers2

6

It is easyer if you keep with you the button initial angles, then modify the angle to produce the rotation. so in pseudocode:

newAngle = Angle+rot;
xbutton = center.x+cos(newAngle)*radius;
ybutton = center.y+sin(newAngle)*radius;

If you really just have the coordinates of the buttons, you can convert them to the angle by using the function atan2, in pseudocode:

buttonAngle = atan2(button.y-center.y,button.x-center.x);
Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • You have made my day Felice. Thank you very much for your help. I've posted the working android code in the question post. – Alin Apr 26 '11 at 12:00
  • Actually after testing this a bit more the buttons go crazy. As I slide the circle around, after a while buttons aren't on the same line anymore. Debugging to see what's all about. Any ideas in the meanwhile ? In case I rotate the circle in the other direction (not clockwise) does the formula change ? – Alin Apr 29 '11 at 07:24
  • store one single angle and increment one single angle. assume the other is always +180, so you avoid rounding errors. – Felice Pollano Apr 29 '11 at 07:32
1

x1 = x + r sin 10

y1 = y + r cos 10

x2 = x - r sin 10

y2 = y - r cos 10

NickT
  • 23,844
  • 11
  • 78
  • 121
  • As I commented before I have problems with buttons not moving correctly. Could you explain please in which cases I get + and in which ones I get - on the formula. Thank you. – Alin Apr 29 '11 at 07:27