Midpoint ellipse drawing algorithm
Mid-point Ellipse algorithm is used to draw an ellipse in computer graphics.
Also refer : Midpoint line algorithm, Midpoint circle algorithm

Midpoint ellipse algorithm plots(finds) points of an ellipse on the first quadrant by dividing the quadrant into two regions.
Each point(x, y) is then projected into other three quadrants (-x, y), (x, -y), (-x, -y) i.e. it uses 4-way symmetry.
Function of ellipse:
fellipse(x, y)=ry2x2+rx2y2-rx2ry2
fellipse(x, y)<0 then (x, y) is inside the ellipse.
fellipse(x, y)>0 then (x, y) is outside the ellipse.
fellipse(x, y)=0 then (x, y) is on the ellipse.
Decision parameter:
Initially, we have two decision parameters p10 in region 1 and p20 in region 2.
These parameters are defined as : p10 in region 1 is given as :
p10=ry2+1/4rx2-rx2ry
Mid-Point Ellipse Algorithm :
- Take input radius along x axis and y axis and obtain center of ellipse.
- Initially, we assume ellipse to be centered at origin and the first point as : (x, y0)= (0, ry).
- Obtain the initial decision parameter for region 1 as: p10=ry2+1/4rx2-rx 2ry
- For every xk position in region 1 :
If p1k<0 then the next point along the is (xk+1 , yk) and p1k+1=p1k+2ry2xk+1+ry2
Else, the next point is (xk+1, yk-1 )
And p1k+1=p1k+2ry2xk+1 – 2rx2yk+1+ry2 - Obtain the initial value in region 2 using the last point (x0, y0) of region 1 as: p20=ry2(x0+1/2)2+rx2 (y0-1)2-rx2ry2
- At each yk in region 2 starting at k =0 perform the following task.
If p2k<0 the next point is (xk, yk+1) and p2k+1=p2k-2rx2yk+1+rx2
- Else, the next point is (xk+1, yk -1) and p2k+1=p2k+2ry2xk+1 -2rx2yk+1+rx2
- Now obtain the symmetric points in the three quadrants and plot the coordinate value as: x=x+xc, y=y+yc
- Repeat the steps for region 1 until 2ry2x>=2rx2y
Implementation:
C++
// C++ program for implementing // Mid-Point Ellipse Drawing Algorithm #include <bits/stdc++.h> using namespace std; void midptellipse(int rx, int ry, int xc, int yc) { float dx, dy, d1, d2, x, y; x = 0; y = ry; // Initial decision parameter of region 1 d1 = (ry * ry) - (rx * rx * ry) + (0.25 * rx * rx); dx = 2 * ry * ry * x; dy = 2 * rx * rx * y; // For region 1 while (dx < dy) { // Print points based on 4-way symmetry cout << x + xc << " , " << y + yc << endl; cout << -x + xc << " , " << y + yc << endl; cout << x + xc << " , " << -y + yc << endl; cout << -x + xc << " , " << -y + yc << endl; // Checking and updating value of // decision parameter based on algorithm if (d1 < 0) { x++; dx = dx + (2 * ry * ry); d1 = d1 + dx + (ry * ry); } else { x++; y--; dx = dx + (2 * ry * ry); dy = dy - (2 * rx * rx); d1 = d1 + dx - dy + (ry * ry); } } // Decision parameter of region 2 d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) + ((rx * rx) * ((y - 1) * (y - 1))) - (rx * rx * ry * ry); // Plotting points of region 2 while (y >= 0) { // Print points based on 4-way symmetry cout << x + xc << " , " << y + yc << endl; cout << -x + xc << " , " << y + yc << endl; cout << x + xc << " , " << -y + yc << endl; cout << -x + xc << " , " << -y + yc << endl; // Checking and updating parameter // value based on algorithm if (d2 > 0) { y--; dy = dy - (2 * rx * rx); d2 = d2 + (rx * rx) - dy; } else { y--; x++; dx = dx + (2 * ry * ry); dy = dy - (2 * rx * rx); d2 = d2 + dx - dy + (rx * rx); } } } // Driver code int main() { // To draw a ellipse of major and // minor radius 15, 10 centred at (50, 50) midptellipse(10, 15, 50, 50); return 0; } // This code is contributed // by Akanksha Rai |
C
// C program for implementing // Mid-Point Ellipse Drawing Algorithm #include <stdio.h> void midptellipse(int rx, int ry, int xc, int yc) { float dx, dy, d1, d2, x, y; x = 0; y = ry; // Initial decision parameter of region 1 d1 = (ry * ry) - (rx * rx * ry) + (0.25 * rx * rx); dx = 2 * ry * ry * x; dy = 2 * rx * rx * y; // For region 1 while (dx < dy) { // Print points based on 4-way symmetry printf("(%f, %f)\n", x + xc, y + yc); printf("(%f, %f)\n", -x + xc, y + yc); printf("(%f, %f)\n", x + xc, -y + yc); printf("(%f, %f)\n", -x + xc, -y + yc); // Checking and updating value of // decision parameter based on algorithm if (d1 < 0) { x++; dx = dx + (2 * ry * ry); d1 = d1 + dx + (ry * ry); } else { x++; y--; dx = dx + (2 * ry * ry); dy = dy - (2 * rx * rx); d1 = d1 + dx - dy + (ry * ry); } } // Decision parameter of region 2 d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) + ((rx * rx) * ((y - 1) * (y - 1))) - (rx * rx * ry * ry); // Plotting points of region 2 while (y >= 0) { // printing points based on 4-way symmetry printf("(%f, %f)\n", x + xc, y + yc); printf("(%f, %f)\n", -x + xc, y + yc); printf("(%f, %f)\n", x + xc, -y + yc); printf("(%f, %f)\n", -x + xc, -y + yc); // Checking and updating parameter // value based on algorithm if (d2 > 0) { y--; dy = dy - (2 * rx * rx); d2 = d2 + (rx * rx) - dy; } else { y--; x++; dx = dx + (2 * ry * ry); dy = dy - (2 * rx * rx); d2 = d2 + dx - dy + (rx * rx); } } } // Driver code int main() { // To draw a ellipse of major and // minor radius 15, 10 centred at (50, 50) midptellipse(10, 15, 50, 50); return 0; } |
Java
// Java program for implementing // Mid-Point Ellipse Drawing Algorithm import java.util.*; import java.text.DecimalFormat; class GFG { static void midptellipse(float rx, float ry, float xc, float yc) { float dx, dy, d1, d2, x, y; x = 0; y = ry; // Initial decision parameter of region 1 d1 = (ry * ry) - (rx * rx * ry) + (0.25f * rx * rx); dx = 2 * ry * ry * x; dy = 2 * rx * rx * y; DecimalFormat df = new DecimalFormat("#,###,##0.00000"); // For region 1 while (dx < dy) { // Print points based on 4-way symmetry System.out.println(df.format((x + xc)) + ", "+df.format((y + yc))); System.out.println(df.format((-x + xc)) + ", "+ df.format((y + yc))); System.out.println(df.format((x + xc)) + ", "+ df.format((-y + yc))); System.out.println(df.format((-x + xc)) + ", "+df.format((-y + yc))); // Checking and updating value of // decision parameter based on algorithm if (d1 < 0) { x++; dx = dx + (2 * ry * ry); d1 = d1 + dx + (ry * ry); } else { x++; y--; dx = dx + (2 * ry * ry); dy = dy - (2 * rx * rx); d1 = d1 + dx - dy + (ry * ry); } } // Decision parameter of region 2 d2 = ((ry * ry) * ((x + 0.5f) * (x + 0.5f))) + ((rx * rx) * ((y - 1) * (y - 1))) - (rx * rx * ry * ry); // Plotting points of region 2 while (y >= 0) { // printing points based on 4-way symmetry System.out.println(df.format((x + xc)) + ", " + df.format((y + yc))); System.out.println(df.format((-x + xc)) + ", "+ df.format((y + yc))); System.out.println(df.format((x + xc)) + ", " + df.format((-y + yc))); System.out.println(df.format((-x + xc)) + ", " + df.format((-y + yc))); // Checking and updating parameter // value based on algorithm if (d2 > 0) { y--; dy = dy - (2 * rx * rx); d2 = d2 + (rx * rx) - dy; } else { y--; x++; dx = dx + (2 * ry * ry); dy = dy - (2 * rx * rx); d2 = d2 + dx - dy + (rx * rx); } } } // Driver code public static void main(String args[]) { // To draw a ellipse of major and // minor radius 15, 10 centred at (50, 50) midptellipse(10, 15, 50, 50); } } // This code is contributed by // Surendra_Gangwar |
Python3
# Python3 program for implementing
# Mid-Point Ellipse Drawing Algorithm
def midptellipse(rx, ry, xc, yc):
x = 0;
y = ry;
# Initial decision parameter of region 1
d1 = ((ry * ry) – (rx * rx * ry) +
(0.25 * rx * rx));
dx = 2 * ry * ry * x;
dy = 2 * rx * rx * y;
# For region 1
while (dx < dy):
# Print points based on 4-way symmetry
print("(", x + xc, ",", y + yc, ")");
print("(",-x + xc,",", y + yc, ")");
print("(",x + xc,",", -y + yc ,")");
print("(",-x + xc, ",", -y + yc, ")");
# Checking and updating value of
# decision parameter based on algorithm
if (d1 < 0):
x += 1;
dx = dx + (2 * ry * ry);
d1 = d1 + dx + (ry * ry);
else:
x += 1;
y -= 1;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d1 = d1 + dx - dy + (ry * ry);
# Decision parameter of region 2
d2 = (((ry * ry) * ((x + 0.5) * (x + 0.5))) +
((rx * rx) * ((y - 1) * (y - 1))) -
(rx * rx * ry * ry));
# Plotting points of region 2
while (y >= 0):
# printing points based on 4-way symmetry
print(“(“, x + xc, “,”, y + yc, “)”);
print(“(“, -x + xc, “,”, y + yc, “)”);
print(“(“, x + xc, “,”, -y + yc, “)”);
print(“(“, -x + xc, “,”, -y + yc, “)”);
# Checking and updating parameter
# value based on algorithm
if (d2 > 0):
y -= 1;
dy = dy – (2 * rx * rx);
d2 = d2 + (rx * rx) – dy;
else:
y -= 1;
x += 1;
dx = dx + (2 * ry * ry);
dy = dy – (2 * rx * rx);
d2 = d2 + dx – dy + (rx * rx);
# Driver code
# To draw a ellipse of major and
# minor radius 15, 10 centred at (50, 50)
midptellipse(10, 15, 50, 50);
# This code is contributed by chandan_jnu
C#
// C# program for implementing // Mid-Point Ellipse Drawing Algorithm using System; class GFG { static void midptellipse(double rx, double ry, double xc, double yc) { double dx, dy, d1, d2, x, y; x = 0; y = ry; // Initial decision parameter of region 1 d1 = (ry * ry) - (rx * rx * ry) + (0.25f * rx * rx); dx = 2 * ry * ry * x; dy = 2 * rx * rx * y; // For region 1 while (dx < dy) { // Print points based on 4-way symmetry Console.WriteLine(String.Format("{0:0.000000}", (x + xc)) + ", "+String.Format ("{0:0.000000}",(y + yc))); Console.WriteLine(String.Format("{0:0.000000}", (-x + xc)) + ", "+ String.Format ("{0:0.000000}",(y + yc))); Console.WriteLine(String.Format("{0:0.000000}", (x + xc)) + ", "+String.Format ("{0:0.000000}",(-y + yc))); Console.WriteLine(String.Format("{0:0.000000}", (-x + xc)) +", "+String.Format ("{0:0.000000}",(-y + yc))); // Checking and updating value of // decision parameter based on algorithm if (d1 < 0) { x++; dx = dx + (2 * ry * ry); d1 = d1 + dx + (ry * ry); } else { x++; y--; dx = dx + (2 * ry * ry); dy = dy - (2 * rx * rx); d1 = d1 + dx - dy + (ry * ry); } } // Decision parameter of region 2 d2 = ((ry * ry) * ((x + 0.5f) * (x + 0.5f))) + ((rx * rx) * ((y - 1) * (y - 1))) - (rx * rx * ry * ry); // Plotting points of region 2 while (y >= 0) { // printing points based on 4-way symmetry Console.WriteLine(String.Format("{0:0.000000}", (x + xc)) + ", " + String.Format ("{0:0.000000}",(y + yc))); Console.WriteLine(String.Format("{0:0.000000}", (-x + xc)) + ", "+ String.Format ("{0:0.000000}",(y + yc))); Console.WriteLine(String.Format("{0:0.000000}", (x + xc)) + ", " + String.Format ("{0:0.000000}",(-y + yc))); Console.WriteLine(String.Format("{0:0.000000}", (-x + xc)) + ", " + String.Format ("{0:0.000000}",(-y + yc))); // Checking and updating parameter // value based on algorithm if (d2 > 0) { y--; dy = dy - (2 * rx * rx); d2 = d2 + (rx * rx) - dy; } else { y--; x++; dx = dx + (2 * ry * ry); dy = dy - (2 * rx * rx); d2 = d2 + dx - dy + (rx * rx); } } } // Driver code static void Main() { // To draw a ellipse of major and // minor radius 15, 10 centred at (50, 50) midptellipse(10, 15, 50, 50); } } // This code is contributed by mits |
PHP
<?php // PHP program for implementing // Mid-Point Ellipse Drawing Algorithm function midptellipse($rx, $ry, $xc, $yc) { $x = 0; $y = $ry; // Initial decision parameter of region 1 $d1 = ($ry * $ry) - ($rx * $rx * $ry) + (0.25 * $rx * $rx); $dx = 2 * $ry * $ry * $x; $dy = 2 * $rx * $rx * $y; // For region 1 while ($dx < $dy) { // Print points based on 4-way symmetry echo "( ", $x + $xc, ", ", $y + $yc, " )\n"; echo "( ",-$x + $xc,", ", $y + $yc, " )\n"; echo "( ",$x + $xc,", ", -$y + $yc , " )\n"; echo "( ",-$x + $xc, ", ", -$y + $yc, " )\n"; // Checking and updating value of // decision parameter based on algorithm if ($d1 < 0) { $x++; $dx = $dx + (2 * $ry * $ry); $d1 = $d1 + $dx + ($ry * $ry); } else { $x++; $y--; $dx = $dx + (2 * $ry * $ry); $dy = $dy - (2 * $rx * $rx); $d1 = $d1 + $dx - $dy + ($ry * $ry); } } // Decision parameter of region 2 $d2 = (($ry * $ry) * (($x + 0.5) * ($x + 0.5))) + (($rx * $rx) * (($y - 1) * ($y - 1))) - ($rx * $rx * $ry * $ry); // Plotting points of region 2 while ($y >= 0) { // printing points based on 4-way symmetry echo "( ",$x + $xc,", ", $y + $yc ," )\n"; echo "( ",-$x + $xc,", ", $y + $yc , " )\n"; echo "( ",$x + $xc,", ", -$y + $yc, " )\n"; echo "( ",-$x + $xc,", ", -$y + $yc, " )\n"; // Checking and updating parameter // value based on algorithm if ($d2 > 0) { $y--; $dy = $dy - (2 * $rx * $rx); $d2 = $d2 + ($rx * $rx) - $dy; } else { $y--; $x++; $dx = $dx + (2 * $ry * $ry); $dy = $dy - (2 * $rx * $rx); $d2 = $d2 + $dx - $dy + ($rx * $rx); } } } // Driver code // To draw a ellipse of major and // minor radius 15, 10 centred at (50, 50) midptellipse(10, 15, 50, 50); // This code is contributed by Ryuga ?> |
(50.000000, 65.000000) (50.000000, 65.000000) (50.000000, 35.000000) (50.000000, 35.000000) (51.000000, 65.000000) (49.000000, 65.000000) (51.000000, 35.000000) (49.000000, 35.000000) (52.000000, 65.000000) (48.000000, 65.000000) (52.000000, 35.000000) (48.000000, 35.000000) (53.000000, 64.000000) (47.000000, 64.000000) (53.000000, 36.000000) (47.000000, 36.000000) (54.000000, 64.000000) (46.000000, 64.000000) (54.000000, 36.000000) (46.000000, 36.000000) (55.000000, 63.000000) (45.000000, 63.000000) (55.000000, 37.000000) (45.000000, 37.000000) (56.000000, 62.000000) (44.000000, 62.000000) (56.000000, 38.000000) (44.000000, 38.000000) (57.000000, 61.000000) (43.000000, 61.000000) (57.000000, 39.000000) (43.000000, 39.000000) (57.000000, 60.000000) (43.000000, 60.000000) (57.000000, 40.000000) (43.000000, 40.000000) (58.000000, 59.000000) (42.000000, 59.000000) (58.000000, 41.000000) (42.000000, 41.000000) (58.000000, 58.000000) (42.000000, 58.000000) (58.000000, 42.000000) (42.000000, 42.000000) (59.000000, 57.000000) (41.000000, 57.000000) (59.000000, 43.000000) (41.000000, 43.000000) (59.000000, 56.000000) (41.000000, 56.000000) (59.000000, 44.000000) (41.000000, 44.000000) (59.000000, 55.000000) (41.000000, 55.000000) (59.000000, 45.000000) (41.000000, 45.000000) (60.000000, 54.000000) (40.000000, 54.000000) (60.000000, 46.000000) (40.000000, 46.000000) (60.000000, 53.000000) (40.000000, 53.000000) (60.000000, 47.000000) (40.000000, 47.000000) (60.000000, 52.000000) (40.000000, 52.000000) (60.000000, 48.000000) (40.000000, 48.000000) (60.000000, 51.000000) (40.000000, 51.000000) (60.000000, 49.000000) (40.000000, 49.000000) (60.000000, 50.000000) (40.000000, 50.000000) (60.000000, 50.000000) (40.000000, 50.000000)
Recommended Posts:
- Bresenham's Algorithm for 3-D Line Drawing
- Bresenham’s circle drawing algorithm
- Mid-Point Circle Drawing Algorithm
- Comparions between DDA and Bresenham Line Drawing algorithm
- Find coordinates of the triangle given midpoint of each side
- Draw ellipse in C graphics
- Largest triangle that can be inscribed in an ellipse
- Program to find the Area of an Ellipse
- Check if a point is inside, outside or on the ellipse
- Draw an Ellipse rotating over a Circle in C++ graphics
- Area of Largest rectangle that can be inscribed in an Ellipse
- Area of the biggest ellipse inscribed within a rectangle
- Equation of ellipse from its focus, directrix, and eccentricity
- Area of the Largest square that can be inscribed in an ellipse
- Find the area of largest circle inscribed in ellipse
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.



