How do I rotate a picturebox around a point?

人盡茶涼 提交于 2019-12-02 15:17:57

问题


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.



来源:https://stackoverflow.com/questions/37061856/how-do-i-rotate-a-picturebox-around-a-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!