The Wayback Machine - https://web.archive.org/web/20240812195147/https://www.geeksforgeeks.org/mid-point-circle-drawing-algorithm/
Open In App

Mid-Point Circle Drawing Algorithm

Last Updated : 19 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The mid-point circle drawing algorithm is an algorithm used to determine the points needed for rasterizing a circle. 
 

We use the mid-point algorithm to calculate all the perimeter points of the circle in the first octant and then print them along with their mirror points in the other octants. This will work because a circle is symmetric about its centre.

 

Circle octants

 

The algorithm is very similar to the Mid-Point Line Generation Algorithm. Here, only the boundary condition is different.

 

For any given pixel (x, y), the next pixel to be plotted is either (x, y+1) or (x-1, y+1). This can be decided by following the steps below.

 

  1. Find the mid-point p of the two possible pixels i.e (x-0.5, y+1)
  2. If p lies inside or on the circle perimeter, we plot the pixel (x, y+1), otherwise if it’s outside we plot the pixel (x-1, y+1)

Boundary Condition : Whether the mid-point lies inside or outside the circle can be decided by using the formula:- 
 

Given a circle centered at (0,0) and radius r and a point p(x,y) 
F(p) = x2 + y2 – r2 
if F(p)<0, the point is inside the circle
F(p)=0, the point is on the perimeter
F(p)>0, the point is outside the circle 
 

 

example

In our program, we denote F(p) with P. The value of P is calculated at the mid-point of the two contending pixels i.e. (x-0.5, y+1). Each pixel is described with a subscript k.
 

Pk = (Xk — 0.5)2 + (yk + 1)2 – r2 
Now, 
xk+1 = xk or xk-1 , yk+1= yk +1
? Pk+1 = (xk+1 – 0.5)2 + (yk+1 +1)2 – r2 
= (xk+1 – 0.5)2 + [(yk +1) + 1]2 – r2 
= (xk+1 – 0.5)2 + (yk +1)2 + 2(yk + 1) + 1 – r2 
= (xk+1 – 0.5)2 + [ – (xk – 0.5)2 +(xk – 0.5)2 ] + (yk + 1)2 – r2 + 2(yk + 1) + 1
= Pk + (xk+1 – 0.5)2 – (xk – 0.5)2 + 2(yk + 1) + 1 
= Pk + (x2k+1 – x2k) – (xk+1 – xk) + 2(yk + 1) + 1 
= Pk + 2(yk +1) + 1, when Pk <=0 i.e the midpoint is inside the circle 
(xk+1 = xk) 
Pk + 2(yk +1) – 2(xk – 1) + 1, when Pk>0 I.e the mid point is outside the circle(xk+1 = xk-1) 
 

The first point to be plotted is (r, 0) on the x-axis. The initial value of P is calculated as follows:-
 

P1 = (r – 0.5)2 + (0+1)2 – r2 
= 1.25 – r 
= 1 -r (When rounded off)
 

Examples: 
 

Input : Centre -> (0, 0), Radius -> 3
Output : (3, 0) (3, 0) (0, 3) (0, 3)
         (3, 1) (-3, 1) (3, -1) (-3, -1)
         (1, 3) (-1, 3) (1, -3) (-1, -3)
         (2, 2) (-2, 2) (2, -2) (-2, -2)

first point to be plotted

 
Input : Centre -> (4, 4), Radius -> 2
Output : (6, 4) (6, 4) (4, 6) (4, 6)
         (6, 5) (2, 5) (6, 3) (2, 3)
         (5, 6) (3, 6) (5, 2) (3, 2)

CPP




// C++ program for implementing
// Mid-Point Circle Drawing Algorithm
#include<iostream>
using namespace std;
 
