7

I have a ellipse with center point is at origin(0,0)

double dHalfwidthEllipse = 10; 
double dHalfheightEllipse = 20;
double dAngle = 30;//Its in degree
PointF ptfPoint = new PointF();//To be found
PointF ptfOrigin = new PointF(0, 0);//Origin

Angle of point with respect to origin = 30 degree; How to get the point now given the above values using C#?

1 Answers1

21

See http://www.mathopenref.com/coordparamellipse.html

The parametric equation for an ellipse with center point at the origin, half width a and half height b is

x(t) = a cos t,
y(t) = b sin t

If you simply wish to draw an ellipse, given

double dHalfwidthEllipse = 10;       // a
double dHalfheightEllipse = 20;      // b
PointF ptfOrigin = new PointF(0, 0); // Origin

all you need is

PointF ptfPoint = 
    new PointF(ptfOrigin.X + dHalfwidthEllipse * Math.Cos(t * Math.Pi/180.0), 
               ptfOrigin.Y + dHalfheightEllipse * Math.Sin(t * Math.Pi/180.0) );

with t varying between -180 and 180 degrees.

However, as @Sebastian points out, if you wish to compute the exact intersection with a line through the center with angle theta, it gets a bit more complicated, since we need to find a t that corresponds to theta:

y(t)/x(t) = tan θ

b sin t / (a cos t) = tan θ

b/a tan t = tan θ

t= arctan(a tan θ / b) + n * π

So if we add

double dAngle = 30;                  // theta, between -90 and 90 degrees

We can compute t and ptfPoint:

double t = Math.Atan( dHalfwidthEllipse * Math.Tan( dAngle * Math.Pi/180.0 ) 
                                                    / dHalfheightEllipse);
PointF ptfPoint = 
    new PointF(ptfOrigin.X + dHalfwidthEllipse * Math.Cos(t), 
               ptfOrigin.Y + dHalfheightEllipse * Math.Sin(t) );

This works fine for the area around the positive x axis. For theta between 90 and 180 degrees, add π:

double t = Math.Atan( dHalfwidthEllipse * Math.Tan( dAngle * Math.Pi/180.0 ) 
                                                    / dHalfheightEllipse) + Math.Pi;

For theta between -180 and -90 degrees, subtract π:

double t = Math.Atan( dHalfwidthEllipse * Math.Tan( dAngle * Math.Pi/180.0 ) 
                                                    / dHalfheightEllipse) - Math.Pi;

As you get close to the y axis, x(t) approaches zero and the above calculation divides by zero, but there you can use the opposite:

x(t)/y(t) = tan (90 - θ)

a cos t / (b sin t) = tan (90 - θ)

a/b tan t = tan (90 - θ)

t = arctan ( b tan (90 - θ) / a ) + n * π

Community
  • 1
  • 1
flup
  • 26,937
  • 7
  • 52
  • 74
  • +1 - I prefer using an extension method instead of the `Math.Pi/180` but thats out of scope of question :) – Sayse Jul 20 '13 at 12:15
  • 1
    This gives you *a* point on the ellipse, but not the point at 30 degrees. From the link in your answer: "The parameter t can be a little confusing with ellipses [...] But t is *not* the angle subtended by that point at the center." See [this answer on math stackexchange](http://math.stackexchange.com/a/22067) for the correct solution. – Sebastian Feb 12 '14 at 05:23