Midpoint ellipse drawing algorithm - GeeksforGeeks

19 Dec, 2021
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 2ryFor 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+ry2Else, the next point is (xk+1, yk-1 ) And p1k+1=p1k+2ry2xk+1 – 2rx2yk+1+ry2Obtain 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-rx2ry2At 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+rx2Else, the next point is (xk+1, yk -1) and p2k+1=p2k+2ry2xk+1 -2rx2yk+1+rx2Now obtain the symmetric points in the three quadrants and plot the coordinate value as: x=x+xc, y=y+ycRepeat the steps for region 1 until 2ry2x>=2rx2y
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+ry2Else, 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
Java
Python3
C#
PHP
Javascript
// 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 codeint main(){    // To draw a ellipse of major and    // minor radius 15, 10 centered at (50, 50)    midptellipse(10, 15, 50, 50);     return 0;} // This code is contributed// by Akanksha Rai
// 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 codeint main(){    // To draw a ellipse of major and    // minor radius 15, 10  centered at (50, 50)    midptellipse(10, 15, 50, 50);     return 0;}
// Java program for implementing// Mid-Point Ellipse Drawing Algorithmimport 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 codepublic static void main(String args[]){    // To draw a ellipse of major and    // minor radius 15, 10 centered at (50, 50)    midptellipse(10, 15, 50, 50);}} // This code is contributed by// Surendra_Gangwar
# 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 centered at (50, 50)midptellipse(10, 15, 50, 50); # This code is contributed by chandan_jnu
// C# program for implementing// Mid-Point Ellipse Drawing Algorithmusing 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 codestatic void Main(){     // To draw a ellipse of major and    // minor radius 15, 10 centered at (50, 50)    midptellipse(10, 15, 50, 50);}} // This code is contributed by mits
<?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 centered at (50, 50)midptellipse(10, 15, 50, 50); // This code is contributed by Ryuga?>
<script> // Javascript program for implementing// Mid-Point Ellipse Drawing Algorithmfunction midptellipse(rx, ry, xc, yc){    var 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        document.write("(" + (x + xc).toFixed(5) +                     " , " + (y + yc).toFixed(5) +                       ")" + "<br>");        document.write("(" + (-x + xc).toFixed(5) +                     " , " + (y + yc).toFixed(5) +                       ")" + "<br>");        document.write("(" + (x + xc).toFixed(5) +                     " , " + (-y + yc).toFixed(5) +                       ")" + "<br>");        document.write("(" +  (-x + xc).toFixed(5) +                     " , " + (-y + yc).toFixed(5) +                       ")" + "<br>");         // 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        document.write("(" + (x + xc).toFixed(5) +                     " , " + (y + yc).toFixed(5) +                      " )" + "<br>");        document.write("(" + (-x + xc).toFixed(5) +                     " , " + (y + yc).toFixed(5) +                       ")" + "<br>");        document.write("(" + (x + xc).toFixed(5) +                     " , " + (-y + yc).toFixed(5) +                       ")" + "<br>");        document.write("(" + (-x + xc).toFixed(5) +                     " , " + (-y + yc).toFixed(5) +                       ")" + "<br>");         // 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 centered at (50, 50)midptellipse(10, 15, 50, 50); // This code is contributed by akshitsaxenaa09 </script>
(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)
 
ankthon
SURENDRA_GANGWAR
Mithun Kumar
Akanksha_Rai
Chandan_Kumar
NishantKumar37
akshitsaxenaa09
adnanirshad158
computer-graphics
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Print all possible combinations of r elements in a given array of size n
Find minimum number of coins that make a given value
Program for factorial of a number
Sieve of Eratosthenes
The Knight's tour problem | Backtracking-1
Operators in C / C++