// Implementing Mid-Point Circle Drawing Algorithm
void midPointCircleDraw(int x_centre, int y_centre, int r)
{
    int x = r, y = 0;
     
    // Printing the initial point on the axes
    // after translation
    cout << "(" << x + x_centre << ", " << y + y_centre << ") ";
     
    // When radius is zero only a single
    // point will be printed
    if (r > 0)
    {
        cout << "(" << x + x_centre << ", " << -y + y_centre << ") ";
        cout << "(" << y + x_centre << ", " << x + y_centre << ") ";
        cout << "(" << -y + x_centre << ", " << x + y_centre << ")\n";
    }
     
    // Initialising the value of P
    int P = 1 - r;
    while (x > y)
    {
        y++;
         
        // Mid-point is inside or on the perimeter
        if (P <= 0)
            P = P + 2*y + 1;
        // Mid-point is outside the perimeter
        else
        {
            x--;
            P = P + 2*y - 2*x + 1;
        }
         
        // All the perimeter points have already been printed
        if (x < y)
            break;
         
        // Printing the generated point and its reflection
        // in the other octants after translation
        cout << "(" << x + x_centre << ", " << y + y_centre << ") ";
        cout << "(" << -x + x_centre << ", " << y + y_centre << ") ";
        cout << "(" << x + x_centre << ", " << -y + y_centre << ") ";
        cout << "(" << -x + x_centre << ", " << -y + y_centre << ")\n";
         
        // If the generated point is on the line x = y then
        // the perimeter points have already been printed
        if (x != y)
        {
            cout << "(" << y + x_centre << ", " << x + y_centre << ") ";
            cout << "(" << -y + x_centre << ", " << x + y_centre << ") ";
            cout << "(" << y + x_centre << ", " << -x + y_centre << ") ";
            cout << "(" << -y + x_centre << ", " << -x + y_centre << ")\n";
        }
    }
}
 
// Driver code
int main()
{
    // To draw a circle of radius 3 centered at (0, 0)
    midPointCircleDraw(0, 0, 3);
    return 0;
}


C




// C program for implementing
// Mid-Point Circle Drawing Algorithm
#include<stdio.h>
 
// Implementing Mid-Point Circle Drawing Algorithm
void midPointCircleDraw(int x_centre, int y_centre, int r)
{
    int x = r, y = 0;
     
    // Printing the initial point on the axes
    // after translation
    printf("(%d, %d) ", x + x_centre, y + y_centre);
     
    // When radius is zero only a single
    // point will be printed
    if (r > 0)
    {
        printf("(%d, %d) ", x + x_centre, -y + y_centre);
        printf("(%d, %d) ", y + x_centre, x + y_centre);
        printf("(%d, %d)\n", -y + x_centre, x + y_centre);
    }
     
    // Initialising the value of P
    int P = 1 - r;
    while (x > y)
    {
        y++;
         
        // Mid-point is inside or on the perimeter
        if (P <= 0)
            P = P + 2*y + 1;
             
        // Mid-point is outside the perimeter
        else
        {
            x--;
            P = P + 2*y - 2*x + 1;
        }
         
        // All the perimeter points have already been printed
        if (x < y)
            break;
         
        // Printing the generated point and its reflection
        // in the other octants after translation
        printf("(%d, %d) ", x + x_centre, y + y_centre);
        printf("(%d, %d) ", -x + x_centre, y + y_centre);
        printf("(%d, %d) ", x + x_centre, -y + y_centre);
        printf("(%d, %d)\n", -x + x_centre, -y + y_centre);
         
        // If the generated point is on the line x = y then
        // the perimeter points have already been printed
        if (x != y)
        {
            printf("(%d, %d) ", y + x_centre, x + y_centre);
            printf("(%d, %d) ", -y + x_centre, x + y_centre);
            printf("(%d, %d) ", y + x_centre, -x + y_centre);
            printf("(%d, %d)\n", -y + x_centre, -x + y_centre);
        }
    }
}
 
// Driver code
int main()
{
    // To draw a circle of radius 3 centered at (0, 0)
    midPointCircleDraw(0, 0, 3);
    return 0;
}


Java




// Java program for implementing
// Mid-Point Circle Drawing Algorithm
class GFG {
     
    // Implementing Mid-Point Circle
    // Drawing Algorithm
    static void midPointCircleDraw(int x_centre,
                            int y_centre, int r)
    {
         
        int x = r, y = 0;
     
        // Printing the initial point
        // on the axes after translation
        System.out.print("(" + (x + x_centre)
                + ", " + (y + y_centre) + ")");
     
        // When radius is zero only a single
        // point will be printed
        if (r > 0) {
             
            System.out.print("(" + (x + x_centre)
                + ", " + (-y + y_centre) + ")");
                 
            System.out.print("(" + (y + x_centre)
                 + ", " + (x + y_centre) + ")");
                  
            System.out.println("(" + (-y + x_centre)
                   + ", " + (x + y_centre) + ")");
        }
     
        // Initialising the value of P
        int P = 1 - r;
        while (x > y) {
             
            y++;
         
            // Mid-point is inside or on the perimeter
            if (P <= 0)
                P = P + 2 * y + 1;
         
            // Mid-point is outside the perimeter
            else {
                x--;
                P = P + 2 * y - 2 * x + 1;
            }
         
            // All the perimeter points have already
            // been printed
            if (x < y)
                break;
         
            // Printing the generated point and its
            // reflection in the other octants after
            // translation
            System.out.print("(" + (x + x_centre)
                    + ", " + (y + y_centre) + ")");
                     
            System.out.print("(" + (-x + x_centre)
                    + ", " + (y + y_centre) + ")");
                     
            System.out.print("(" + (x + x_centre) +
                    ", " + (-y + y_centre) + ")");
                     
            System.out.println("(" + (-x + x_centre)
                    + ", " + (-y + y_centre) + ")");
         
            // If the generated point is on the
            // line x = y then the perimeter points
            // have already been printed
            if (x != y) {
                 
                System.out.print("(" + (y + x_centre)
                      + ", " + (x + y_centre) + ")");
                       
                System.out.print("(" + (-y + x_centre)
                      + ", " + (x + y_centre) + ")");
                       
                System.out.print("(" + (y + x_centre)
                      + ", " + (-x + y_centre) + ")");
                       
                System.out.println("(" + (-y + x_centre)
                    + ", " + (-x + y_centre) +")");
            }
        }
    }
     
    // Driver code
    public static void main(String[] args) {
         
        // To draw a circle of radius
        // 3 centered at (0, 0)
        midPointCircleDraw(0, 0, 3);
    }
}
 
// This code is contributed by Anant Agarwal.


Python3




# Python3 program for implementing
# Mid-Point Circle Drawing Algorithm
 
def midPointCircleDraw(x_centre, y_centre, r):
    x = r
    y = 0
     
    # Printing the initial point the
    # axes after translation
    print("(", x + x_centre, ", ",
               y + y_centre, ")",
               sep = "", end = "")
     
    # When radius is zero only a single
    # point be printed
    if (r > 0) :
     
        print("(", x + x_centre, ", ",
                  -y + y_centre, ")",
                  sep = "", end = "")
        print("(", y + x_centre, ", ",
                   x + y_centre, ")",
                   sep = "", end = "")
        print("(", -y + x_centre, ", ",
                    x + y_centre, ")", sep = "")
     
    # Initialising the value of P
    P = 1 - r
 
    while x > y:
     
        y += 1
         
        # Mid-point inside or on the perimeter
        if P <= 0:
            P = P + 2 * y + 1
             
        # Mid-point outside the perimeter
        else:        
            x -= 1
            P = P + 2 * y - 2 * x + 1
         
        # All the perimeter points have
        # already been printed
        if (x < y):
            break
         
        # Printing the generated point its reflection
        # in the other octants after translation
        print("(", x + x_centre, ", ", y + y_centre,
                            ")", sep = "", end = "")
        print("(", -x + x_centre, ", ", y + y_centre,
                             ")", sep = "", end = "")
        print("(", x + x_centre, ", ", -y + y_centre,
                             ")", sep = "", end = "")
        print("(", -x + x_centre, ", ", -y + y_centre,
                                        ")", sep = "")
         
        # If the generated point on the line x = y then
        # the perimeter points have already been printed
        if x != y:
         
            print("(", y + x_centre, ", ", x + y_centre,
                                ")", sep = "", end = "")
            print("(", -y + x_centre, ", ", x + y_centre,
                                 ")", sep = "", end = "")
            print("(", y + x_centre, ", ", -x + y_centre,
                                 ")", sep = "", end = "")
            print("(", -y + x_centre, ", ", -x + y_centre,
                                            ")", sep = "")
                             
# Driver Code
if __name__ == '__main__':
     
    # To draw a circle of radius 3
    # centered at (0, 0)
    midPointCircleDraw(0, 0, 3)
 
 
# Contributed by: SHUBHAMSINGH10
# Improved by: siddharthx_07


C#




// C# program for implementing Mid-Point
// Circle Drawing Algorithm
using System;
 
class GFG {
     
    // Implementing Mid-Point Circle
    // Drawing Algorithm
    static void midPointCircleDraw(int x_centre,
                            int y_centre, int r)
    {
         
        int x = r, y = 0;
     
        // Printing the initial point on the
        // axes after translation
        Console.Write("(" + (x + x_centre)
                + ", " + (y + y_centre) + ")");
     
        // When radius is zero only a single
        // point will be printed
        if (r > 0)
        {
             
            Console.Write("(" + (x + x_centre)
                + ", " + (-y + y_centre) + ")");
                 
            Console.Write("(" + (y + x_centre)
                + ", " + (x + y_centre) + ")");
                 
            Console.WriteLine("(" + (-y + x_centre)
                + ", " + (x + y_centre) + ")");
        }
     
        // Initialising the value of P
        int P = 1 - r;
        while (x > y)
        {
             
            y++;
         
            // Mid-point is inside or on the perimeter
            if (P <= 0)
                P = P + 2 * y + 1;
         
            // Mid-point is outside the perimeter
            else
            {
                x--;
                P = P + 2 * y - 2 * x + 1;
            }
         
            // All the perimeter points have already
            // been printed
            if (x < y)
                break;
         
            // Printing the generated point and its
            // reflection in the other octants after
            // translation
            Console.Write("(" + (x + x_centre)
                    + ", " + (y + y_centre) + ")");
                     
            Console.Write("(" + (-x + x_centre)
                    + ", " + (y + y_centre) + ")");
                     
            Console.Write("(" + (x + x_centre) +
                    ", " + (-y + y_centre) + ")");
                     
            Console.WriteLine("(" + (-x + x_centre)
                    + ", " + (-y + y_centre) + ")");
         
            // If the generated point is on the
            // line x = y then the perimeter points
            // have already been printed
            if (x != y)
            {
                Console.Write("(" + (y + x_centre)
                    + ", " + (x + y_centre) + ")");
                         
                Console.Write("(" + (-y + x_centre)
                    + ", " + (x + y_centre) + ")");
                         
                Console.Write("(" + (y + x_centre)
                    + ", " + (-x + y_centre) + ")");
                         
                Console.WriteLine("(" + (-y + x_centre)
                    + ", " + (-x + y_centre) +")");
            }
        }
    }
     
    // Driver code
    public static void Main()
    {
         
        // To draw a circle of radius
        // 3 centered at (0, 0)
        midPointCircleDraw(0, 0, 3);
    }
}
 
// This code is contributed by nitin mittal.


PHP




<?php
// PHP program for implementing
// Mid-Point Circle Drawing Algorithm
 
