Experiment 1

You might also like

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

Page 1

Experiment 1(A)

DDA (Digital Differential Analyzer) Algorithm:

Introduction

● DDA stands for Digital Differential Analyzer.


● It is an incremental method of scan conversion of lines.
● In this method calculation is performed at each step but by using results of previous steps.

Mathematical steps:

1. Take x1, y1, x2, y2


2. Find dx = x2-x1 and dy = y2-y1
3. Find steps = max(abs(dx), abs(dy))
𝑑𝑥 𝑑𝑦
4. Calculate xincr= 𝑛𝑜. 𝑜𝑓 𝑠𝑡𝑒𝑝𝑠 and yincr= 𝑛𝑜. 𝑜𝑓 𝑠𝑡𝑒𝑝𝑠

5. Now x = x1 + xincr and y = y1 + yincr


6. Now x1 = x and y1 = y
7. x’ = round(x) and y’ = round(y)
8. Ans = (x’,y’)

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

Create a straight line using the DDA algorithm.

Execution:

import matplotlib.pyplot as plt

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

Create 5 straight non-intersecting lines using the DDA algorithm.

Execution:

import matplotlib.pyplot as plt

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:

import matplotlib.pyplot as plt

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

You might also like