Algorithm Draw a Full Circle
The mid-bespeak circle drawing algorithm is an algorithm used to decide the points needed for rasterizing a circle.
We use the mid-signal algorithm to calculate all the perimeter points of the circumvolve in the commencement octant and then print them forth with their mirror points in the other octants. This volition piece of work because a circle is symmetric about its middle.
The algorithm is very like to the Mid-Bespeak Line Generation Algorithm. Hither, but the boundary condition is different.
For whatsoever given pixel (ten, y), the next pixel to be plotted is either (10, y+1) or (ten-1, y+1). This tin be decided by following the steps below.
- Observe the mid-point p of the 2 possible pixels i.eastward (10-0.five, y+ane)
- If p lies inside or on the circle perimeter, we plot the pixel (x, y+i), otherwise if it's outside we plot the pixel (x-1, y+1)
Boundary Condition : Whether the mid-indicate lies inside or exterior the circumvolve tin can be decided by using the formula:-
Given a circle centered at (0,0) and radius r and a point p(10,y)
F(p) = x2 + y2 – r2
if F(p)<0, the point is within the circumvolve
F(p)=0, the point is on the perimeter
F(p)>0, the signal is outside the circle
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. (x-0.v, y+i). Each pixel is described with a subscript one thousand.
Pk = (Tenk — 0.5)2 + (yg + 1)2 – r2
At present,
xone thousand+1 = xthousand or 10g-1 , yk+1= yk +i
∴ P1000+1 = (xgrand+1 – 0.5)ii + (yk+i +1)2 – r2
= (tenk+1 – 0.5)2 + [(yk +i) + 1]2 – r2
= (10k+1 – 0.5)2 + (y1000 +1)ii + 2(yk + i) + 1 – r2
= (xk+i – 0.five)2 + [ – (xchiliad – 0.5)2 +(xk – 0.5)2 ] + (yk + 1)2 – rii + two(yg + 1) + 1
= Pk + (xthousand+1 – 0.v)two – (10yard – 0.5)2 + 2(yk + one) + ane
= Pthou + (xtwo k+ane – xii k) – (xk+1 – xg) + two(yk + 1) + ane
= Pk + 2(yk +1) + one, when Pthousand <=0 i.e the midpoint is inside the circumvolve
(10k+1 = 10k)
Pk + 2(y1000 +1) – 2(xk – one) + 1, when Pg>0 I.eastward the mid point is exterior the circle(xchiliad+1 = xk-1)
The offset betoken to exist plotted is (r, 0) on the x-axis. The initial value of P is calculated as follows:-
P1 = (r – 0.5)2 + (0+1)ii – rtwo
= ane.25 – r
= one -r (When rounded off)
Examples:
Input : Centre -> (0, 0), Radius -> 3 Output : (iii, 0) (3, 0) (0, 3) (0, iii) (3, i) (-iii, 1) (three, -1) (-three, -1) (1, iii) (-one, iii) (1, -iii) (-1, -3) (2, 2) (-2, ii) (ii, -2) (-2, -2)
Input : Center -> (4, 4), Radius -> 2 Output : (vi, 4) (half-dozen, 4) (four, vi) (4, 6) (6, v) (2, 5) (6, 3) (2, 3) (5, 6) (3, 6) (five, two) (3, two)
CPP
#include<iostream>
using
namespace
std;
void
midPointCircleDraw(
int
x_centre,
int
y_centre,
int
r)
{
int
x = 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 <<
", "
<< x + y_centre <<
")\n"
;
}
int
P = ane - r;
while
(x > y)
{
y++;
if
(P <= 0)
P = P + two*y + ane;
else
{
10--;
P = P + 2*y - 2*ten + i;
}
if
(ten < y)
suspension
;
cout <<
"("
<< x + x_centre <<
", "
<< y + y_centre <<
") "
;
cout <<
"("
<< -x + x_centre <<
", "
<< y + y_centre <<
") "
;
cout <<
"("
<< 10 + 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
10 = 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)\n"
, -y + x_centre, x + y_centre);
}
int
P = ane - r;
while
(ten > y)
{
y++;
if
(P <= 0)
P = P + two*y + 1;
else
{
x--;
P = P + 2*y - 2*x + 1;
}
if
(ten < y)
pause
;
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)\northward"
, -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)\n"
, -y + x_centre, -ten + y_centre);
}
}
}
int
main()
{
midPointCircleDraw(0, 0, 3);
render
0;
}
Java
class
GFG {
static
void
midPointCircleDraw(
int
x_centre,
int
y_centre,
int
r)
{
int
10 = r, y =
0
;
Arrangement.out.print(
"("
+ (x + x_centre)
+
", "
+ (y + y_centre) +
")"
);
if
(r >
0
) {
Organization.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) +
")"
);
}
int
P =
1
- r;
while
(ten > y) {
y++;
if
(P <=
0
)
P = P +
2
* y +
1
;
else
{
x--;
P = P +
2
* y -
2
* 10 +
1
;
}
if
(x < y)
interruption
;
System.out.print(
"("
+ (x + x_centre)
+
", "
+ (y + y_centre) +
")"
);
Arrangement.out.print(
"("
+ (-x + x_centre)
+
", "
+ (y + y_centre) +
")"
);
Arrangement.out.print(
"("
+ (ten + x_centre) +
", "
+ (-y + y_centre) +
")"
);
Arrangement.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)
+
", "
+ (10 + y_centre) +
")"
);
System.out.impress(
"("
+ (y + x_centre)
+
", "
+ (-ten + y_centre) +
")"
);
System.out.println(
"("
+ (-y + x_centre)
+
", "
+ (-x + y_centre) +
")"
);
}
}
}
public
static
void
main(String[] args) {
midPointCircleDraw(
0
,
0
,
3
);
}
}
Python3
def
midPointCircleDraw(x_centre, y_centre, r):
ten
=
r
y
=
0
impress
(
"("
, 10
+
x_centre,
", "
,
y
+
y_centre,
")"
,
sep
=
"
", end = "
")
if
(r >
0
) :
print
(
"("
, 10
+
x_centre,
", "
,
-
y
+
y_centre,
")"
,
sep
=
"
", end = "
")
print
(
"("
, y
+
x_centre,
", "
,
x
+
y_centre,
")"
,
sep
=
"
", end = "
")
impress
(
"("
,
-
y
+
x_centre,
", "
,
x
+
y_centre,
")"
, sep
=
"")
P
=
1
-
r
while
ten > y:
y
+
=
one
if
P <
=
0
:
P
=
P
+
2
*
y
+
ane
else
:
x
-
=
1
P
=
P
+
2
*
y
-
2
*
ten
+
1
if
(10 < y):
suspension
impress
(
"("
, x
+
x_centre,
", "
, y
+
y_centre,
")"
, sep
=
"
", end = "
")
print
(
"("
,
-
x
+
x_centre,
", "
, y
+
y_centre,
")"
, sep
=
"
", finish = "
")
print
(
"("
, x
+
x_centre,
", "
,
-
y
+
y_centre,
")"
, sep
=
"
", terminate = "
")
print
(
"("
,
-
10
+
x_centre,
", "
,
-
y
+
y_centre,
")"
, sep
=
"")
if
x !
=
y:
print
(
"("
, y
+
x_centre,
", "
, x
+
y_centre,
")"
, sep
=
"
", finish = "
")
print
(
"("
,
-
y
+
x_centre,
", "
, x
+
y_centre,
")"
, sep
=
"
", end = "
")
print
(
"("
, y
+
x_centre,
", "
,
-
x
+
y_centre,
")"
, sep
=
"
", cease = "
")
print
(
"("
,
-
y
+
x_centre,
", "
,
-
x
+
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;
Panel.Write(
"("
+ (x + x_centre)
+
", "
+ (y + y_centre) +
")"
);
if
(r > 0)
{
Console.Write(
"("
+ (x + x_centre)
+
", "
+ (-y + y_centre) +
")"
);
Console.Write(
"("
+ (y + x_centre)
+
", "
+ (x + y_centre) +
")"
);
Console.WriteLine(
"("
+ (-y + x_centre)
+
", "
+ (10 + y_centre) +
")"
);
}
int
P = 1 - r;
while
(x > y)
{
y++;
if
(P <= 0)
P = P + 2 * y + 1;
else
{
10--;
P = P + two * y - 2 * x + 1;
}
if
(x < y)
intermission
;
Panel.Write(
"("
+ (10 + x_centre)
+
", "
+ (y + y_centre) +
")"
);
Console.Write(
"("
+ (-x + x_centre)
+
", "
+ (y + y_centre) +
")"
);
Console.Write(
"("
+ (x + x_centre) +
", "
+ (-y + y_centre) +
")"
);
Console.WriteLine(
"("
+ (-10 + x_centre)
+
", "
+ (-y + y_centre) +
")"
);
if
(x != y)
{
Panel.Write(
"("
+ (y + x_centre)
+
", "
+ (ten + y_centre) +
")"
);
Console.Write(
"("
+ (-y + x_centre)
+
", "
+ (ten + y_centre) +
")"
);
Console.Write(
"("
+ (y + x_centre)
+
", "
+ (-x + y_centre) +
")"
);
Console.WriteLine(
"("
+ (-y + x_centre)
+
", "
+ (-ten + y_centre) +
")"
);
}
}
}
public
static
void
Master()
{
midPointCircleDraw(0, 0, 3);
}
}
PHP
<?php
function
midPointCircleDraw(
$x_centre
,
$y_centre
,
$r
)
{
$x
=
$r
;
$y
= 0;
repeat
"("
,
$10
+
$x_centre
,
","
,
$y
+
$y_centre
,
")"
;
if
(
$r
> 0)
{
echo
"("
,
$10
+
$x_centre
,
","
, -
$y
+
$y_centre
,
")"
;
echo
"("
,
$y
+
$x_centre
,
","
,
$x
+
$y_centre
,
")"
;
repeat
"("
,-
$y
+
$x_centre
,
","
,
$x
+
$y_centre
,
")"
,
"\n"
;
}
$P
= 1 -
$r
;
while
(
$10
>
$y
)
{
$y
++;
if
(
$P
<= 0)
$P
=
$P
+ two *
$y
+ ane;
else
{
$x
--;
$P
=
$P
+ two *
$y
-
ii *
$x
+ one;
}
if
(
$x
<
$y
)
break
;
repeat
"("
,
$ten
+
$x_centre
,
","
,
$y
+
$y_centre
,
")"
;
repeat
"("
,-
$x
+
$x_centre
,
","
,
$y
+
$y_centre
,
")"
;
echo
"("
,
$x
+
$x_centre
,
","
, -
$y
+
$y_centre
,
")"
;
repeat
"("
,-
$x
+
$x_centre
,
","
, -
$y
+
$y_centre
,
")"
,
"\due north"
;
if
(
$x
!=
$y
)
{
echo
"("
,
$y
+
$x_centre
,
","
,
$10
+
$y_centre
,
")"
;
repeat
"("
,-
$y
+
$x_centre
,
","
,
$x
+
$y_centre
,
")"
;
echo
"("
,
$y
+
$x_centre
,
","
, -
$x
+
$y_centre
,
")"
;
repeat
"("
,-
$y
+
$x_centre
,
","
, -
$x
+
$y_centre
,
")"
,
"\north"
;
}
}
}
midPointCircleDraw(0, 0, 3);
?>
Javascript
<script>
role
midPointCircleDraw(x_centre , y_centre , r) {
var
ten = r, y = 0;
document.write(
"("
+ (x + x_centre) +
", "
+ (y + y_centre) +
")"
);
if
(r > 0) {
document.write(
"("
+ (ten + x_centre) +
", "
+ (-y + y_centre) +
")"
);
document.write(
"("
+ (y + x_centre) +
", "
+ (x + y_centre) +
")"
);
document.write(
"("
+ (-y + x_centre) +
", "
+ (x + y_centre) +
")<br/>"
);
}
var
P = ane - r;
while
(10 > y) {
y++;
if
(P <= 0)
P = P + ii * y + ane;
else
{
x--;
P = P + two * y - two * x + 1;
}
if
(x < y)
break
;
document.write(
"("
+ (x + x_centre) +
", "
+ (y + y_centre) +
")"
);
document.write(
"("
+ (-x + x_centre) +
", "
+ (y + y_centre) +
")"
);
document.write(
"("
+ (ten + x_centre) +
", "
+ (-y + y_centre) +
")"
);
document.write(
"("
+ (-x + x_centre) +
", "
+ (-y + y_centre) +
")<br/>"
);
if
(10 != y) {
certificate.write(
"("
+ (y + x_centre) +
", "
+ (ten + y_centre) +
")"
);
certificate.write(
"("
+ (-y + x_centre) +
", "
+ (10 + 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, ane) (-iii, i) (3, -1) (-3, -1) (1, 3) (-one, 3) (i, -three) (-1, -3) (ii, ii) (-ii, 2) (2, -two) (-2, -2)
Time Complexity: O(x – y)
Auxiliary Infinite: O(1)
References : Midpoint Circle Algorithm
Image References : Octants of a circumvolve, Rasterised Circle, the other images were created for this article past the geek
Thanks Tuhina Singh and Teva Zanker for improving this article.
This article is contributed by Nabaneet Roy. If y'all like GeeksforGeeks and would similar to contribute, you can also write an commodity using write.geeksforgeeks.org or mail service your article to review-team@geeksforgeeks.org. See your commodity appearing on the GeeksforGeeks main folio and help other Geeks.
Please write comments if you lot find annihilation wrong, or you lot want to share more information about the topic discussed in a higher place.
Source: https://www.geeksforgeeks.org/mid-point-circle-drawing-algorithm/
0 Response to "Algorithm Draw a Full Circle"
Enregistrer un commentaire