This is a simple method of selecting three points in order from the array, finding two perpendicular bisectors, and centering the intersection of them.
import numpy as np
import matplotlib.pyplot as plt
import random
#Total number of points
N = 4
#xy is input, xy_o outputs
#Both are two-dimensional arrays and shapes,[[x1, y1], [x2, y2], [x3, y3], ...]
def circle_c(xy):
#Since 3 points are used to find the center of the circle, the output is reduced by 2 points.
xy_o_len = len(xy) - 2
#Set the array for output.
xy_o = np.zeros((xy_o_len, 2))
#Find the center of the circle by 3 points in order from the front.
for i in range(xy_o_len):
#Auxiliary calculation
x12 = xy[i+0, 0] + xy[i+1, 0]
# x13 = xy[i+0, 0] + xy[i+2, 0]
x23 = xy[i+1, 0] + xy[i+2, 0]
x21 = xy[i+1, 0] - xy[i+0, 0]
# x31 = xy[i+2, 0] - xy[i+0, 0]
x32 = xy[i+2, 0] - xy[i+1, 0]
y12 = xy[i+0, 1] + xy[i+1, 1]
# y13 = xy[i+0, 1] + xy[i+2, 1]
# y23 = xy[i+1, 1] + xy[i+2, 1]
y21 = xy[i+1, 1] - xy[i+0, 1]
y31 = xy[i+2, 1] - xy[i+0, 1]
y32 = xy[i+2, 1] - xy[i+1, 1]
#Calculate the center of the circle
xy_o[i, 0] = (y31 - x21*x12/y21 + x32*x23/y32)*0.5 / (x32/y32 - x21/y21) #x component
xy_o[i, 1] = -(xy_o[i, 0] - x12 * 0.5)*x21/y21 + y12*0.5 #y component
return xy_o
x = np.arange(N)
y = np.random.rand(N)
xy = np.c_[x, y]
cxy = circle_c(xy)
f = plt.subplot()
f.plot(xy[:, 0], xy[:, 1], marker='.', markersize=20)
f.scatter(cxy[:, 0], cxy[:, 1], s=900, c="pink", alpha=0.5, linewidths="2", edgecolors="red")
f.set_aspect('equal')
plt.show()
Recommended Posts