Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

DDA line drawing algorithm

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 string continue next line 
Python :: split paragraphs in python 
Python :: python sqrt 
Python :: print without newline 
Python :: add horizontal line to plotly scatter 
Python :: a int and float. python 
Python :: django login 
Python :: activate virtual environment 
Python :: program to add first and last digit of a number in python 
Python :: oops concept in python 
Python :: f readlines python not working 
Python :: how to remove role from people with a reaction discord bot python 
Python :: matplotlib 
Python :: how to check if a variable holds a class reference in python 
Python :: hide tkinter window 
Python :: numpy copy a array vertical 
Python :: flask on gevent over https 
Python :: pylab plotting data 
Python :: pafy doc 
Python :: python os get dir path 
Python :: string to list python 
Python :: get files in directory and subdirectory 
Python :: get mode using python 
Python :: python not equal to symbol 
Python :: print binary tree python 
Python :: what is the difference between accuracy and loss 
Python :: python empty dataframe 
Python :: pandas if value present in df index 
Python :: datetime print the current time 
Python :: for loop with index python 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =