Pau*_*cks -4 python trigonometry
我在圆上有2个点,并且它们之间有一个角度,我想找到这样定义的圆的中心(嗯,最好是两个中心)。

def find_center(p1,p2,angle):
# magic happens... What to do here?
return (center_x, center_y)
Run Code Online (Sandbox Code Playgroud)
这是我的测试代码解决方案
from pylab import *
from numpy import *
def find_center(p1, p2, angle):
# End points of the chord
x1, y1 = p1
x2, y2 = p2
# Slope of the line through the chord
slope = (y1-y2)/(x1-x2)
# Slope of a line perpendicular to the chord
new_slope = -1/slope
# Point on the line perpendicular to the chord
# Note that this line also passes through the center of the circle
xm, ym = (x1+x2)/2, (y1+y2)/2
# Distance between p1 and p2
d_chord = sqrt((x1-x2)**2 + (y1-y2)**2)
# Distance between xm, ym and center of the circle (xc, yc)
d_perp = d_chord/(2*tan(angle))
# Equation of line perpendicular to the chord: y-ym = new_slope(x-xm)
# Distance between xm,ym and xc, yc: (yc-ym)^2 + (xc-xm)^2 = d_perp^2
# Substituting from 1st to 2nd equation for y,
# we get: (new_slope^2+1)(xc-xm)^2 = d^2
# Solve for xc:
xc = (d_perp)/sqrt(new_slope**2+1) + xm
# Solve for yc:
yc = (new_slope)*(xc-xm) + ym
return xc, yc
if __name__=='__main__':
p1 = [1., 2.]
p2 = [-3, 4.]
angle = pi/6
xc, yc = find_center(p1, p2,angle)
# Calculate the radius and draw a circle
r = sqrt((xc-p1[0])**2 + (yc-p1[1])**2)
cir = Circle((xc,yc), radius=r, fc='y')
gca().add_patch(cir)
# mark p1 and p2 and the center of the circle
plot(p1[0], p1[1], 'ro')
plot(p2[0], p2[1], 'ro')
plot(xc, yc, 'go')
show()
Run Code Online (Sandbox Code Playgroud)