Draw Circle of Radius 12
The mid-point circle cartoon 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 so print them along with their mirror points in the other octants. This will piece of work because a circle is symmetric virtually its centre.
The algorithm is very similar to the Mid-Bespeak Line Generation Algorithm. Here, only the purlieus condition is dissimilar.
For whatsoever given pixel (x, y), the adjacent pixel to be plotted is either (x, y+1) or (10-i, y+1). This can be decided by following the steps below.
- Find the mid-point p of the ii possible pixels i.e (x-0.5, y+1)
- If p lies inside or on the circle perimeter, we plot the pixel (x, y+ane), otherwise if information technology's outside we plot the pixel (x-1, y+1)
Boundary Condition : Whether the mid-signal lies inside or outside the circle can be decided past using the formula:-
Given a circumvolve centered at (0,0) and radius r and a signal p(x,y)
F(p) = ten2 + y2 – r2
if F(p)<0, the indicate is within the circle
F(p)=0, the point is on the perimeter
F(p)>0, the point is exterior the circle
In our program, we denote F(p) with P. The value of P is calculated at the mid-betoken of the two contending pixels i.e. (x-0.five, y+1). Each pixel is described with a subscript k.
Pm = (Xk — 0.v)2 + (ythou + 1)two – r2
Now,
tenk+ane = xk or xk-one , ychiliad+1= yk +1
∴ Pk+1 = (xyard+1 – 0.five)ii + (ym+ane +1)2 – r2
= (tenk+i – 0.5)2 + [(yone thousand +1) + ane]2 – r2
= (xk+1 – 0.5)2 + (yk +i)2 + 2(ygrand + 1) + 1 – r2
= (tenyard+1 – 0.5)2 + [ – (xk – 0.5)ii +(xyard – 0.5)ii ] + (ythousand + i)ii – r2 + 2(ygrand + 1) + 1
= Pyard + (tenk+i – 0.five)2 – (10k – 0.five)2 + two(yk + i) + ane
= Pyard + (x2 one thousand+1 – xtwo k) – (xk+1 – 10k) + 2(y1000 + 1) + ane
= Pk + 2(ychiliad +1) + i, when Pg <=0 i.eastward the midpoint is within the circle
(xk+1 = tengrand)
Pthou + two(ygrand +one) – 2(10yard – 1) + 1, when Pk>0 I.e the mid signal is outside the circumvolve(xm+one = xgrand-i)
The get-go point to be plotted is (r, 0) on the ten-axis. The initial value of P is calculated as follows:-
P1 = (r – 0.five)2 + (0+1)2 – r2
= 1.25 – r
= one -r (When rounded off)
Examples:
Input : Middle -> (0, 0), Radius -> iii Output : (3, 0) (3, 0) (0, 3) (0, three) (3, ane) (-3, 1) (3, -ane) (-3, -1) (1, 3) (-1, iii) (i, -three) (-i, -3) (ii, 2) (-two, 2) (2, -2) (-two, -ii)
Input : Middle -> (4, 4), Radius -> 2 Output : (six, four) (6, four) (4, 6) (4, 6) (six, v) (ii, 5) (half dozen, three) (2, three) (5, half dozen) (3, 6) (five, 2) (3, 2)
CPP
#include<iostream>
using namespace std;
void midPointCircleDraw( int x_centre, int y_centre, int r)
{
int 10 = r, y = 0;
cout << "(" << ten + x_centre << ", " << y + y_centre << ") " ;
if (r > 0)
{
cout << "(" << 10 + x_centre << ", " << -y + y_centre << ") " ;
cout << "(" << y + x_centre << ", " << x + y_centre << ") " ;
cout << "(" << -y + x_centre << ", " << 10 + y_centre << ")\due north" ;
}
int P = one - r;
while (x > y)
{
y++;
if (P <= 0)
P = P + two*y + 1;
else
{
ten--;
P = P + 2*y - 2*x + 1;
}
if (ten < 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 << ")\n" ;
if (10 != y)
{
cout << "(" << y + x_centre << ", " << ten + y_centre << ") " ;
cout << "(" << -y + x_centre << ", " << 10 + 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 ten = 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, 10 + 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 + ii*y + one;
else
{
10--;
P = P + 2*y - two*ten + 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) " , x + x_centre, -y + y_centre);
printf ( "(%d, %d)\due north" , -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, -ten + y_centre);
printf ( "(%d, %d)\due north" , -y + x_centre, -x + y_centre);
}
}
}
int primary()
{
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 ;
Organisation.out.print( "(" + (x + x_centre)
+ ", " + (y + y_centre) + ")" );
if (r > 0 ) {
Arrangement.out.impress( "(" + (x + x_centre)
+ ", " + (-y + y_centre) + ")" );
System.out.print( "(" + (y + x_centre)
+ ", " + (10 + y_centre) + ")" );
System.out.println( "(" + (-y + x_centre)
+ ", " + (ten + y_centre) + ")" );
}
int P = i - r;
while (x > y) {
y++;
if (P <= 0 )
P = P + two * y + 1 ;
else {
x--;
P = P + ii * y - 2 * x + 1 ;
}
if (ten < y)
interruption ;
System.out.print( "(" + (x + x_centre)
+ ", " + (y + y_centre) + ")" );
System.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)
+ ", " + (x + y_centre) + ")" );
Organisation.out.print( "(" + (-y + x_centre)
+ ", " + (ten + y_centre) + ")" );
System.out.impress( "(" + (y + x_centre)
+ ", " + (-10 + y_centre) + ")" );
System.out.println( "(" + (-y + x_centre)
+ ", " + (-ten + y_centre) + ")" );
}
}
}
public static void main(String[] args) {
midPointCircleDraw( 0 , 0 , 3 );
}
}
Python3
def midPointCircleDraw(x_centre, y_centre, r):
x = r
y = 0
impress ( "(" , x + x_centre, ", " ,
y + y_centre, ")" ,
sep = " ", stop = " ")
if (r > 0 ) :
print ( "(" , ten + x_centre, ", " ,
- y + y_centre, ")" ,
sep = " ", finish = " ")
impress ( "(" , y + x_centre, ", " ,
x + y_centre, ")" ,
sep = " ", end = " ")
print ( "(" , - y + x_centre, ", " ,
ten + y_centre, ")" , sep = "")
P = 1 - r
while x > y:
y + = ane
if P < = 0 :
P = P + 2 * y + 1
else :
x - = 1
P = P + 2 * y - 2 * x + ane
if (10 < y):
pause
print ( "(" , ten + x_centre, ", " , y + y_centre,
")" , sep = " ", end = " ")
print ( "(" , - x + x_centre, ", " , y + y_centre,
")" , sep = " ", stop = " ")
print ( "(" , x + x_centre, ", " , - y + y_centre,
")" , sep = " ", stop = " ")
print ( "(" , - x + x_centre, ", " , - y + y_centre,
")" , sep = "")
if x ! = y:
impress ( "(" , y + x_centre, ", " , x + y_centre,
")" , sep = " ", end = " ")
print ( "(" , - y + x_centre, ", " , x + y_centre,
")" , sep = " ", end = " ")
print ( "(" , y + x_centre, ", " , - 10 + y_centre,
")" , sep = " ", finish = " ")
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;
Panel.Write( "(" + (x + x_centre)
+ ", " + (y + y_centre) + ")" );
if (r > 0)
{
Console.Write( "(" + (10 + x_centre)
+ ", " + (-y + y_centre) + ")" );
Console.Write( "(" + (y + x_centre)
+ ", " + (ten + 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 + i;
else
{
x--;
P = P + 2 * y - two * x + i;
}
if (x < y)
break ;
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( "(" + (-ten + x_centre)
+ ", " + (-y + y_centre) + ")" );
if (ten != y)
{
Console.Write( "(" + (y + x_centre)
+ ", " + (ten + y_centre) + ")" );
Console.Write( "(" + (-y + x_centre)
+ ", " + (10 + 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
function midPointCircleDraw( $x_centre ,
$y_centre ,
$r )
{
$x = $r ;
$y = 0;
echo "(" , $x + $x_centre , "," , $y + $y_centre , ")" ;
if ( $r > 0)
{
echo "(" , $x + $x_centre , "," , - $y + $y_centre , ")" ;
echo "(" , $y + $x_centre , "," , $x + $y_centre , ")" ;
repeat "(" ,- $y + $x_centre , "," , $ten + $y_centre , ")" , "\n" ;
}
$P = one - $r ;
while ( $ten > $y )
{
$y ++;
if ( $P <= 0)
$P = $P + two * $y + 1;
else
{
$x --;
$P = $P + 2 * $y -
2 * $x + 1;
}
if ( $x < $y )
break ;
echo "(" , $ten + $x_centre , "," , $y + $y_centre , ")" ;
echo "(" ,- $10 + $x_centre , "," , $y + $y_centre , ")" ;
repeat "(" , $x + $x_centre , "," , - $y + $y_centre , ")" ;
repeat "(" ,- $10 + $x_centre , "," , - $y + $y_centre , ")" , "\due north" ;
if ( $x != $y )
{
repeat "(" , $y + $x_centre , "," , $x + $y_centre , ")" ;
echo "(" ,- $y + $x_centre , "," , $10 + $y_centre , ")" ;
repeat "(" , $y + $x_centre , "," , - $x + $y_centre , ")" ;
echo "(" ,- $y + $x_centre , "," , - $ten + $y_centre , ")" , "\northward" ;
}
}
}
midPointCircleDraw(0, 0, 3);
?>
Javascript
<script>
function midPointCircleDraw(x_centre , y_centre , r) {
var x = r, y = 0;
document.write( "(" + (10 + x_centre) + ", " + (y + y_centre) + ")" );
if (r > 0) {
document.write( "(" + (x + x_centre) + ", " + (-y + y_centre) + ")" );
document.write( "(" + (y + x_centre) + ", " + (x + y_centre) + ")" );
document.write( "(" + (-y + x_centre) + ", " + (ten + y_centre) + ")<br/>" );
}
var P = i - r;
while (x > y) {
y++;
if (P <= 0)
P = P + two * y + 1;
else {
10--;
P = P + 2 * y - two * x + 1;
}
if (x < y)
break ;
document.write( "(" + (ten + x_centre) + ", " + (y + y_centre) + ")" );
document.write( "(" + (-ten + x_centre) + ", " + (y + y_centre) + ")" );
certificate.write( "(" + (x + x_centre) + ", " + (-y + y_centre) + ")" );
certificate.write( "(" + (-10 + x_centre) + ", " + (-y + y_centre) + ")<br/>" );
if (x != y) {
document.write( "(" + (y + x_centre) + ", " + (10 + 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/>" );
}
}
}
midPointCircleDraw(0, 0, 3);
</script>
Output:
(3, 0) (3, 0) (0, 3) (0, 3) (3, 1) (-three, 1) (iii, -1) (-3, -1) (ane, iii) (-ane, 3) (1, -3) (-1, -3) (2, 2) (-two, 2) (2, -2) (-2, -2)
Time Complication: 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
Thank you Tuhina Singh and Teva Zanker for improving this article.
This article is contributed by Nabaneet Roy. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail service your article to review-team@geeksforgeeks.org. Run across your article appearing on the GeeksforGeeks main page and help other Geeks.
Delight write comments if yous notice anything wrong, or you want to share more information about the topic discussed above.
Source: https://www.geeksforgeeks.org/mid-point-circle-drawing-algorithm/
0 Response to "Draw Circle of Radius 12"
Post a Comment