Let the user select a conic section in C#

Recall that the equation for a conic section is:

If you know the coordinates of some point on a conic section, then you can plug their X and Y coordinates into this general equation to get a system of equations with the unknown values A, B, C, D, E, and F.

You can also arbitrarily set one of those values. For example, you can set F = 1. (Suppose you find an equation for the same conic section where F = f ≠ 1, then simply divide A, B, C, D, E, and F by f to get another equation for the conic section where F = 1.)

To let the user select a conic section, let the user select five points. Then plug the points' X and Y coordinates into the general equation with F = 1 to get five equations and five unknowns A, B, C, D, and E. Now use Gaussian elimination to solve the equations to get A, B, C, D, and E.

For information on how to use Gaussian elimination, see the post Solve a system of equations with Gaussian elimination in C#.

The values used by this example tend to produce very small values for most of the constants A, B, C, D, and E. This tiny values cause some problems when determining which kind of conic section the equation represents. To make working with the values easier, this example scales the results so the smallest value (in absolute value) is 1.

After the program uses Gaussian elimination to find A, B, C, D, and E, it uses the techniques described in the example Draw a conic section from its polynomial equation in C# to draw the selected conic section.

See the code for details.

This method still doesn't handle some odd cases. For example, if you select five points that have the same X coordinate, then Gaussian elimination doesn't produce a unique solution. (The conic section would be the degenerate case of a line.)

Another unusual case occurs if the conic section's equation should have F = 0. If F = 0, then the step that assumes F = 1 is invalid. You would still have five equations and five unknowns but this program doesn't solve the equations correctly in that case.

This program doesn't look for these odd cases. It simply crashes. (You can add code to look for them if you like.)

(I'll have one more post on conic sections and then I'll leave the topic alone for a while.)



What did you think of this article?

  • No trackbacks exist for this post.
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.


 Email (will not be published)


Your comment is 0 characters limited to 3000 characters.