Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

mid-point line drawing

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: "))


#### Mid-Point Line Drawing Algorithm ####

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


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


### Step 02
decision_parameter = 2 * delta_y - delta_x
delta_d = 2 * (delta_y - delta_x)



### Step 03
x = x_init
y = y_init
d = decision_parameter

while(x != x_final and y != y_final):
    x_coordinates.append(x)
    y_coordinates.append(y)
    
    if d < 1:
        x += 1
        d += 2 * delta_y
    else:
        x += 1
        y += 1
        d += delta_d

# append the final point
x_coordinates.append(x_final)
y_coordinates.append(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("Mid-Point Line Drawing Algorithm")

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

plt.show()
 
PREVIOUS NEXT
Tagged: #line #drawing
ADD COMMENT
Topic
Name
9+1 =