// Implementing Mid-Point
// Circle Drawing Algorithm
function midPointCircleDraw($x_centre,
                            $y_centre,
                            $r)
{
    $x = $r;
    $y = 0;
     
    // Printing the initial
    // point on the axes
    // after translation
    echo "(",$x + $x_centre,",", $y + $y_centre,")";
     
    // When radius is zero only a single
    // point will be printed
    if ($r > 0)
    {
        echo "(",$x + $x_centre,",", -$y + $y_centre,")";
        echo "(",$y + $x_centre,",", $x + $y_centre,")";
        echo "(",-$y + $x_centre,",", $x + $y_centre,")","\n";
    }
     
    // Initializing the value of P
    $P = 1 - $r;
    while ($x > $y)
    {
        $y++;
         
        // Mid-point is inside
        // or on the perimeter
        if ($P <= 0)
            $P = $P + 2 * $y + 1;
             
        // Mid-point is outside
        // the perimeter
        else
        {
            $x--;
            $P = $P + 2 * $y -
                  2 * $x + 1;
        }
         
        // All the perimeter points
        // have already been printed
        if ($x < $y)
            break;
         
        // Printing the generated
        // point and its reflection
        // in the other octants
        // after translation
        echo "(",$x + $x_centre,",", $y + $y_centre,")";
        echo "(",-$x + $x_centre,",", $y + $y_centre,")";
        echo "(",$x +$x_centre,",", -$y + $y_centre,")";
        echo "(",-$x + $x_centre,",", -$y + $y_centre,")","\n";
         
        // If the generated point is
        // on the line x = y then
        // the perimeter points have
        // already been printed
        if ($x != $y)
        {
            echo "(",$y + $x_centre,",", $x + $y_centre,")";
            echo "(",-$y + $x_centre,",", $x + $y_centre,")";
            echo "(",$y + $x_centre,",", -$x + $y_centre,")";
            echo "(",-$y + $x_centre,",", -$x + $y_centre,")","\n";
        }
    }
}
 
    // Driver code
    // To draw a circle of radius
    // 3 centered at (0, 0)
    midPointCircleDraw(0, 0, 3);
     
// This code is contributed by nitin mittal.
?>


Javascript




<script>
// javascript program for implementing
// Mid-Point Circle Drawing Algorithm   
// Implementing Mid-Point Circle
    // Drawing Algorithm
    function midPointCircleDraw(x_centre , y_centre , r) {
 
        var x = r, y = 0;
 
        // Printing the initial point
        // on the axes after translation
        document.write("(" + (x + x_centre) + ", " + (y + y_centre) + ")");
 
        // When radius is zero only a single
        // point will be printed
        if (r > 0) {
 
            document.write("(" + (x + x_centre) + ", " + (-y + y_centre) + ")");
 
            document.write("(" + (y + x_centre) + ", " + (x + y_centre) + ")");
 
            document.write("(" + (-y + x_centre) + ", " + (x + y_centre) + ")<br/>");
        }
 
        // Initialising the value of P
        var P = 1 - r;
        while (x > y) {
 
            y++;
 
            // Mid-point is inside or on the perimeter
            if (P <= 0)
                P = P + 2 * y + 1;
 
            // Mid-point is outside the perimeter
            else {
                x--;
                P = P + 2 * y - 2 * x + 1;
            }
 
            // All the perimeter points have already
            // been printed
            if (x < y)
                break;
 
            // Printing the generated point and its
            // reflection in the other octants after
            // translation
            document.write("(" + (x + x_centre) + ", " + (y + y_centre) + ")");
 
            document.write("(" + (-x + x_centre) + ", " + (y + y_centre) + ")");
 
            document.write("(" + (x + x_centre) + ", " + (-y + y_centre) + ")");
 
            document.write("(" + (-x + x_centre) + ", " + (-y + y_centre) + ")<br/>");
 
            // If the generated point is on the
            // line x = y then the perimeter points
            // have already been printed
            if (x != y) {
 
                document.write("(" + (y + x_centre) + ", " + (x + y_centre) + ")");
 
                document.write("(" + (-y + x_centre) + ", " + (x + y_centre) + ")");
 
                document.write("(" + (y + x_centre) + ", " + (-x + y_centre) + ")");
 
                document.write("(" + (-y + x_centre) + ", " + (-x + y_centre) + ")<br/>");
            }
        }
    }
 
    // Driver code
     
 
        // To draw a circle of radius
        // 3 centered at (0, 0)
        midPointCircleDraw(0, 0, 3);
 
// This code is contributed by umadevi9616
</script>


Output: 
 

(3, 0) (3, 0) (0, 3) (0, 3)
(3, 1) (-3, 1) (3, -1) (-3, -1)
(1, 3) (-1, 3) (1, -3) (-1, -3)
(2, 2) (-2, 2) (2, -2) (-2, -2)

