Professional Documents
Culture Documents
Experiment 1
Experiment 1
Experiment 1
Experiment 1(A)
Introduction
Mathematical steps:
Algorithm steps:-
1. Start
2. Read x1,y1,x2,y2
3. Compute dx = x2-x1 and dy = y2-y1
4. Compute step = abs(max(dx,dy))
5. Compute xinc=dx/step and yinc=dy/step
6. while i<=step:
i+=1
x1+=xinc
y1+=yinc
x=x1
y=y1
show round(x)
show round(y)
7. End
20304
Page 2
Execution:
x1=abs(int(input("Enter x1:")))
y1=abs(int(input("Enter y1:")))
x2=abs(int(input("Enter x2:")))
y2=abs(int(input("Enter y2:")))
dx=(x2-x1)
dy=(y2-y1)
step=abs(max(dx,dy))
xinc=dx/step
yinc=dy/step
i=1
xcoo=[]
ycoo=[]
xcoo.append(x1)
ycoo.append(y1)
while i<=step:
i+=1
x1+=xinc
y1+=yinc
xcoo.append(x1)
ycoo.append(y1)
plt.plot(xcoo,ycoo,color="green")
plt.ylim(ymin=0)
plt.xlim(xmin=0)
for i, j in zip(xcoo, ycoo):
plt.text(i, j+0.5, '({}, {})'.format(i, j))
plt.show()
20304
Page 3
Input:
Enter x1:2
Enter y1:7
Enter x2:9
Enter y2:15
Output:
20304
Page 4
Experiment 1B
Execution:
def line(lx1,ly1,lx2,ly2):
lx=[]
ly=[]
dx=(lx2-lx1)
dy=(ly2-ly1)
step=abs(max(dx,dy))
xinc=dx/step
yinc=dy/step
i=1
lx.append(lx1)
ly.append(ly1)
while i<=step:
i+=1
lx1=(lx1+xinc)
ly1=(ly1+yinc)
lx.append(round(lx1))
ly.append(round(ly1))
plt.plot(lx,ly)
lx.clear()
ly.clear()
line(1,2,1,7)
line(2,1,2,6)
line(3,3,8,8)
line(4,2,8,2)
line(5,1,9,1)
plt.ylim(ymin=0)
plt.xlim(xmin=0)
plt.show()
20304
Page 5
Output:
20304
Page 6
Experiment 1C
Create a 2-Dimensional image using straight lines with the DDA algorithm.
Execution:
def line(x1,y1,x2,y2,linecolor,thickness):
lx=[]
ly=[]
dx=(x2-x1)
dy=(y2-y1)
step=abs(max(dx,dy))
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(round(x1))
ly.append(round(y1))
plt.plot(lx,ly,color=linecolor,linewidth=thickness)
lx.clear()
ly.clear()
line(1,1,1,9,'black',3)
line(1,9,13,9,'black',3)
line(13,1,13,9,'black',3)
line(1,1,13,1,'black',3)
line(5,3,9,3,'red',7)
line(3,5,3,7,'blue',7)
line(3,7,5,7,'blue',7)
line(5,5,5,7,'blue',7)
line(3,5,5,5,'blue',7)
line(9,5,9,7,'blue',7)
line(9,7,11,7,'blue',7)
line(11,5,11,7,'blue',7)
20304
Page 7
line(9,5,11,5,'blue',7)
plt.show()
Output:
20304