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 :: python how to loop through array 
Python :: if statment with logical or operator for multiple varaibles 
Python :: How to obtain a jpeg resolution in python 
Python :: Python __ne__ 
Python :: __ge__ 
Python :: create different size matplotlib 
Python :: NumPy rot90 Syntax 
Python :: python service linux 
Python :: godot knockback 
Python :: NumPy bitwise_or Code When inputs are Boolean 
Python :: URL to origin python 
Python :: Printing a long code line to span over multiple lines 
Python :: #check if the given date is a weekday or weekend 
Python :: adjoint of 3x3 matrix in numpy 
Python :: send http request from python with quesry 
Python :: raspberry pi set python 3 as default 
Python :: beautifulsoup - extracting link, text, and title within child div 
Python :: python extract multiple values from a single cell in a dataframe column using pandas 
Python :: browser environment: 
Python :: multiply each element by x in python 
Python :: Flask_SQLAlchemy is claiming my value is not boolean 
Python :: python pandas to visualise the tangent of a curve 
Python :: walrus with ternary python 
Python :: python entry element 
Python :: swagger django 
Python :: how to insert a character into a string in python 
Python :: python alphabet to number 
Python :: player to walk on the surface 
Python :: Matplotlib-Object oriented interface 
Python :: real python linear regression 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =