Dda Circle Drawing Algorithm in Computer Graphics Using C
The mid-betoken circle drawing algorithm is an algorithm used to decide the points needed for rasterizing a circumvolve.
We use the mid-point algorithm to summate all the perimeter points of the circle in the first octant and then print them along with their mirror points in the other octants. This volition work because a circumvolve is symmetric nearly its center.
The algorithm is very similar to the Mid-Indicate Line Generation Algorithm. Hither, only the boundary condition is different.
For any given pixel (ten, y), the next pixel to be plotted is either (ten, y+one) or (x-one, y+1). This can be decided by following the steps below.
- Detect the mid-point p of the two possible pixels i.e (x-0.5, y+ane)
- If p lies inside or on the circle perimeter, we plot the pixel (x, y+ane), otherwise if it's outside we plot the pixel (x-1, y+1)
Boundary Status : Whether the mid-point lies within or outside the circumvolve can be decided by using the formula:-
Given a circle centered at (0,0) and radius r and a betoken p(x,y)
F(p) = xii + yii – rtwo
if F(p)<0, the point is inside the circumvolve
F(p)=0, the bespeak is on the perimeter
F(p)>0, the point is exterior the circle
In our programme, nosotros denote F(p) with P. The value of P is calculated at the mid-bespeak of the 2 contending pixels i.eastward. (x-0.v, y+1). Each pixel is described with a subscript k.
Pchiliad = (Tengrand — 0.5)ii + (yk + ane)2 – r2
Now,
ten1000+1 = xm or tenk-1 , y1000+1= ygrand +1
∴ Pk+1 = (10k+i – 0.5)ii + (ychiliad+1 +one)two – r2
= (teng+ane – 0.5)2 + [(yk +i) + 1]two – r2
= (xthou+i – 0.5)2 + (ythou +1)2 + 2(yk + 1) + 1 – rtwo
= (xk+1 – 0.v)2 + [ – (tenk – 0.5)2 +(xk – 0.v)two ] + (ygrand + 1)ii – r2 + ii(yk + 1) + 1
= Pk + (10k+i – 0.five)ii – (10chiliad – 0.5)ii + two(ythou + 1) + i
= Pg + (xii k+i – x2 k) – (10k+1 – xk) + 2(yk + 1) + ane
= Pyard + 2(ythousand +1) + i, when Pk <=0 i.e the midpoint is within the circle
(xk+1 = xk)
Pone thousand + 2(yk +1) – ii(tenk – 1) + one, when Pchiliad>0 I.east the mid betoken is outside the circle(xk+1 = xgrand-1)
The first point to be plotted is (r, 0) on the x-axis. The initial value of P is calculated every bit follows:-
P1 = (r – 0.v)2 + (0+1)two – rtwo
= 1.25 – r
= 1 -r (When rounded off)
Examples:
Input : Heart -> (0, 0), Radius -> 3 Output : (3, 0) (3, 0) (0, 3) (0, iii) (3, 1) (-3, 1) (3, -1) (-3, -one) (ane, 3) (-1, 3) (1, -3) (-i, -iii) (2, two) (-2, 2) (2, -ii) (-ii, -2)
Input : Centre -> (4, iv), Radius -> 2 Output : (6, four) (6, 4) (4, half dozen) (4, half dozen) (6, 5) (2, five) (6, 3) (2, 3) (five, 6) (3, 6) (5, 2) (3, two)
CPP
#include<iostream>
using
namespace
std;
void
midPointCircleDraw(
int
x_centre,
int
y_centre,
int
r)
{
int
x = r, y = 0;
cout <<
"("
<< ten + x_centre <<
", "
<< y + y_centre <<
") "
;
if
(r > 0)
{
cout <<
"("
<< ten + x_centre <<
", "
<< -y + y_centre <<
") "
;
cout <<
"("
<< y + x_centre <<
", "
<< x + y_centre <<
") "
;
cout <<
"("
<< -y + x_centre <<
", "
<< x + y_centre <<
")\n"
;
}
int
P = i - r;
while
(10 > y)
{
y++;
if
(P <= 0)
P = P + 2*y + i;
else
{
10--;
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 <<
"("
<< 10 + x_centre <<
", "
<< -y + y_centre <<
") "
;
cout <<
"("
<< -x + x_centre <<
", "
<< -y + y_centre <<
")\n"
;
if
(10 != y)
{
cout <<
"("
<< y + x_centre <<
", "
<< 10 + 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
principal()
{
midPointCircleDraw(0, 0, three);
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)\north"
, -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 - ii*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) "
, 10 + 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, -10 + y_centre);
printf
(
"(%d, %d)\due north"
, -y + x_centre, -ten + y_centre);
}
}
}
int
main()
{
midPointCircleDraw(0, 0, three);
return
0;
}
Java
class
GFG {
static
void
midPointCircleDraw(
int
x_centre,
int
y_centre,
int
r)
{
int
x = r, y =
0
;
System.out.print(
"("
+ (10 + x_centre)
+
", "
+ (y + y_centre) +
")"
);
if
(r >
0
) {
System.out.impress(
"("
+ (x + x_centre)
+
", "
+ (-y + y_centre) +
")"
);
System.out.impress(
"("
+ (y + x_centre)
+
", "
+ (x + y_centre) +
")"
);
System.out.println(
"("
+ (-y + x_centre)
+
", "
+ (ten + y_centre) +
")"
);
}
int
P =
1
- r;
while
(x > y) {
y++;
if
(P <=
0
)
P = P +
2
* y +
1
;
else
{
ten--;
P = P +
ii
* y -
two
* x +
1
;
}
if
(ten < y)
break
;
Arrangement.out.print(
"("
+ (ten + x_centre)
+
", "
+ (y + y_centre) +
")"
);
Organisation.out.print(
"("
+ (-x + x_centre)
+
", "
+ (y + y_centre) +
")"
);
Organisation.out.print(
"("
+ (x + x_centre) +
", "
+ (-y + y_centre) +
")"
);
Organization.out.println(
"("
+ (-10 + x_centre)
+
", "
+ (-y + y_centre) +
")"
);
if
(ten != y) {
System.out.impress(
"("
+ (y + x_centre)
+
", "
+ (x + y_centre) +
")"
);
System.out.print(
"("
+ (-y + x_centre)
+
", "
+ (x + y_centre) +
")"
);
Organization.out.print(
"("
+ (y + x_centre)
+
", "
+ (-10 + y_centre) +
")"
);
System.out.println(
"("
+ (-y + x_centre)
+
", "
+ (-x + y_centre) +
")"
);
}
}
}
public
static
void
master(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
=
"
", end = "
")
if
(r >
0
) :
impress
(
"("
, 10
+
x_centre,
", "
,
-
y
+
y_centre,
")"
,
sep
=
"
", end = "
")
print
(
"("
, y
+
x_centre,
", "
,
x
+
y_centre,
")"
,
sep
=
"
", end = "
")
print
(
"("
,
-
y
+
x_centre,
", "
,
x
+
y_centre,
")"
, sep
=
"")
P
=
1
-
r
while
x > y:
y
+
=
1
if
P <
=
0
:
P
=
P
+
2
*
y
+
1
else
:
10
-
=
one
P
=
P
+
2
*
y
-
2
*
ten
+
1
if
(x < y):
intermission
print
(
"("
, x
+
x_centre,
", "
, y
+
y_centre,
")"
, sep
=
"
", end = "
")
print
(
"("
,
-
x
+
x_centre,
", "
, y
+
y_centre,
")"
, sep
=
"
", end = "
")
print
(
"("
, 10
+
x_centre,
", "
,
-
y
+
y_centre,
")"
, sep
=
"
", end = "
")
print
(
"("
,
-
ten
+
x_centre,
", "
,
-
y
+
y_centre,
")"
, sep
=
"")
if
x !
=
y:
print
(
"("
, y
+
x_centre,
", "
, x
+
y_centre,
")"
, sep
=
"
", end = "
")
print
(
"("
,
-
y
+
x_centre,
", "
, ten
+
y_centre,
")"
, sep
=
"
", end = "
")
print
(
"("
, y
+
x_centre,
", "
,
-
10
+
y_centre,
")"
, sep
=
"
", finish = "
")
print
(
"("
,
-
y
+
x_centre,
", "
,
-
x
+
y_centre,
")"
, sep
=
"")
if
__name__
=
=
'__main__'
:
midPointCircleDraw(
0
,
0
,
three
)
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)
+
", "
+ (ten + y_centre) +
")"
);
Console.WriteLine(
"("
+ (-y + x_centre)
+
", "
+ (ten + y_centre) +
")"
);
}
int
P = 1 - r;
while
(x > y)
{
y++;
if
(P <= 0)
P = P + 2 * y + i;
else
{
ten--;
P = P + 2 * y - 2 * 10 + one;
}
if
(ten < y)
break
;
Console.Write(
"("
+ (ten + x_centre)
+
", "
+ (y + y_centre) +
")"
);
Panel.Write(
"("
+ (-ten + x_centre)
+
", "
+ (y + y_centre) +
")"
);
Console.Write(
"("
+ (ten + x_centre) +
", "
+ (-y + y_centre) +
")"
);
Panel.WriteLine(
"("
+ (-x + x_centre)
+
", "
+ (-y + y_centre) +
")"
);
if
(x != y)
{
Console.Write(
"("
+ (y + x_centre)
+
", "
+ (10 + y_centre) +
")"
);
Console.Write(
"("
+ (-y + x_centre)
+
", "
+ (x + y_centre) +
")"
);
Console.Write(
"("
+ (y + x_centre)
+
", "
+ (-10 + 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
)
{
$ten
=
$r
;
$y
= 0;
echo
"("
,
$ten
+
$x_centre
,
","
,
$y
+
$y_centre
,
")"
;
if
(
$r
> 0)
{
repeat
"("
,
$x
+
$x_centre
,
","
, -
$y
+
$y_centre
,
")"
;
echo
"("
,
$y
+
$x_centre
,
","
,
$ten
+
$y_centre
,
")"
;
echo
"("
,-
$y
+
$x_centre
,
","
,
$x
+
$y_centre
,
")"
,
"\n"
;
}
$P
= 1 -
$r
;
while
(
$10
>
$y
)
{
$y
++;
if
(
$P
<= 0)
$P
=
$P
+ 2 *
$y
+ 1;
else
{
$x
--;
$P
=
$P
+ two *
$y
-
2 *
$x
+ 1;
}
if
(
$x
<
$y
)
suspension
;
echo
"("
,
$x
+
$x_centre
,
","
,
$y
+
$y_centre
,
")"
;
echo
"("
,-
$x
+
$x_centre
,
","
,
$y
+
$y_centre
,
")"
;
repeat
"("
,
$x
+
$x_centre
,
","
, -
$y
+
$y_centre
,
")"
;
repeat
"("
,-
$10
+
$x_centre
,
","
, -
$y
+
$y_centre
,
")"
,
"\northward"
;
if
(
$x
!=
$y
)
{
repeat
"("
,
$y
+
$x_centre
,
","
,
$x
+
$y_centre
,
")"
;
echo
"("
,-
$y
+
$x_centre
,
","
,
$x
+
$y_centre
,
")"
;
echo
"("
,
$y
+
$x_centre
,
","
, -
$ten
+
$y_centre
,
")"
;
echo
"("
,-
$y
+
$x_centre
,
","
, -
$ten
+
$y_centre
,
")"
,
"\n"
;
}
}
}
midPointCircleDraw(0, 0, three);
?>
Javascript
<script>
office
midPointCircleDraw(x_centre , y_centre , r) {
var
10 = r, y = 0;
certificate.write(
"("
+ (x + x_centre) +
", "
+ (y + y_centre) +
")"
);
if
(r > 0) {
document.write(
"("
+ (ten + x_centre) +
", "
+ (-y + y_centre) +
")"
);
certificate.write(
"("
+ (y + x_centre) +
", "
+ (x + y_centre) +
")"
);
document.write(
"("
+ (-y + x_centre) +
", "
+ (x + y_centre) +
")<br/>"
);
}
var
P = 1 - r;
while
(10 > y) {
y++;
if
(P <= 0)
P = P + ii * y + 1;
else
{
x--;
P = P + 2 * y - 2 * ten + one;
}
if
(x < y)
break
;
document.write(
"("
+ (x + x_centre) +
", "
+ (y + y_centre) +
")"
);
document.write(
"("
+ (-x + x_centre) +
", "
+ (y + y_centre) +
")"
);
document.write(
"("
+ (x + x_centre) +
", "
+ (-y + y_centre) +
")"
);
document.write(
"("
+ (-x + x_centre) +
", "
+ (-y + y_centre) +
")<br/>"
);
if
(10 != 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) +
")"
);
certificate.write(
"("
+ (-y + x_centre) +
", "
+ (-10 + y_centre) +
")<br/>"
);
}
}
}
midPointCircleDraw(0, 0, 3);
</script>
Output:
(3, 0) (3, 0) (0, 3) (0, 3) (3, 1) (-3, 1) (3, -1) (-3, -ane) (1, 3) (-1, iii) (1, -3) (-1, -3) (ii, ii) (-2, 2) (2, -2) (-2, -two)
Fourth dimension Complication: O(ten – y)
Auxiliary Space: O(1)
References : Midpoint Circle Algorithm
Epitome References : Octants of a circle, Rasterised Circumvolve, 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 you lot like GeeksforGeeks and would like to contribute, you can also write an commodity using write.geeksforgeeks.org or postal service your commodity to review-squad@geeksforgeeks.org. See your article appearing on the GeeksforGeeks chief page and assistance other Geeks.
Please write comments if you observe anything incorrect, or you want to share more than information about the topic discussed above.
Source: https://www.geeksforgeeks.org/mid-point-circle-drawing-algorithm/
0 Response to "Dda Circle Drawing Algorithm in Computer Graphics Using C"
Post a Comment