Dda Circle Drawing Algorithm in Cpp

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

We apply the mid-point algorithm to calculate all the perimeter points of the circumvolve in the commencement octant and and so print them along with their mirror points in the other octants. This will work because a circle is symmetric virtually its centre.

Circle octants

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

For any given pixel (ten, y), the side by side pixel to be plotted is either (x, y+one) or (ten-1, y+1). This can be decided past following the steps beneath.

  1. Find the mid-bespeak p of the 2 possible pixels i.e (10-0.5, y+1)
  2. If p lies within 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-betoken lies inside or exterior the circle can be decided by using the formula:-

Given a circumvolve centered at (0,0) and radius r and a bespeak p(x,y)
F(p) = xtwo + y2 – rtwo
if F(p)<0, the signal is within the circle
F(p)=0, the bespeak is on the perimeter
F(p)>0, the point is outside the circumvolve

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 thou.

Pone thousand = (Xchiliad — 0.5)2 + (yk + one)2 – r2
Now,
xk+1 = tenyard or 10k-1 , ychiliad+ane= yk +ane
∴ Pthou+1 = (10grand+1 – 0.5)2 + (yg+1 +one)ii – r2
= (xk+ane – 0.5)ii + [(yyard +one) + one]2 – r2
= (xk+one – 0.5)two + (yyard +1)2 + ii(yyard + i) + 1 – r2
= (xk+1 – 0.5)ii + [ – (xk – 0.5)two +(xone thousand – 0.5)ii ] + (yone thousand + one)ii – r2 + ii(yk + 1) + 1
= Pg + (xk+ane – 0.5)2 – (xk – 0.five)2 + 2(yk + 1) + i
= Pone thousand + (102 thou+1 – x2 k) – (x1000+one – xk) + 2(yk + one) + ane
= P1000 + 2(yk +one) + 1, when Pk <=0 i.e the midpoint is inside the circle
(xk+one = xk)
Pk + ii(yk +1) – two(xgrand – 1) + 1, when Pchiliad>0 I.e the mid point is exterior the circumvolve(xk+ane = tenk-1)

The first indicate to exist plotted is (r, 0) on the x-centrality. The initial value of P is calculated as follows:-

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

Examples:

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

first point to be plotted

        
          Input :          Centre -> (4, four), Radius -> 2          Output :          (six, four) (vi, 4) (iv, half-dozen) (4, half dozen)          (6, v) (2, 5) (6, 3) (2, iii)          (5, half dozen) (3, 6) (five, 2) (3, two)

CPP

#include<iostream>

using namespace std;

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

{

int ten = r, y = 0;

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

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" ;

}

int P = 1 - r;

while (x > y)

{

y++;

if (P <= 0)

P = P + 2*y + 1;

else

{

x--;

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

}

if (10 < y)

interruption ;

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 << ")\n" ;

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 << ", " << -10 + y_centre << ")\n" ;

}

}

}

int main()

{

midPointCircleDraw(0, 0, 3);

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

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

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

}

int P = one - r;

while (ten > y)

{

y++;

if (P <= 0)

P = P + 2*y + 1;

else

{

10--;

P = P + two*y - 2*ten + one;

}

if (x < y)

interruption ;

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

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

printf ( "(%d, %d) " , ten + 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)\n" , -y + x_centre, -10 + y_centre);

}

}

}

int main()

{

midPointCircleDraw(0, 0, 3);

return 0;

}

Java

class GFG {

static void midPointCircleDraw( int x_centre,

int y_centre, int r)

{

int ten = r, y = 0 ;

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

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

if (r > 0 ) {

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

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

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

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

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

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

}

int P = 1 - r;

while (x > y) {

y++;

if (P <= 0 )

P = P + ii * y + 1 ;

else {

x--;

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

}

if (x < y)

interruption ;

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

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

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

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

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

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

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

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

if (ten != y) {

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

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

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

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

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

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

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

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

}

}

}

public static void main(String[] args) {

midPointCircleDraw( 0 , 0 , iii );

}

}

Python3

def midPointCircleDraw(x_centre, y_centre, r):

ten = r

y = 0

print ( "(" , x + x_centre, ", " ,

y + y_centre, ")" ,

sep = " ", end = " ")

if (r > 0 ) :

print ( "(" , x + x_centre, ", " ,

- y + y_centre, ")" ,

sep = " ", end = " ")

impress ( "(" , y + x_centre, ", " ,

x + y_centre, ")" ,

sep = " ", cease = " ")

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

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

P = ane - r

while x > y:

y + = one

if P < = 0 :

P = P + 2 * y + 1

else :

10 - = 1

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

if (x < y):

break

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

")" , sep = " ", stop = " ")

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

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

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

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

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

")" , sep = "")

if 10 ! = y:

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

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

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

")" , sep = " ", stop = " ")

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

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

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

")" , sep = "")

if __name__ = = '__main__' :

midPointCircleDraw( 0 , 0 , 3 )

C#

using System;

class GFG {

static void midPointCircleDraw( int x_centre,

int y_centre, int r)

{

int x = r, y = 0;

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

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

if (r > 0)

{

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

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

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

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

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

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

}

int P = 1 - r;

while (x > y)

{

y++;

if (P <= 0)

P = P + two * y + one;

else

{

x--;

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

}

if (x < y)

intermission ;

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

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

Console.Write( "(" + (-x + x_centre)

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

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

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

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

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

if (x != y)

{

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

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

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

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

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

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

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

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

}

}

}

public static void Principal()

{

midPointCircleDraw(0, 0, 3);

}

}

PHP

<?php

function midPointCircleDraw( $x_centre ,

$y_centre ,

$r )

{

$10 = $r ;

$y = 0;

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

if ( $r > 0)

{

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

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

echo "(" ,- $y + $x_centre , "," , $ten + $y_centre , ")" , "\due north" ;

}

$P = 1 - $r ;

while ( $x > $y )

{

$y ++;

if ( $P <= 0)

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

else

{

$x --;

$P = $P + two * $y -

2 * $10 + 1;

}

if ( $x < $y )

intermission ;

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

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

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

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

if ( $x != $y )

{

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

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

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

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

}

}

}

midPointCircleDraw(0, 0, 3);

?>

Javascript

<script>

role midPointCircleDraw(x_centre , y_centre , r) {

var x = r, y = 0;

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

if (r > 0) {

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

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

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

}

var P = 1 - r;

while (x > y) {

y++;

if (P <= 0)

P = P + two * y + 1;

else {

10--;

P = P + two * y - 2 * 10 + 1;

}

if (x < y)

break ;

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

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

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

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

if (x != y) {

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

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

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

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

}

}

}

midPointCircleDraw(0, 0, 3);

</script>

Output:

(3, 0) (3, 0) (0, 3) (0, iii) (3, 1) (-three, 1) (three, -one) (-3, -ane) (1, 3) (-one, three) (1, -three) (-ane, -three) (two, 2) (-2, two) (2, -2) (-two, -2)

Fourth dimension 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.
This article is contributed by Nabaneet Roy. If yous like GeeksforGeeks and would like to contribute, you lot can too write an article using write.geeksforgeeks.org or postal service your article to review-team@geeksforgeeks.org. Run into your article appearing on the GeeksforGeeks main folio and help other Geeks.
Delight write comments if you discover anything wrong, or y'all desire to share more information most the topic discussed higher up.


bowieseliestionce.blogspot.com

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

0 Response to "Dda Circle Drawing Algorithm in Cpp"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel