Search
 
SCRIPT & CODE EXAMPLE
 

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()
Comment

PREVIOUS NEXT
Code Example
Python :: (ax=self.canv.axes ,style="ro--") 
Python :: max index tuple 
Python :: python pandas read parquet with progressbar 
Python :: Python __ge__ 
Python :: Python __truediv__ magic method 
Python :: find max in for scartch python 
Python :: NumPy unique Example Identify the index of the first occurrence of unique values 
Python :: setstylesheet python 
Python :: how to split a string every 2 characters python 
Python :: NumPy bitwise_or Code When inputs are numbers 
Python :: Convertion of an array into binary using NumPy binary_repr 
Python :: ax bar different colors 
Python :: # convert dictionary keys to a list 
Python :: dictionary display 
Python :: python truncade number 
Python :: numpy extract decimal 
Python :: how to remove a strech in pyqt5 
Python :: python Tkinter widget displacement with pack() 
Python :: Examples of incorrect code for this rule: 
Python :: long armstrong numbers 
Python :: how to show type of a variable 
Python :: Determining the Data Type 
Python :: Python-Specific Operators 
Python :: flask-sqlalchemy inserting a dictionary to a database 
Python :: plt datas use left and right yaxes 
Python :: Python cut down OS path to certain part 
Python :: dateentry python centered 
Python :: modules django 
Python :: websocket communitation to another pc python 
Python :: phobia of butterflies 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =