Deriving the major and minor axis of an ellipse

In summary, the current solution for deriving the major and minor axis of an ellipse from conjugate diameters points has a problem with certain angles resulting in a division by zero error. To make the code more robust, it is recommended to guard against division by zero by checking for specific conditions, such as making sure x2 - xc is not zero, xc is not zero or close to zero, phi is not an odd multiple of pi/2, and theta is not any multiple of pi. Additionally, it is suggested to use alternative approaches, such as exploiting the geometry of the points, to handle these special cases. Finally, it is also recommended to avoid using Math.Pow when squaring a number for better performance.
  • #1
frankinstein
74
0
1. Deriving the major and minor axis of an ellipse from conjugate diameters pointsCurrent solution:
Code:
double x1 = Diameter1.X - Center.X;
double y1 = Diameter1.Y - Center.Y;
double x2 = Diameter2.X - Center.X;
double y2 = Diameter2.Y - Center.Y;
double xc = (x2 + y1) / 2;
double yc = (y2 - x1) / 2;
double theta = Math.Atan2(yc - y2, x2 - xc);
double phi = Math.Atan2(yc, xc);
double d = xc / Math.Cos(phi);
double yu = yc + d * Math.Sin(theta);
double xv = xc + d * Math.Cos(theta);
double yv = yc - d * Math.Sin(theta);
double alpha = Math.Atan2(yv, xv);
a = (yu - y2) / Math.Sin(theta);
b = 0;
double SemiDiag1 = GraphicTools.GetRadius(Center, Diameter1);
double SemiDiag2 = GraphicTools.GetRadius(Center, Diameter2);
if (a != 0 && !double.IsNaN(a))
{
      b = Math.Sqrt((Math.Pow(SemiDiag1, 2) + Math.Pow(SemiDiag2, 2)) - Math.Pow(a, 2));
}
else
{
       b = d;
       a = Math.Sqrt((Math.Pow(SemiDiag1, 2) + Math.Pow(SemiDiag2, 2)) - Math.Pow(b, 2));
}

The above function where a = (yu - y2) / Math.Sin(theta) can be NaN when the conjugate radii's angles are: 0, 1.57, 3.14, 4.71, 6.28

All other angles the function works perfectly. The alternative process where a = 0 or a = NaN is inaccurate or completely dysfunctional. How does one treat the angles that don't work?
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
frankinstein said:
1. Deriving the major and minor axis of an ellipse from conjugate diameters points


Current solution:
Code:
double x1 = Diameter1.X - Center.X;
double y1 = Diameter1.Y - Center.Y;
double x2 = Diameter2.X - Center.X;
double y2 = Diameter2.Y - Center.Y;
double xc = (x2 + y1) / 2;
double yc = (y2 - x1) / 2;
double theta = Math.Atan2(yc - y2, x2 - xc);
double phi = Math.Atan2(yc, xc);
double d = xc / Math.Cos(phi);
double yu = yc + d * Math.Sin(theta);
double xv = xc + d * Math.Cos(theta);
double yv = yc - d * Math.Sin(theta);
double alpha = Math.Atan2(yv, xv);
a = (yu - y2) / Math.Sin(theta);
b = 0;
double SemiDiag1 = GraphicTools.GetRadius(Center, Diameter1);
double SemiDiag2 = GraphicTools.GetRadius(Center, Diameter2);
if (a != 0 && !double.IsNaN(a))
{
      b = Math.Sqrt((Math.Pow(SemiDiag1, 2) + Math.Pow(SemiDiag2, 2)) - Math.Pow(a, 2));
}
else
{
       b = d;
       a = Math.Sqrt((Math.Pow(SemiDiag1, 2) + Math.Pow(SemiDiag2, 2)) - Math.Pow(b, 2));
}

The above function where a = (yu - y2) / Math.Sin(theta) can be NaN when the conjugate radii's angles are: 0, 1.57, 3.14, 4.71, 6.28
Whenever theta is ##k\pi##, sin(theta) will be zero, so you'll have a division by zero problem at these values. You shouldn't have problems at ##\pi/2## (which you show above as 1.57) or ##3\pi/2## (which you show as 4.71), or at any odd multiple of ##\pi/2##.
frankinstein said:
All other angles the function works perfectly. The alternative process where a = 0 or a = NaN is inaccurate or completely dysfunctional. How does one treat the angles that don't work?

The short answer is - don't use those angles. It would help if you explained what you were doing.
 
  • #3
Mark44 said:
Whenever theta is ##k\pi##, sin(theta) will be zero, so you'll have a division by zero problem at these values. You shouldn't have problems at ##\pi/2## (which you show above as 1.57) or ##3\pi/2## (which you show as 4.71), or at any odd multiple of ##\pi/2##.


The short answer is - don't use those angles. It would help if you explained what you were doing.

Actually its the angle between the radii that's the problem and that's when theta is zero and if any of the x or y components zero out, like when the angle between the radii are ∏/2 or some multiple, causes the product of "a" to be zero or division by zero.

The function is used to generate an arc using the parametric equations for an ellipse, why the need for finding the major and minor axis from the conjugate diameter points. The parameters given to the function will have angles that result in products of "a" becoming unusable.

I was hoping that there was a better approach to extracting the major and minor axis from conjugate diameter points that didn't have the problems that my current solution has or a work around for those angles that produce an unusable "a".
 
  • #4
I don't know if you can work around the problem, but you can make your code more robust, by guarding against division by zero.

You'll potentially have problems with these lines of code:
Code:
double theta = Math.Atan2(yc - y2, x2 - xc);
double phi = Math.Atan2(yc, xc);
double d = xc / Math.Cos(phi);
// <snip>

double alpha = Math.Atan2(yv, xv);
a = (yu - y2) / Math.Sin(theta);
In the first line above, you should make sure that x2 - xc ≠ 0 or close to zero (say 10-7 or so).
In the second line, you don't want xc to be zero or very close to zero.
In the third line, you don't want phi to be an odd multiple of pi/2.
In the last line, you don't want theta to be any multiple of pi.

For these special cases, I would see if there is some geometry I could exploit, like maybe one point is directly above the other, or on the same horizontal line or what-have-you, and take that into consideration.

BTW, I always avoided using Pow when I was just squaring a number. Rather than use Math.Pow(x, 2), I would just have x * x. I'm not sure the compiler is smart enough to convert a Pow() function call to a simple multiplication, which would be much faster.
 

What is an ellipse?

An ellipse is a geometric shape that resembles a flattened circle. It has two axes, a major axis and a minor axis, which intersect at the center of the ellipse.

How do you calculate the major and minor axis of an ellipse?

The major axis is the longest diameter of an ellipse, while the minor axis is the shortest diameter. To calculate the major axis, you can measure the distance between the two farthest points on the ellipse. The minor axis can be calculated by measuring the distance between the two closest points on the ellipse.

What is the importance of knowing the major and minor axis of an ellipse?

The major and minor axis of an ellipse are important for understanding the shape and dimensions of the ellipse. They also play a crucial role in many mathematical and scientific applications, such as in astronomy, engineering, and physics.

Can the major and minor axis of an ellipse be equal in length?

Yes, in a circle, the major and minor axis are equal in length. This is because a circle is a special case of an ellipse where both axes have the same length.

How does changing the major and minor axis affect the shape of an ellipse?

Changing the length of the major and minor axis will change the overall shape of the ellipse. If the major axis is longer, the ellipse will be more elongated, while a shorter major axis will result in a more circular shape. Similarly, changing the length of the minor axis will also affect the roundness or elongation of the ellipse.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
Replies
4
Views
8K
Back
Top