-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgen_circle.py
54 lines (42 loc) · 1.85 KB
/
gen_circle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import numpy as np
import numpy.matlib
# uses midpoint circle algorithm: https://en.wikipedia.org/wiki/Midpoint_circle_algorithm
# Richard Arthurs port from C > Python, with addition of circle filler
def gencircle(radius, x0, y0):
x = radius - 1
y = 0
dx = 1
dy = 1
err = dx - 2*radius
c = []
while x >= y:
c.append([x0 + x, y0 + y])
c.append([x0 + y, y0 + x])
c.append([x0 - y, y0 + x])
c.append([x0 - x, y0 + y])
c.append([x0 - x, y0 - y])
c.append([x0 - y, y0 - x])
c.append([x0 + y, y0 - x])
c.append([x0 + x, y0 - y])
if err <= 0:
y = y + 1
err = err + dy
dy = dy + 2
else:
x = x - 1
dx = dx + 2
err = err + dx - 2*radius
c = np.asarray(c) # convert to numpy
uniques = np.vstack({tuple(row) for row in c}) # the unique points
extrapts = ([0,0]) # to contain
# fill in the circles. Note: we don't actually need to do this for the algorithm to work.
for row in uniques: # find the max and min y values for each x. Create the intermediate y's at each x to fill in the circle
miny = c[c[:,0] == row[0], 1].min() # logical indexing
maxy = c[c[:,0] == row[0], 1].max()
tempy = np.arange(miny, maxy+1) # range of y's to make the fill
tempx = np.matlib.repmat(row[0],np.size(tempy,0),1) # the x indices
# horzcat the tempx and tempy, then stack that onto existing final
extrapts = np.vstack((extrapts,np.column_stack((tempx, tempy)))) # https://stackoverflow.com/questions/14741061/concatenating-column-vectors-using-numpy-arrays
extrapts = np.delete(extrapts,0,axis = 0) # delete the initial row of zeros used in construction
# return np.vstack((c,extrapts)) # stack fill in points onto the original outline points
return c