In this article, we’ll see how to discretize an Ellipse (or a circle) into a polygon. Since a circle is just a specific case of an ellipse we’ll not discuss separately how to discretize a circle to a polygon!
Why discretize an ellipse to a polygon?
Discretization has several applications, the two most important being:
- Rendering Curves: Curves cannot be rendered directly on the screen. They first need to be approximated to polygons (in case of Circles and ellipses) or chained-line-segments (in case of Bezier curves and splines) and discretization serves that purpose! *
- Collision-Detection: While checking intersection for curves like Circle and Bezier curve is simple, some curves like Ellipses are way too complicated and checking intersection for them precisely is very inefficient! Hence they are first discretized to simple shapes like rectangle, polygon, etc for which collision can be detected efficiently!
* While one can render curves pixel by pixel with Bresengham’s algorithm, it is often not the best practice! Since modern GPUs have become much more powerful they can render a series of the pixel without any bloat! Also drawing any curve pixel by pixel strips any possibility of batching and GPU “memorization”! And because of the way modern CPUs work trigonometric functions are no longer “computationally expensive” (atleast most of the time)!
Proof-of-concept: So the main idea of discretizing an ellipse is to break the ellipse into vertices representing polygon whereby the number of segments of the polygon would either be calculated automatically or given by the client user! For this example, we’ll not calculate the number of segments automatically!
We’ll use C Borland Graphics API but the same principles can be applied to any graphics library! Linux Users may want to use SDL libgraph as a replacement for Borland Graphics Library! Also we’ll use C++ instead of C to use the built-in
pair data-structure that STL provides (and also for function-overloading that will come handy later)!
As it turns out, the Borland Graphics API doesn’t actually have a multi-format polygon rendering function like modern graphics libraries have! There’s drawPoly and fillPoly but the representation of the polygon might not make sense to some people and also it might lead us to some pointer problems. So we’ll implement our own polygon rendering function! We’ll represent a polygon as a vector of pairs of integers representing the vertices of the polygon! The main benefit of this is that a vector always knows it’s size, unlike arrays which under the hood is just a pointer unknown of its bounds.
In any case, here’s our code for rendering a polygon in Borland Graphics Library:-
Discretizing Ellipse to Polygon:
Now that we can render polygons we are ready to discretize an ellipse to a polygon!
So the key to discretizing an ellipse is to have a moving point that moves across the ellipse in equal intervals and creating a vertex on every such point (every point where the moving point passes over)! For this one must know the parametric form of an ellipse which is:-
Based on the above formula, here’s the code-snippet that discretizes our ellipse to a polygon:-
One last thing that’s left to do is overload the function such that the last parameter is not needed! We can set
segments to some default value but we’d like it to be calculated based on the dimensions of the ellipse! So here’s the second overload:-
To end this article, here’s the complete source-code:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Draw an Ellipse rotating over a Circle in C++ graphics
- Draw circle in C graphics
- Regular polygon using only 1s in a binary numbered circle
- Side of a regular n-sided polygon circumscribed in a circle
- Find the area of largest circle inscribed in ellipse
- Area of largest Circle inscribe in N-sided Regular polygon
- Program to calculate area of inner circle which passes through center of outer circle and touches its circumference
- Area of the circle that has a square and a circle inscribed in it
- Check if a circle lies inside another circle or not
- Equation of circle when three points on the circle are given
- Find area of the larger circle when radius of the smaller circle and difference in the area is given
- Mandlebrot Set in C/C++ Using Graphics
- Add QT GUI to Python for plotting graphics
- Computer Graphics | Antialiasing
- Julia Fractal set in C/C++ Using Graphics
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.