Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

midpoint circle drawing algorithm

import numpy as np
import matplotlib.pyplot as plt


### Data input
x_cent = int(input("Enter x-coordinates of the center: "))
y_cent = int(input("Enter y-coordinates of the center: "))
r = int(input("Enter the radius of the circle: "))



#### Mid-Point Circle Drawing Algorithm ####

### Initializations
x_quad1 = []
y_quad1 = []
decor = []


### Step 01
x_init = 0
y_init = r


### Step 02
decision_parameter = 1 - r


### Step 03: First octant
x = x_init
y = y_init
p = decision_parameter

while(x < y):
    x_quad1.append(x)
    y_quad1.append(y)
    
    if p < 0:
        x += 1
        p += 1 + 2 * x
    else:
        x += 1
        y -= 1
        p += 1 - 2 * y + 2 * x

# append the final point
x_quad1.append(x)
y_quad1.append(y)

# append the second octant to the first quadrant
for i in range(len(x_quad1)):
    x_quad1.append(y_quad1[i])
    y_quad1.append(x_quad1[i])

# Second Quadrant, WRT center coordinates
x_quad2 = [(x_cent - x) for x in x_quad1]
y_quad2 = [(y_cent + y) for y in y_quad1]

# Third Quadrant, WRT center coordinates
x_quad3 = [(x_cent - x) for x in x_quad1]
y_quad3 = [(y_cent - y) for y in y_quad1]

# Fourth Quadrant, WRT center coordinates
x_quad4 = [(x_cent + x) for x in x_quad1]
y_quad4 = [(y_cent - y) for y in y_quad1]

# First Quadrant, WRT center coordinates
x_quad1 = [(x_cent + x) for x in x_quad1]
y_quad1 = [(y_cent + y) for y in y_quad1]

# transform coordinates into a numpy array and print them out
x_quad1 = np.array(x_quad1, dtype = int)
y_quad1 = np.array(y_quad1, dtype = int)
x_quad2 = np.array(x_quad2, dtype = int)
y_quad2 = np.array(y_quad2, dtype = int)
x_quad3 = np.array(x_quad3, dtype = int)
y_quad3 = np.array(y_quad3, dtype = int)
x_quad4 = np.array(x_quad4, dtype = int)
y_quad4 = np.array(y_quad4, dtype = int)

print(np.vstack((x_quad1, y_quad1, x_quad2, y_quad2, 
                 x_quad3, y_quad3, x_quad4, y_quad4)).T)


### Step 04
fig = plt.figure()
ax = fig.add_subplot(111)

plt.plot(x_quad1, y_quad1, x_quad2, y_quad2, x_quad3, y_quad3, x_quad4, y_quad4)

ax.set_aspect('equal', adjustable='box')

plt.title("Bresenham Circle Drawing Algorithm")

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

plt.show()
Comment

PREVIOUS NEXT
Code Example
Python :: remove rows from a dataframe that are present in another dataframe? 
Python :: python add to dictionary 
Python :: python print ling line in print 
Python :: python convert docx to pdf 
Python :: how to swirtch the placement of the levels in pandas 
Python :: split df coliumn 
Python :: plotting mean in density plot ggplot2 
Python :: custom dataset pytorch 
Python :: python remove everything except numbers from string 
Python :: iterrows pd 
Python :: import statsmodels as sm 
Python :: states and capitals us comma separated list 
Python :: return all values in a list python 
Python :: convert numpy array to HSV cv 
Python :: class chain methods python 
Python :: :: python 
Python :: python string assignment by index 
Python :: importing time and sleep. python 
Python :: Finding if 2 consecutive numbers in a list have a sum equal to a given number 
Python :: puthon for loop 
Python :: how to use return python 
Python :: sftp python 
Python :: np random list 
Python :: airflow schedule interval timezone 
Python :: python string replace 
Python :: python generate tuple from lists 
Python :: requesting with aiohttp 
Python :: pydrive download by url 
Python :: clipboard python 
Python :: python kivy bind 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =