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 json serialize print pretty 
Python :: pytorch Jaccard Index 
Python :: mypy run on single file 
Python :: Python __le__ 
Python :: Python how to use __truediv__ 
Python :: Exception has occurred: FileNotFoundError 
Python :: NumPy fliplr Example 
Python :: pandas listagg equivalent in python 
Python :: python multiply function with return keyword 
Python :: NumPy bitwise_or Code When inputs are arrays 
Python :: django disable foreign key checks temporary 
Python :: should either include a `queryset` attribute, 
Python :: flatten a list using numpy and itertools 
Python :: adjugate of 3x3 matrix in python 
Python :: how to change voice in pyttsx3 
Python :: valid paranthesis 
Python :: from android.runnable in python 
Python :: Compress multiple directories but exclude directory - Python zipfile(or anything native to Windows 2012+ 
Python :: Examples of correct code for this rule with global declaration: 
Python :: empty list 
Python :: Flask application displaying list of items from SQL database as text 
Python :: Converting Data Types 
Python :: sqlalchemy validation at db level 
Python :: clock replacement algorithm python 
Python :: how to enter tavble in sal through sql 
Python :: rpi python read spi 
Python :: Error: Directory not empty @ dir_s_rmdir - /usr/local/Cellar/python/3.7.3 
Python :: how to execute more than one line of code in one line python 
Python :: dataframe from function 
Python :: Print Wavelet modes 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =