Mid Point Circle Drawing Algorithm

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

Nosotros apply the mid-point algorithm to calculate all the perimeter points of the circumvolve in the beginning 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-Indicate Line Generation Algorithm. Here, only the boundary condition is different.

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

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

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

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

example

In our program, nosotros announce F(p) with P. The value of P is calculated at the mid-point of the two contending pixels i.due east. (ten-0.five, y+1). Each pixel is described with a subscript k.

Pthou = (Tenk — 0.five)ii + (yk + 1)two – r2
Now,
xk+1 = xm or xk-ane , ythousand+1= ythou +1
∴ Pk+one = (xk+one – 0.5)2 + (ygrand+1 +one)ii – r2
= (xgrand+one – 0.5)ii + [(yk +1) + 1]2 – rtwo
= (10thou+ane – 0.five)2 + (yk +1)2 + ii(yk + 1) + 1 – r2
= (xk+1 – 0.v)2 + [ – (xg – 0.5)2 +(xk – 0.5)ii ] + (yk + i)2 – r2 + two(yk + ane) + ane
= Pthou + (xk+1 – 0.5)2 – (xk – 0.five)2 + 2(yk + 1) + ane
= Pg + (tenii k+1 – x2 k) – (tenk+ane – xk) + ii(y1000 + ane) + 1
= Pk + 2(yk +1) + ane, when Pk <=0 i.due east the midpoint is inside the circle
(xk+1 = xk)
Pk + 2(yg +one) – 2(xg – 1) + 1, when Pk>0 I.e the mid signal is outside the circumvolve(tenk+1 = xk-ane)

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

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

Examples:

          Input :          Centre -> (0, 0), Radius -> 3          Output :          (iii, 0) (3, 0) (0, iii) (0, 3)          (three, 1) (-3, ane) (3, -ane) (-3, -1)          (one, iii) (-1, iii) (1, -three) (-1, -3)          (2, 2) (-2, two) (two, -ii) (-2, -two)

first point to be plotted

        
          Input :          Centre -> (4, iv), Radius -> ii          Output :          (half-dozen, 4) (6, iv) (4, vi) (4, half-dozen)          (vi, 5) (ii, 5) (6, iii) (2, 3)          (5, half-dozen) (3, 6) (5, 2) (iii, 2)

CPP

#include<iostream>

using namespace std;

void midPointCircleDraw( int x_centre, int y_centre, int r)

{

int ten = r, y = 0;

cout << "(" << ten + x_centre << ", " << y + y_centre << ") " ;

if (r > 0)

{

cout << "(" << x + x_centre << ", " << -y + y_centre << ") " ;

cout << "(" << y + x_centre << ", " << 10 + y_centre << ") " ;

cout << "(" << -y + x_centre << ", " << 10 + y_centre << ")\n" ;

}

int P = 1 - r;

while (10 > y)

{

y++;

if (P <= 0)

P = P + 2*y + one;

else

{

x--;

P = P + ii*y - two*x + 1;

}

if (x < y)

break ;

cout << "(" << x + x_centre << ", " << y + y_centre << ") " ;

cout << "(" << -10 + x_centre << ", " << y + y_centre << ") " ;

cout << "(" << x + x_centre << ", " << -y + y_centre << ") " ;

cout << "(" << -x + x_centre << ", " << -y + y_centre << ")\northward" ;

if (x != y)

{

cout << "(" << y + x_centre << ", " << x + y_centre << ") " ;

cout << "(" << -y + x_centre << ", " << 10 + y_centre << ") " ;

cout << "(" << y + x_centre << ", " << -ten + y_centre << ") " ;

cout << "(" << -y + x_centre << ", " << -x + y_centre << ")\n" ;

}

}

}

int chief()

{

midPointCircleDraw(0, 0, iii);

return 0;

}

C

#include<stdio.h>

void midPointCircleDraw( int x_centre, int y_centre, int r)

{

int x = r, y = 0;

printf ( "(%d, %d) " , x + x_centre, y + y_centre);

if (r > 0)

{

printf ( "(%d, %d) " , x + x_centre, -y + y_centre);

printf ( "(%d, %d) " , y + x_centre, ten + y_centre);

printf ( "(%d, %d)\n" , -y + x_centre, 10 + y_centre);

}

int P = one - r;

while (10 > y)

{

y++;

if (P <= 0)

P = P + 2*y + 1;

else

{

x--;

P = P + 2*y - 2*x + 1;

}

if (x < y)

break ;

printf ( "(%d, %d) " , x + x_centre, y + y_centre);

printf ( "(%d, %d) " , -x + x_centre, y + y_centre);

printf ( "(%d, %d) " , 10 + x_centre, -y + y_centre);

printf ( "(%d, %d)\n" , -x + x_centre, -y + y_centre);

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)\due north" , -y + x_centre, -ten + y_centre);

}

}

}

int main()

{

midPointCircleDraw(0, 0, 3);

render 0;

}

Coffee

class GFG {

static void midPointCircleDraw( int x_centre,

int y_centre, int r)

{

int x = r, y = 0 ;

System.out.impress( "(" + (10 + x_centre)

+ ", " + (y + y_centre) + ")" );

if (r > 0 ) {

System.out.impress( "(" + (x + x_centre)

+ ", " + (-y + y_centre) + ")" );

System.out.impress( "(" + (y + x_centre)

+ ", " + (x + y_centre) + ")" );

System.out.println( "(" + (-y + x_centre)

+ ", " + (ten + y_centre) + ")" );

}

int P = 1 - r;

while (x > y) {

y++;

if (P <= 0 )

P = P + ii * y + 1 ;

else {

x--;

P = P + ii * y - two * x + 1 ;

}

if (10 < y)

pause ;

Organisation.out.print( "(" + (10 + x_centre)

+ ", " + (y + y_centre) + ")" );

System.out.impress( "(" + (-x + x_centre)

+ ", " + (y + y_centre) + ")" );

System.out.print( "(" + (x + x_centre) +

", " + (-y + y_centre) + ")" );

Organization.out.println( "(" + (-x + x_centre)

+ ", " + (-y + y_centre) + ")" );

if (x != y) {

System.out.print( "(" + (y + x_centre)

+ ", " + (ten + y_centre) + ")" );

System.out.print( "(" + (-y + x_centre)

+ ", " + (x + y_centre) + ")" );

System.out.print( "(" + (y + x_centre)

+ ", " + (-10 + y_centre) + ")" );

System.out.println( "(" + (-y + x_centre)

+ ", " + (-x + y_centre) + ")" );

}

}

}

public static void main(Cord[] args) {

midPointCircleDraw( 0 , 0 , three );

}

}

Python3

def midPointCircleDraw(x_centre, y_centre, r):

ten = r

y = 0

print ( "(" , ten + x_centre, ", " ,

y + y_centre, ")" ,

sep = " ", end = " ")

if (r > 0 ) :

impress ( "(" , 10 + x_centre, ", " ,

- y + y_centre, ")" ,

sep = " ", end = " ")

print ( "(" , y + x_centre, ", " ,

x + y_centre, ")" ,

sep = " ", end = " ")

print ( "(" , - y + x_centre, ", " ,

x + y_centre, ")" , sep = "")

P = 1 - r

while 10 > y:

y + = i

if P < = 0 :

P = P + 2 * y + 1

else :

x - = 1

P = P + 2 * y - two * 10 + ane

if (ten < y):

suspension

print ( "(" , x + x_centre, ", " , y + y_centre,

")" , sep = " ", cease = " ")

print ( "(" , - ten + x_centre, ", " , y + y_centre,

")" , sep = " ", end = " ")

print ( "(" , x + x_centre, ", " , - y + y_centre,

")" , sep = " ", end = " ")

print ( "(" , - ten + x_centre, ", " , - y + y_centre,

")" , sep = "")

if x ! = y:

print ( "(" , y + x_centre, ", " , x + y_centre,

")" , sep = " ", terminate = " ")

print ( "(" , - y + x_centre, ", " , x + y_centre,

")" , sep = " ", end = " ")

print ( "(" , y + x_centre, ", " , - x + y_centre,

")" , sep = " ", end = " ")

impress ( "(" , - y + x_centre, ", " , - ten + y_centre,

")" , sep = "")

if __name__ = = '__main__' :

midPointCircleDraw( 0 , 0 , three )

C#

using System;

