Professional Documents
Culture Documents
Experiment 3a
Experiment 3a
Experiment 3a
1. Introduction
• The mid-point circle drawing 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
then print them along with their mirror points in the other octants. This will work because a circle is
symmetric about its centre.
2.Mathematical steps:
Suppose we have to draw a circle with equation x2+y2=r2, where r is the radius of the circle.
3.Algorithm
• Start Algorithm
• Enter value of r, which is the radius of the circle, and the point (xk,yk) which is the highest point of the
circle.
• Calculate the initial decision parameter, i.e., Pk=5/4-r.
• Assign x = xk, where x is the array of x coordinates.
• Assign y = yk, where y is the array of y coordinates.
• If Pk<0, then:
o xk+1=xk+1
o yk+1=yk
20323 ITPE12
o Pk+1=Pk+2xk+3
• Else if Pk>0, then:
o xk+1=xk+1
o yk+1=yk-1
o Pk+1=Pk+2(xk-yk)+5.
• Assign x = xk, where x is the array of x coordinates.
• Assign y = yk, where y is the array of y coordinates.
• Set pixels (x,y)
• Repeat above two steps until xk>=yk.
• End Algorithm
• Take mirror images of the points obtained from the octant across the lines:
o x=y
o x=-y
o x=0
o y=0
• Plot all the points.
4.Execution
Code:
def circle(radius):
xcoo=[]
ycoo=[]
r=radius
def mpc(xk,yk,r):
pk=1.25-r
while yk>=xk:
xcoo.append(xk)
ycoo.append(yk)
if pk>0:
pk=pk+2*(xk-yk)+5
xk+=1
yk=yk-1
elif pk<0:
pk=pk+2*xk+3
xk+=1
yk=yk
length=(xk**2+yk**2)**0.5
20323 ITPE12
xcoo.append(xk)
ycoo.append(yk)
plt.plot(xcoo,ycoo,color='black' ,linewidth=5)
mpc(0,radius,radius)
minusxcoo=[-x for x in xcoo]
minusycoo=[-y for y in ycoo]
plt.plot(ycoo,xcoo,color='black' ,linewidth=5)
plt.plot(ycoo,minusxcoo,color='black' ,linewidth=5)
plt.plot(minusxcoo,ycoo,color='black' ,linewidth=5)
plt.plot(minusxcoo,minusycoo,color='black' ,linewidth=5)
plt.plot(xcoo,minusycoo,color='black' ,linewidth=5)
plt.plot(minusycoo,xcoo,color='black' ,linewidth=5)
plt.plot(minusycoo,minusxcoo,color='black' ,linewidth=5)
circle(1000)
plt.show()
Output:
20323 ITPE12