Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Digital Differential Analyzer

import numpy as np
import matplotlib.pyplot as plt


### Data input
x_init = int(input("Enter starting x-coordinates: "))
y_init = int(input("Enter starting y-coordinates: "))

x_final = int(input("Enter ending x-coordinates: "))
y_final = int(input("Enter ending y-coordinates: "))



#### Digital Differential Analyzer (DDA) Line Drawing Algorithm ####

### Initializations
x_coordinates = []
y_coordinates = []


### Step 01
delta_x = x_final - x_init
delta_y = y_final - y_init
m = delta_y / delta_x


### Step 02
steps = np.max([delta_x, delta_y])


### Step 03
x = x_init
y = y_init

if m < 1:
    for i in range(steps):
        x_coordinates.append(np.ceil(x))
        y_coordinates.append(np.ceil(y))
        x += 1
        y += m
elif m == 1:
    for i in range(steps):
        x_coordinates.append(np.ceil(x))
        y_coordinates.append(np.ceil(y))
        x += 1
        y += 1
else:
    for i in range(steps):
        x_coordinates.append(np.ceil(x))
        y_coordinates.append(np.ceil(y))
        x += 1/m
        y += 1
    
# append the final point
x_coordinates.append(np.ceil(x_final))
y_coordinates.append(np.ceil(y_final))

# transform coordinates into a numpy array and print them out
x_coordinates = np.array(x_coordinates, dtype = int)
y_coordinates = np.array(y_coordinates, dtype = int)

print(np.vstack((x_coordinates, y_coordinates)).T)


### Step 04
plt.plot(x_coordinates, y_coordinates)

plt.title("DDA Line Drawing Algorithm")

plt.xlabel("x-axis")
plt.ylabel("y-axis")

plt.show()
Comment

PREVIOUS NEXT
Code Example
Python :: python for loop index 
Python :: install scrapy on pycharm 
Python :: pandas replace values 
Python :: freecodecamp python 
Python :: xml depth python 
Python :: make sure it only has letters and numbers python 
Python :: django login required 
Python :: pca 
Python :: using Decorators 
Python :: comtypes python 
Python :: python get total gpu memory 
Python :: how to remove role discord bot python 
Python :: python for dummies 
Python :: how to print during multiprocessing 
Python :: matrix diagonal sum leetcode 
Python :: Disctionary to Array 
Python :: flask app with spark 
Python :: python tkinter checkbox default value 
Python :: trim string to max length python 
Python :: python command line keyword arguments 
Python :: pandas dataframe from list how to make the date column an index 
Python :: remove empty string from list python single line 
Python :: django from 
Python :: not equal to python 
Python :: python extract all characters from string before a character 
Python :: covariance in python 
Python :: python string encode 
Python :: lucky number codechef solution 
Python :: create_polygon tkinter 
Python :: get discord guild members discord.py 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =