How do I rotate a picturebox around a point?

2020-04-20 12:32发布

问题:

Solved

I am trying to simulate a planet rotating a star. I am currently aware of the syntax to move the picture box (I have this in a timer so it is repeated)

private void rotate_timer(object sender, EventArgs e) {
picturebox1.location = new point (picturebox1.location.x + 1,           
picturebox1.location.y + 1);
}

But I don't know where to start so that it rotates around a specific point. How would I go about rotating it around (0,0)?

回答1:

This may help:

float angle = 0;
float rotSpeed = 1;
Point origin = new Point(222, 222);  // your origin
int distance = 100;                  // your distance

private void timer1_Tick(object sender, EventArgs e)
{
    angle += rotSpeed;
    int x = (int)(origin.X + distance * Math.Sin(angle *Math.PI / 180f));
    int y = (int)(origin.Y + distance * Math.Cos(angle *Math.PI / 180f));
    yourControl.Location = new Point(x, y);
}

Pick your timer Interval and don't be disappointed that it will look a little uneven. Winforms is really bad at animation..

If you want the image to rotate as well you can find an example here.