class GFG {

static void midPointCircleDraw( int x_centre,

int y_centre, int r)

{

int ten = r, y = 0;

Console.Write( "(" + (10 + x_centre)

+ ", " + (y + y_centre) + ")" );

if (r > 0)

{

Panel.Write( "(" + (ten + x_centre)

+ ", " + (-y + y_centre) + ")" );

Panel.Write( "(" + (y + x_centre)

+ ", " + (x + y_centre) + ")" );

Panel.WriteLine( "(" + (-y + x_centre)

+ ", " + (ten + y_centre) + ")" );

}

int P = 1 - r;

while (10 > y)

{

y++;

if (P <= 0)

P = P + two * y + 1;

else

{

ten--;

P = P + 2 * y - 2 * 10 + i;

}

if (10 < y)

intermission ;

Console.Write( "(" + (ten + 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 (10 != y)

{

Console.Write( "(" + (y + x_centre)

+ ", " + (ten + y_centre) + ")" );

Panel.Write( "(" + (-y + x_centre)

+ ", " + (x + y_centre) + ")" );

Console.Write( "(" + (y + x_centre)

+ ", " + (-x + y_centre) + ")" );

Console.WriteLine( "(" + (-y + x_centre)

+ ", " + (-x + y_centre) + ")" );

}

}

}

public static void Main()

{

midPointCircleDraw(0, 0, 3);

}

}

PHP

<?php

part midPointCircleDraw( $x_centre ,

$y_centre ,

$r )

{

$x = $r ;

$y = 0;

echo "(" , $x + $x_centre , "," , $y + $y_centre , ")" ;

if ( $r > 0)

{

echo "(" , $10 + $x_centre , "," , - $y + $y_centre , ")" ;

repeat "(" , $y + $x_centre , "," , $x + $y_centre , ")" ;

echo "(" ,- $y + $x_centre , "," , $10 + $y_centre , ")" , "\n" ;

}

$P = 1 - $r ;

while ( $x > $y )

{

$y ++;

if ( $P <= 0)

$P = $P + 2 * $y + 1;

else

{

$10 --;

$P = $P + 2 * $y -

2 * $x + ane;

}

if ( $x < $y )

interruption ;

echo "(" , $x + $x_centre , "," , $y + $y_centre , ")" ;

echo "(" ,- $x + $x_centre , "," , $y + $y_centre , ")" ;

echo "(" , $x + $x_centre , "," , - $y + $y_centre , ")" ;

repeat "(" ,- $x + $x_centre , "," , - $y + $y_centre , ")" , "\n" ;

if ( $x != $y )

{

repeat "(" , $y + $x_centre , "," , $10 + $y_centre , ")" ;

repeat "(" ,- $y + $x_centre , "," , $ten + $y_centre , ")" ;

repeat "(" , $y + $x_centre , "," , - $10 + $y_centre , ")" ;

echo "(" ,- $y + $x_centre , "," , - $x + $y_centre , ")" , "\n" ;

}

}

}

midPointCircleDraw(0, 0, 3);

?>

Javascript

<script>

function midPointCircleDraw(x_centre , y_centre , r) {

var x = r, y = 0;

document.write( "(" + (x + x_centre) + ", " + (y + y_centre) + ")" );

if (r > 0) {

certificate.write( "(" + (x + x_centre) + ", " + (-y + y_centre) + ")" );

certificate.write( "(" + (y + x_centre) + ", " + (x + y_centre) + ")" );

certificate.write( "(" + (-y + x_centre) + ", " + (x + y_centre) + ")<br/>" );

}

var P = i - r;

while (x > y) {

y++;

if (P <= 0)

P = P + two * y + one;

else {

ten--;

P = P + 2 * y - 2 * ten + 1;

}

if (x < y)

interruption ;

document.write( "(" + (x + x_centre) + ", " + (y + y_centre) + ")" );

document.write( "(" + (-x + x_centre) + ", " + (y + y_centre) + ")" );

certificate.write( "(" + (ten + x_centre) + ", " + (-y + y_centre) + ")" );

certificate.write( "(" + (-10 + x_centre) + ", " + (-y + y_centre) + ")<br/>" );

if (x != y) {

document.write( "(" + (y + x_centre) + ", " + (x + y_centre) + ")" );

document.write( "(" + (-y + x_centre) + ", " + (ten + y_centre) + ")" );

document.write( "(" + (y + x_centre) + ", " + (-x + y_centre) + ")" );

document.write( "(" + (-y + x_centre) + ", " + (-ten + y_centre) + ")<br/>" );

}

}

}

midPointCircleDraw(0, 0, 3);

</script>

Output:

(3, 0) (3, 0) (0, 3) (0, 3) (3, 1) (-3, ane) (iii, -ane) (-3, -1) (1, 3) (-1, 3) (1, -iii) (-1, -3) (ii, 2) (-2, 2) (ii, -ii) (-2, -2)

Time Complexity: O(ten – y)
Auxiliary Space: O(one)
References : Midpoint Circle Algorithm
Epitome References : Octants of a circle, Rasterised Circumvolve, the other images were created for this commodity by the geek
Thanks Tuhina Singh and Teva Zanker for improving this commodity.
This article is contributed by Nabaneet Roy. If you lot similar GeeksforGeeks and would like to contribute, y'all can besides write an article using write.geeksforgeeks.org or postal service your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks master page and help other Geeks.
Delight write comments if you lot notice annihilation wrong, or you want to share more information nigh the topic discussed above.


huskeycourriund.blogspot.com

Source: https://www.geeksforgeeks.org/mid-point-circle-drawing-algorithm/

0 Response to "Mid Point Circle Drawing Algorithm"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel