1

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

Dario Mazhara
  • 71
  • 1
  • 7
  • You would rotate the image *within* the picturebox, not the picturebox itself - [How do I rotate a picture in C#](http://stackoverflow.com/questions/2163829/how-do-i-rotate-a-picture-in-c-sharp) - The same principle holds for moving the picturebox around in general: consider instead drawing the image at specific coordinates directly on to (presumably) the form. – Alex K. May 05 '16 at 23:29
  • But I don't need the image to rotate. I need the box to rotate _around_ a point with a given distance. – Dario Mazhara May 05 '16 at 23:35
  • 1
    You'll need to work with trigonometric functions (e.g. `x = Math.Sin(angle); y = Math.Cos(angle);`) if you want to rotate an object about an axis. (Origin of [0, 0] would simply be a rotation about the *z* axis at the origin.) – code_dredd May 05 '16 at 23:48
  • You mean how to move on a curved path, not how to rotate. The answer is: learn about your path and about animation. In winforms use a timer and calculate the new location points for each tick. if you also want to rotate you still need to rotate the image. controls can only be straight and cannot be rotated. – TaW May 06 '16 at 00:05

1 Answers1

0

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.

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111