Draw a Circle With Radius 20 Around Origin

The mid-betoken circle cartoon algorithm is an algorithm used to make up one's mind the points needed for rasterizing a circumvolve.

We employ the mid-point algorithm to calculate all the perimeter points of the circle in the first octant and and then print them along with their mirror points in the other octants. This volition work considering a circumvolve is symmetric virtually its centre.

Circle octants

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

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

  1. Notice the mid-point p of the 2 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+ane), otherwise if it'due south outside we plot the pixel (10-ane, y+1)

Purlieus Condition : Whether the mid-point lies inside or exterior the circle tin can be decided by using the formula:-

Given a circumvolve centered at (0,0) and radius r and a indicate p(x,y)
F(p) = 10two + 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 betoken is exterior the circle

example

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

Pk = (Xk — 0.v)2 + (ym + 1)2 – rtwo
At present,
tenk+ane = tenyard or xk-ane , yg+1= yg +1
∴ Pthou+ane = (10k+1 – 0.five)2 + (ythousand+1 +1)2 – r2
= (10k+one – 0.five)2 + [(yk +1) + ane]two – r2
= (xchiliad+1 – 0.5)ii + (yk +1)2 + 2(ythousand + one) + ane – r2
= (10thou+i – 0.5)2 + [ – (xgrand – 0.5)two +(xthou – 0.five)2 ] + (y1000 + 1)two – r2 + 2(yk + 1) + ane
= Pgrand + (x1000+i – 0.5)2 – (10g – 0.5)2 + 2(ythou + 1) + 1
= P1000 + (x2 k+1 – 102 thousand) – (tengrand+1 – tenk) + two(yone thousand + 1) + one
= Pk + two(yk +i) + ane, when Pthou <=0 i.due east the midpoint is inside the circle
(xk+1 = 101000)
Pchiliad + ii(y1000 +1) – ii(xk – one) + 1, when Pchiliad>0 I.due east the mid point is exterior the circle(tenthou+1 = xk-1)

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

P1 = (r – 0.5)2 + (0+1)ii – rtwo
= i.25 – r
= 1 -r (When rounded off)

Examples:

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

first point to be plotted

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

CPP

#include<iostream>

using namespace std;

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

{

int 10 = r, y = 0;

cout << "(" << x + 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 << ", " << ten + y_centre << ")\n" ;

}

int P = 1 - r;

while (10 > y)

{

y++;

if (P <= 0)

P = P + 2*y + one;

else

{

ten--;

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

}

if (x < y)

break ;

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

if (ten != 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" ;

}

}

}

int primary()

{

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

if (r > 0)

{

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

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

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

}

int P = 1 - r;

while (x > y)

{

y++;

if (P <= 0)

P = P + 2*y + i;

else

{

x--;

P = P + 2*y - ii*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)\northward" , -x + x_centre, -y + y_centre);

if (10 != y)

{

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

}

}

}

int master()

{

midPointCircleDraw(0, 0, 3);

return 0;

}

Java

class GFG {

static void midPointCircleDraw( int x_centre,

int y_centre, int r)

{

int x = r, y = 0 ;

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

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

if (r > 0 ) {

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

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

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

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

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

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

}

int P = 1 - r;

while (x > y) {

y++;

if (P <= 0 )

P = P + 2 * y + 1 ;

else {

ten--;

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

}

if (x < y)

pause ;

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

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

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

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

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

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

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

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

if (x != y) {

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

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

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

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

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

+ ", " + (-ten + 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

impress ( "(" , x + x_centre, ", " ,

y + y_centre, ")" ,

sep = " ", finish = " ")

if (r > 0 ) :

impress ( "(" , ten + x_centre, ", " ,

- y + y_centre, ")" ,

sep = " ", end = " ")

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

x + y_centre, ")" ,

sep = " ", terminate = " ")

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

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

P = 1 - r

while x > y:

y + = 1

if P < = 0 :

P = P + 2 * y + ane

else :

10 - = 1

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

if (ten < y):

break

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

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

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

")" , sep = " ", finish = " ")

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

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

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

")" , sep = "")

if x ! = y:

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

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

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

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

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

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

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

")" , sep = "")

if __name__ = = '__main__' :

midPointCircleDraw( 0 , 0 , 3 )

C#

using System;

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

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

}

int P = 1 - r;

while (x > y)

{

y++;

if (P <= 0)

P = P + 2 * y + 1;

else

{

x--;

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

}

if (x < y)

break ;

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 (10 != y)

{

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

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

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

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

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

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

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

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

}

}

}

public static void Main()

{

midPointCircleDraw(0, 0, 3);

}

}

PHP

<?php

function midPointCircleDraw( $x_centre ,

$y_centre ,

$r )

{

$ten = $r ;

$y = 0;

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

if ( $r > 0)

{

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

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

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

}

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

pause ;

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

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

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

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

if ( $10 != $y )

{

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

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

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

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

}

}

}

midPointCircleDraw(0, 0, iii);

?>

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

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

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

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

}

var P = 1 - r;

while (x > y) {

y++;

if (P <= 0)

P = P + 2 * y + 1;

else {

ten--;

P = P + ii * y - 2 * ten + ane;

}

if (ten < y)

break ;

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

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

document.write( "(" + (10 + 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) + ", " + (-x + y_centre) + ")" );

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

}

}

}

midPointCircleDraw(0, 0, iii);

</script>

Output:

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

Fourth dimension Complexity: O(x – y)
Auxiliary Space: O(i)
References : Midpoint Circle Algorithm
Paradigm 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 commodity is contributed past Nabaneet Roy. If y'all like GeeksforGeeks and would like to contribute, y'all tin can likewise write an commodity using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. Run across your article appearing on the GeeksforGeeks main page and assist other Geeks.
Please write comments if you lot notice anything incorrect, or y'all want to share more information about the topic discussed above.


petriehicest.blogspot.com

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

0 Response to "Draw a Circle With Radius 20 Around Origin"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel