1
votes

I need to rotate one Shape component (ellipse) around the other one (circle). It's seems to me that it's better to do it with polar coordinates. So the rotation formula is:

X := Round(CenterX + SIN(Angle) * Radius);
Y := Round(CenterY + COS(Angle) * Radius);

where X, Y - ellipse coordinates, Radius - rotation radius; Angle is rotation angle; CenterX, CenterY - center of rotation.

Also I got that in Timer component I must write the following code:

Angle := Angle + 0.01;
if Angle> 2*Pi then Angle := Angle - 2*Pi;

And Shape must be redrawn.

It would look like this:

enter image description here

But I can't gather all in a bunch. I don't know how to organize this all. Thanx for any help.

1
I think you need to specify some more details. I can't understand what X, Y are, what CenterX, CenterY are, what Angle and Radius are. How do they relate to ellipses and circles? - David Heffernan
@DavidHeffernan, thanx, I explained that in my question. - Frankie Drake

1 Answers

3
votes

Add a variable t: double to your form class, and do

procedure TForm1.Timer1Timer(Sender: TObject);
var
  cx, cy: integer;
  x, y: integer;
const
  r = 200;
begin
  cx := Shape1.Left + Shape1.Width div 2;
  cy := Shape1.Top + Shape1.Height div 2;

  x := cx + round(r*sin(t));
  y := cy + round(r*cos(t));

  Shape2.Left := x - Shape2.Width div 2;
  Shape2.Top := y - Shape2.Height div 2;

  t := t + 0.01;
end;

where Timer1.Interval = 30, say.

Personally, however, I really dislike when people do animations by moving VCL controls around. It is much better to resort to manual GDI (or even OpenGL) drawing.