Find point on Circle on Android

2019-04-29 04:21发布

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);

2条回答
神经病院院长
2楼-- · 2019-04-29 05:04

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);
查看更多
Melony?
3楼-- · 2019-04-29 05:16

x1 = x + r sin 10

y1 = y + r cos 10

x2 = x - r sin 10

y2 = y - r cos 10

查看更多
登录 后发表回答