4

Assuming I have a form and paint an oval on it. I then want to take a control (such as a picturebox) and (while keeping the top left corner of the control exactly on the line) I want to move the control pixel by pixel following the drawn oval.

Basically I want to calculate the Top/Left point for each position/pixel in my oval. I know its a basic formula but cant for the life of me remember what its called or how its accomplished.

Anyone care to help?

Example

Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
  • 3
    Not your exact question but close: http://stackoverflow.com/questions/2781206/finding-a-point-on-an-ellipse-circumference-which-is-inside-a-rectangle-having-ce – ChrisWue May 17 '11 at 07:22

2 Answers2

4
double step=1.0; //  how fast do you want it to move

double halfWidth=100.0; //  width of the ellipse divided by 2
double halfHeight=50.0; //  height of the ellipse divided by 2

for (double angle=0; angle<360; angle+=step)
{
    int x=(int)halfWidth * Math.Cos(angle/180*Math.PI);
    int y=(int)halfHeight * Math.Sin(angle/180*Math.PI);
    pictureBox.TopLeft=new Point(x,y);
}

EDIT:

Now, if you are about to ask why isn't it moving if you write it like that - you'll have to add message loop processing to it, in form of:

Application.DoEvents();

which you will place inside the loop.

Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99
1

Ellipse canonical form:

x-x^2/a^2 + y^2/b^2 = 1

where a = Xradius and b = Yradius. So, for example, if you want the top-left point of a rectangle on the bottom side of an ellipse:

y = Sqrt((1-x^2/a^2)*b^2)

upd: to move an ellipse to specified point XC,YC, replace each x with (x-XC) and (y-YC). so if you're (in C#) drawing an ellipse in a rectangle, so XC = rect.X + a YC = rect.Y + b and the final equation is y = Sqrt((1 - Pow(x - rect.X - rect.Width / 2, 2) * Pow(rect.Height / 2, 2)) + rect.Y + rect.Height / 2... seems to be correct)

Mikant
  • 299
  • 3
  • 18