已知A,B坐标,且A为圆心,求圆与AB连线的交点坐标
import copy
class Point(object):
def __init__(self,x=3,y=3):
self.x = x
self.y = y
A = Point(50,50) # 圆点
B = Point(3,3)
threshold = 0.01
target = copy.deepcopy(B) # 初始点
dx = A.x - B.x
dy = A.y - B.y
dx_y = dx/dy
y_step = 0.01
x_step = y_step * dx_y
choice = None
import math
moves = {}
moves["up"] = [x_step,y_step]
moves["down"] = [-x_step,-y_step]
def cal_dist(p1,p2:Point)->float:
return abs(math.sqrt((p1.x-p2.x) ** 2+(p1.y-p2.y)**2))
AB = cal_dist(A,B)
# first_move = True
# choice = None
def move(target,choice):
target.x +=moves[choice][0]
target.y +=moves[choice][1]
import math
import copy
xs = []
ys = []
def find_point(r:int):
"""
r:圆半径
target: 目标点
"""
start_loss = abs(math.sqrt((target.x-A.x) ** 2+(target.y-A.y)**2) - r) # 目标点到圆心的距离
target.x +=moves["up"][0]
target.y +=moves["up"][1]
loss =abs(math.sqrt((target.x-A.x) ** 2+(target.y-A.y)**2) - r)
if loss < start_loss:
choice = "up"
else:
choice = "down"
while loss > 0.01:
move(target,choice)
xs.append(target.x)
ys.append(target.y)
loss = abs(math.sqrt((target.x-A.x) ** 2+(target.y-A.y)**2) - r)
return target
res = find_point(1)
res.x
49.289999999998784
res.y
49.289999999998784
xs
[3.0199999999999996,
3.0299999999999994,
3.039999999999999,
3.049999999999999,
...
13.009999999999787,
...]
import matplotlib.pyplot as plt
plt.plot(xs, ys)
plt.show()