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

Ellipse(4 way symmetry)

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 :

  1. Take input radius along x axis and y axis and obtain center of ellipse.
  2. Initially, we assume ellipse to be centered at origin and the first point as : (x, y0)= (0, ry).
  3. Obtain the initial decision parameter for region 1 as: p10=ry2+1/4rx2-rx 2ry
  4. 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

  5. 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
  6. 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

  7. Else, the next point is (xk+1, yk -1) and p2k+1=p2k+2ry2xk+1 -2rx2yk+1+rx2
  8. Now obtain the symmetric points in the three quadrants and plot the coordinate value as: x=x+xc, y=y+yc
  9. Repeat the steps for region 1 until 2ry2x&gt=2rx2y

Implementation:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

(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)


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.