You are on page 1of 7

1

Experiment-2A

Bresenham's Line Drawing Algorithm

Introduction

● This algorithm helps us to perform scan conversion of a line.


● It is a powerful, useful, and accurate method.
● We use incremental integer calculations to draw a line.
● The integer calculations include addition, subtraction, and multiplication.

Mathematical steps:

1. Take x1,y1,x2,y2
2. Calculate dx = x2 - x1 and dy = y2 - y1
3. Find Pk = 2dy -dx
4. Cases
a. if Pk >=1 then

Pk+1 = Pk +2 (dy - dx)

xk+1 = xk + 1

yk+1 = yk +1

b. if Pk <1 then

Pk+1 = Pk +2 dy

xk+1 = xk + 1

yk+1 = yk

Algorithm Steps:-

1. Start
2. Read x1,y1,x2,y2
3. Compute dx = x2-x1 and dy = y2-y1
4. Compute Pk = 2dy -dx
5. if Pk >=1 then

Pk = Pk +2 (dy - dx)

x1 = x1 + 1

y1 = y1 +1
2

else

Pk = Pk +2 dy

x1 = x1 + 1

y1 = y1

6. Repeat step 5 until x1=x2 and y1=y2


7. End

20304
3

Create a straight line using Bresenham's line drawing algorithm.

Code:

import matplotlib.pyplot as plt

x0=abs(int(input("Enter x0:")))
y0=abs(int(input("Enter y0:")))
xn=abs(int(input("Enter xn:")))
yn=abs(int(input("Enter yn:")))

def linebresen(x1,y1,x2,y2,linecolor):
dx=x2-x1
dy=y2-y1
p=2*dy-dx
xcoo=[]
ycoo=[]
i=x2-x1
while i>=0:
i-=1
xcoo.append(x1)
ycoo.append(y1)
if p>=0:
p=p+2*(dy-dx)
x1+=1
y1+=1
else:
p=p+2*dy
x1+=1
y1=y1
plt.plot(xcoo,ycoo,color=linecolor)

linebresen(x0,y0,xn,yn,'purple')
plt.ylim(ymin=0)
plt.xlim(xmin=0)
plt.show()

Input:

Enter x0:2

20304
4

Enter y0:2
Enter xn:7
Enter yn:7

Output:

20304
5

Experiment-2B

Create a 2-Dimensional image using Bresenham’s line drawing algorithm and DDA algorithm.

Code:

import matplotlib.pyplot as plt

def line(x1,y1,x2,y2,linecolor,thickness,step):

lx=[]
ly=[]
dx=(x2-x1)
dy=(y2-y1)
xinc=dx/step
yinc=dy/step
i=1
lx.append(x1)
ly.append(y1)
while i<=step:
i+=1
x1=(x1+xinc)
y1=(y1+yinc)
lx.append(x1)
ly.append(y1)
plt.plot(lx,ly,color=linecolor,linewidth=thickness)
lx.clear()
ly.clear()

def linebresen(x1,y1,x2,y2,linecolor,thickness):
dx=x2-x1
dy=y2-y1
p=2*dy-dx
xcoo=[]
ycoo=[]
i=x2-x1
while i>=0:
i-=1
xcoo.append(x1)
ycoo.append(y1)
if p>=0:

20304
6

p=p+2*(dy-dx)
x1+=1
y1+=1
else:
p=p+2*dy
x1+=1
y1=y1
plt.plot(xcoo,ycoo,color=linecolor,linewidth=thickness)
linebresen(8,8,12,10,'black',3)
linebresen(9,8,12,10,'black',3)
line(1,1,1,5,'purple',3,4)
line(1,5,3,7,'red',3,4)
line(3,7,5,5,'red',3,2)
line(1,5,5,5,'purple',3,4)
line(5,5,5,1,'purple',3,4)
line(1,1,5,1,'purple',3,4)
line(3,7,9,7,'red',3,6)
line(5,5,11,5,'red',3,6)
line(9,7,11,5,'red',3,2)
line(11,5,11,1,'purple',3,4)
line(5,1,11,1,'purple',3,6)
line(8,7,8,8,'red',3,1)
line(8,8,9,8,'red',3,1)
line(9,8,9,7,'red',3,1)
# door
line(2.5,4,3.5,4,'blue',3,2)
line(2.5,4,2.5,1.5,'blue',3,2)
line(2.5,1.5,3.5,1.5,'blue',3,2)
line(3.5,1.5,3.5,4,'blue',3,2)
# window
line(6.5,4,9.5,4,'blue',3,2)
line(6.5,4,6.5,2,'blue',3,2)
line(6.5,2,9.5,2,'blue',3,2)
line(9.5,2,9.5,4,'blue',3,2)
line(8,4,8,2,'blue',3,2)
line(6.5,3,9.5,3,'blue',3,2)
plt.show()

Output:

20304
7

20304

You might also like