Time Complexity: O(x – y)
Auxiliary Space: O(1) 
References : Midpoint Circle Algorithm 
Image References : Octants of a circle, Rasterised Circle, the other images were created for this article by the geek
Thanks Tuhina Singh and Teva Zanker for improving this article. 

 



Previous Article
Next Article

Similar Reads

Scan conversion methods of circle and circle generation algorithms
Scan conversion of circle : A circle is defined as a set of points that all are the same distance from a common point.The Centre of the circle is known as the Centre, and the radius is the distance from the Centre of the circle to any point on its circumference.It's an eight-sided symmetrical figure divided into four quadrants. By knowing only one
3 min read
Rotation of a point about another point
We have already discussed the rotation of a point P about the origin in the Set 1 and Set 2. The rotation of point P about origin with an angle ? in the anti-clockwise direction is given as under: Rotation of P about origin: P * polar(1.0, ?)Rotation of P about point Q Now, we have to rotate the point P not about origin but about a general point Q.
8 min read
Scan conversion of Line and Line Drawing algorithms
SCAN Conversion of Line : A line connects two points.It is a basic element in graphics.You'll need two spots between which to draw a line to draw a line . A line, or line segment, can be uniquely described by two points, according to geometry. We also know from algebra that a line can be defined by a slope, commonly denoted by the letter m, and a y
4 min read
Draw circle in C graphics
The header file graphics.h contains circle() function which draws a circle with center at (x, y) and given radius. Syntax : circle(x, y, radius); where, (x, y) is center of the circle. 'radius' is the Radius of the circle. Examples : Input : x = 250, y = 200, radius = 50 Output : Input : x = 300, y = 150, radius = 90 Output : Below is the implement
1 min read
Check if a given circle lies completely inside the ring formed by two concentric circles
Given two circles of radius r and R, both have their centre at the origin. Now, given another circle of radius r1 and centre at (x1, y1). Check, if the third circle(circle of radius r1) lies completely inside the ring formed by two circles of radius r and R.Examples : Input : r = 8 R = 4 r1 = 2 x1 = 6 y1 = 0 Output : yes Input : r = 8 R = 4 r1 = 2
6 min read
Program to calculate area of an Circle inscribed in a Square
Given the side of a square. The task is to find the area of an inscribed circle in a square.Examples: Input : a = 8 Output : Area of an inscribed circle: 50.24 Input : a = 12.04 Output : Area of an inscribed circle: 113.795 Given a square i.e. all sides of a square are of equal length and all four angles are 90 degrees. Below diagram depicts an ins
4 min read
Distributing M items in a circle of size N starting from K-th position
M items are to be delivered in a circle of size N. Find the position where the M-th item will be delivered if we start from a given position K. Note that items are distributed at adjacent positions starting from K. Examples : Input : N = 5 // Size of circle M = 2 // Number of items K = 1 // Starting position Output : 2 The first item will be given
7 min read
Circle Sort
Circle sort algorithm can be visualized by drawing concentric circles on an array of integers. The elements of the array lying on the same circle diametrically opposite to each other are compared and if found in the wrong order they are swapped. This goes on in a recursive fashion in which the array is divided into sub-arrays on which the above pro
8 min read
iOS Screen Lock Passcode: A Blessing or Single Point of Failure?
In this article, we will zoom on a quirk of the iOS architecture that narrows down the range of data security options to a single, purportedly insurmountable barrier – the screen lock passcode. This is what nearly all the other protection mechanisms currently depend on. Apple appears to follow some complex unspoken schedule rolling out iPhone tweak
11 min read
Find a Fixed Point (Value equal to index) in a given array | Duplicates Allowed
Given an array of n integers sorted in ascending order, write a function that returns a Fixed Point in the array, if there is any Fixed Point present in the array, else returns -1. Fixed Point in an array is an index i such that arr[i] is equal to i. Note that integers in the array can be negative. Examples: Input: arr[] = {-10, -5, 0, 3, 7} Output
13 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg