Java Draw Circle With Points
The mid-point circumvolve drawing algorithm is an algorithm used to make up one's mind the points needed for rasterizing a circle.
Nosotros use the mid-point algorithm to calculate all the perimeter points of the circumvolve in the first octant and then print them forth with their mirror points in the other octants. This volition piece of work because a circumvolve is symmetric nearly its center.
The algorithm is very similar to the Mid-Point Line Generation Algorithm. Here, merely the boundary condition is different.
For whatever given pixel (x, y), the side by side pixel to be plotted is either (ten, y+1) or (x-1, y+ane). This can be decided by following the steps below.
- Find the mid-point p of the 2 possible pixels i.e (ten-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+one)
Boundary Condition : Whether the mid-indicate 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 point p(x,y)
F(p) = x2 + yii – r2
if F(p)<0, the point is within the circle
F(p)=0, the point is on the perimeter
F(p)>0, the betoken is outside the circumvolve
In our program, we announce 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 k.
Pyard = (Xk — 0.5)2 + (ychiliad + 1)2 – rtwo
Now,
10grand+1 = tenthou or xk-1 , yyard+i= yk +one
∴ Pm+1 = (10k+i – 0.5)2 + (ythousand+1 +1)2 – rii
= (xm+1 – 0.5)ii + [(ychiliad +1) + one]ii – r2
= (xk+i – 0.5)two + (yk +one)two + 2(yk + 1) + one – r2
= (xg+1 – 0.5)2 + [ – (tenk – 0.5)2 +(xone thousand – 0.5)2 ] + (yk + 1)2 – r2 + two(yk + 1) + 1
= Pthousand + (xone thousand+ane – 0.5)ii – (10yard – 0.5)ii + 2(yk + 1) + 1
= Pk + (10ii chiliad+1 – x2 k) – (xk+1 – xthousand) + 2(yg + 1) + one
= Pk + ii(yk +one) + 1, when Pk <=0 i.e the midpoint is inside the circle
(tengrand+1 = xyard)
Pchiliad + 2(yk +1) – ii(10yard – 1) + one, when Pk>0 I.e the mid point is outside the circumvolve(xk+1 = xthousand-ane)
The commencement point to be plotted is (r, 0) on the x-axis. The initial value of P is calculated as follows:-
P1 = (r – 0.5)ii + (0+one)2 – r2
= 1.25 – r
= 1 -r (When rounded off)
Examples:
Input : Centre -> (0, 0), Radius -> 3 Output : (3, 0) (3, 0) (0, three) (0, three) (3, 1) (-3, 1) (3, -one) (-3, -ane) (one, 3) (-1, 3) (one, -iii) (-1, -iii) (2, ii) (-2, ii) (2, -2) (-2, -2)
Input : Eye -> (4, 4), Radius -> ii Output : (6, four) (6, iv) (4, 6) (iv, 6) (6, 5) (two, 5) (half-dozen, 3) (2, 3) (five, vi) (three, 6) (five, two) (3, 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 <<
", "
<< ten + y_centre <<
") "
;
cout <<
"("
<< -y + x_centre <<
", "
<< x + y_centre <<
")\north"
;
}
int
P = 1 - r;
while
(x > y)
{
y++;
if
(P <= 0)
P = P + ii*y + 1;
else
{
x--;
P = P + 2*y - 2*10 + 1;
}
if
(ten < y)
interruption
;
cout <<
"("
<< x + x_centre <<
", "
<< y + y_centre <<
") "
;
cout <<
"("
<< -x + x_centre <<
", "
<< y + y_centre <<
") "
;
cout <<
"("
<< x + x_centre <<
", "
<< -y + y_centre <<
") "
;
cout <<
"("
<< -ten + x_centre <<
", "
<< -y + y_centre <<
")\n"
;
if
(10 != y)
{
cout <<
"("
<< y + x_centre <<
", "
<< x + y_centre <<
") "
;
cout <<
"("
<< -y + x_centre <<
", "
<< x + y_centre <<
") "
;
cout <<
"("
<< y + x_centre <<
", "
<< -ten + y_centre <<
") "
;
cout <<
"("
<< -y + x_centre <<
", "
<< -x + y_centre <<
")\north"
;
}
}
}
int
principal()
{
midPointCircleDraw(0, 0, three);
return
0;
}
C
#include<stdio.h>
void
midPointCircleDraw(
int
x_centre,
int
y_centre,
int
r)
{
int
x = r, y = 0;
printf
(
"(%d, %d) "
, ten + 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)\due north"
, -y + x_centre, x + y_centre);
}
int
P = ane - r;
while
(10 > y)
{
y++;
if
(P <= 0)
P = P + 2*y + 1;
else
{
ten--;
P = P + 2*y - 2*x + i;
}
if
(x < 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)\n"
, -x + x_centre, -y + y_centre);
if
(10 != 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, -x + 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
;
Organization.out.impress(
"("
+ (10 + x_centre)
+
", "
+ (y + y_centre) +
")"
);
if
(r >
0
) {
System.out.print(
"("
+ (x + x_centre)
+
", "
+ (-y + y_centre) +
")"
);
Organization.out.impress(
"("
+ (y + x_centre)
+
", "
+ (10 + 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 -
ii
* x +
1
;
}
if
(ten < y)
break
;
Organisation.out.impress(
"("
+ (10 + x_centre)
+
", "
+ (y + y_centre) +
")"
);
Organisation.out.print(
"("
+ (-ten + x_centre)
+
", "
+ (y + y_centre) +
")"
);
System.out.print(
"("
+ (x + x_centre) +
", "
+ (-y + y_centre) +
")"
);
Organisation.out.println(
"("
+ (-ten + 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)
+
", "
+ (-x + y_centre) +
")"
);
System.out.println(
"("
+ (-y + x_centre)
+
", "
+ (-10 + y_centre) +
")"
);
}
}
}
public
static
void
main(String[] args) {
midPointCircleDraw(
0
,
0
,
3
);
}
}
Python3
def
midPointCircleDraw(x_centre, y_centre, r):
x
=
r
y
=
0
print
(
"("
, ten
+
x_centre,
", "
,
y
+
y_centre,
")"
,
sep
=
"
", end = "
")
if
(r >
0
) :
print
(
"("
, x
+
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
=
one
-
r
while
x > y:
y
+
=
1
if
P <
=
0
:
P
=
P
+
2
*
y
+
ane
else
:
ten
-
=
ane
P
=
P
+
2
*
y
-
2
*
x
+
1
if
(10 < y):
break
print
(
"("
, x
+
x_centre,
", "
, y
+
y_centre,
")"
, sep
=
"
", terminate = "
")
print
(
"("
,
-
x
+
x_centre,
", "
, y
+
y_centre,
")"
, sep
=
"
", end = "
")
print
(
"("
, x
+
x_centre,
", "
,
-
y
+
y_centre,
")"
, sep
=
"
", terminate = "
")
print
(
"("
,
-
x
+
x_centre,
", "
,
-
y
+
y_centre,
")"
, sep
=
"")
if
x !
=
y:
print
(
"("
, y
+
x_centre,
", "
, x
+
y_centre,
")"
, sep
=
"
", cease = "
")
print
(
"("
,
-
y
+
x_centre,
", "
, x
+
y_centre,
")"
, sep
=
"
", end = "
")
print
(
"("
, y
+
x_centre,
", "
,
-
ten
+
y_centre,
")"
, sep
=
"
", finish = "
")
print
(
"("
,
-
y
+
x_centre,
", "
,
-
x
+
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(
"("
+ (10 + x_centre)
+
", "
+ (y + y_centre) +
")"
);
if
(r > 0)
{
Console.Write(
"("
+ (x + x_centre)
+
", "
+ (-y + y_centre) +
")"
);
Console.Write(
"("
+ (y + x_centre)
+
", "
+ (10 + y_centre) +
")"
);
Console.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
(ten < y)
interruption
;
Console.Write(
"("
+ (x + x_centre)
+
", "
+ (y + y_centre) +
")"
);
Console.Write(
"("
+ (-x + x_centre)
+
", "
+ (y + y_centre) +
")"
);
Console.Write(
"("
+ (10 + x_centre) +
", "
+ (-y + y_centre) +
")"
);
Console.WriteLine(
"("
+ (-x + 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)
+
", "
+ (-ten + y_centre) +
")"
);
Console.WriteLine(
"("
+ (-y + x_centre)
+
", "
+ (-ten + y_centre) +
")"
);
}
}
}
public
static
void
Main()
{
midPointCircleDraw(0, 0, three);
}
}
PHP
<?php
function
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
,
")"
;
echo
"("
,
$y
+
$x_centre
,
","
,
$10
+
$y_centre
,
")"
;
echo
"("
,-
$y
+
$x_centre
,
","
,
$ten
+
$y_centre
,
")"
,
"\northward"
;
}
$P
= 1 -
$r
;
while
(
$ten
>
$y
)
{
$y
++;
if
(
$P
<= 0)
$P
=
$P
+ 2 *
$y
+ one;
else
{
$ten
--;
$P
=
$P
+ 2 *
$y
-
2 *
$ten
+ ane;
}
if
(
$x
<
$y
)
suspension
;
repeat
"("
,
$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
)
{
echo
"("
,
$y
+
$x_centre
,
","
,
$x
+
$y_centre
,
")"
;
echo
"("
,-
$y
+
$x_centre
,
","
,
$ten
+
$y_centre
,
")"
;
echo
"("
,
$y
+
$x_centre
,
","
, -
$x
+
$y_centre
,
")"
;
echo
"("
,-
$y
+
$x_centre
,
","
, -
$x
+
$y_centre
,
")"
,
"\n"
;
}
}
}
midPointCircleDraw(0, 0, three);
?>
Javascript
<script>
office
midPointCircleDraw(x_centre , y_centre , r) {
var
x = r, y = 0;
document.write(
"("
+ (ten + x_centre) +
", "
+ (y + y_centre) +
")"
);
if
(r > 0) {
document.write(
"("
+ (ten + x_centre) +
", "
+ (-y + y_centre) +
")"
);
document.write(
"("
+ (y + x_centre) +
", "
+ (ten + y_centre) +
")"
);
certificate.write(
"("
+ (-y + x_centre) +
", "
+ (10 + y_centre) +
")<br/>"
);
}
var
P = i - r;
while
(x > y) {
y++;
if
(P <= 0)
P = P + 2 * y + i;
else
{
x--;
P = P + 2 * y - ii * x + 1;
}
if
(10 < y)
interruption
;
document.write(
"("
+ (x + x_centre) +
", "
+ (y + y_centre) +
")"
);
document.write(
"("
+ (-10 + x_centre) +
", "
+ (y + y_centre) +
")"
);
document.write(
"("
+ (x + x_centre) +
", "
+ (-y + y_centre) +
")"
);
document.write(
"("
+ (-ten + x_centre) +
", "
+ (-y + y_centre) +
")<br/>"
);
if
(ten != y) {
certificate.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, three);
</script>
Output:
(iii, 0) (iii, 0) (0, 3) (0, 3) (iii, 1) (-3, 1) (3, -1) (-3, -1) (1, 3) (-1, 3) (1, -3) (-1, -3) (2, 2) (-2, ii) (2, -2) (-ii, -two)
Time Complexity: O(x – y)
Auxiliary Infinite: O(1)
References : Midpoint Circle Algorithm
Paradigm 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 article.
This article is contributed by Nabaneet Roy. If yous like GeeksforGeeks and would like to contribute, you can likewise write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main folio and assistance other Geeks.
Please write comments if y'all notice anything incorrect, or you desire to share more information about the topic discussed above.
Source: https://www.geeksforgeeks.org/mid-point-circle-drawing-algorithm/