Experiment 3a

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Experiment 3(a)

Mid-Point Circle Drawing Algorithm

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.

➢ Assume we have a point (xi,yi).


➢ When we put the above point in equation of circle, we can get the following results:
1. When the result=0, it means that the point (xi,yi) lies on the circle.
2. When the result>0, it means that the point (xi,yi) lies outside the circle.
3. When the result<0, it means that the point (xi,yi) lies inside the circle.
➢ Let (x1,y1) be the midpoint of the south-east point and east point of a point (xk,yk) lying on highest point
of the circle.
➢ Then, x1=(xk+1+xk+1)/2=xk+1 and y1=(yk+yk-1)/2=yk-0.5
➢ If (x1,y1) lies inside, then we take the east point, else we consider the south east point as the next point
for the circle.
➢ The initial decision parameter will be Pk=5/4-r.
➢ Now if Pk<0 then xk+1=xk+1, yk+1=yk, Pk+1=Pk+2xk+3.
➢ If Pk>0 then xk+1=xk+1, yk+1=yk-1, Pk+1=Pk+2(xk-yk)+5.
➢ This will form the octant 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

Create a circle using Mid-Point Circle Drawing Algorithm.

Code:

import matplotlib.pyplot as plt


plt.figure(figsize=(12,12))
import time as t

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

You might also like