Javascript 沿圆弧寻找一点
我正在尝试编写一个Python或JavaScript例程,在圆弧上找到两点 我将以开始距离和结束距离开始例行训练。我想绕弧的圆周移动一个给定的量,然后返回新的x和y。实际上,我正在尝试移动弧的起点和终点 我对例行程序的输入将是Javascript 沿圆弧寻找一点,javascript,python,math,geometry,Javascript,Python,Math,Geometry,我正在尝试编写一个Python或JavaScript例程,在圆弧上找到两点 我将以开始距离和结束距离开始例行训练。我想绕弧的圆周移动一个给定的量,然后返回新的x和y。实际上,我正在尝试移动弧的起点和终点 我对例行程序的输入将是 def get_points_on_an_arc(x1, y1, x2, y2, radius, large, clockwise, start_distance, end_distance) # logic here return s_x, s_y,
def get_points_on_an_arc(x1, y1, x2, y2, radius, large, clockwise, start_distance, end_distance)
# logic here
return s_x, s_y, e_x, e_y
这是一张可能对你有帮助的图片
你知道怎么写吗
谢谢首先你需要找到圆心。它位于垂直于P1-P2段的中部。中间点
M = (P1 + P2)/2
P12 = P2-P1 = (P12.X, P12.Y)
垂直于P12的向量是
PP = (-P12.Y, P12.X)
中心C=M+PP*t
其中t是参数。你们需要解这个方程
(C-P1)^2 = R^2
针对t参数,然后从两种可能的解决方案中选择一种来满足要求(大,顺时针)(例如,通过CP1和CP2的标量积的符号) 找到中心后,问题的其余部分很容易解决:围绕中心旋转P1(起始距离/R)角,旋转P2(-EndDistance/R)角。解决方案 我编写了以下代码: 代码 类函数 init功能完成所有设置 isoncircle功能检查圆上是否有点 get_point函数获取一个端点 get_points函数将所有数据相加,得到两个端点 总结 仔细查看,您可以在get_point函数中更改距离,甚至更改起点
如果有任何不清楚的地方,请告诉我。您能解释所有传递参数的含义以及返回值成员吗?这里哪一个是“给定量”?我刚刚添加了一个图像来帮助确定,距离是弧度还是rad*r?而
large=false
只是说“从(x1,y1)顺时针方向”的另一种方式?我认为它在弧度中,两个点和一个半径并不定义唯一的圆;有两种可能的解决办法。
class Circle(object):
def __init__(self, center=(0,0), radius=1):
""" Center and radius define a unique circle """
self._center = center
self._radius = radius
...
def isOn(self, point):
if point_on_circle:
return True
return False
...
def get_new_point(self, start=(1,0), distance=1, direction='clockwise'):
""" Inputs:
circle - instance of the Circle() class
start - tuple of (x, y) the exists on circle
distance - distance to travel in radians
direction - 'clockwise' or 'widdershins'
"""
assert circle.isOn(start), "Start point is NOT on the circle"
# code to calculate new point here - basic geometry, use Google
end = ...
return end
def get_points_on_an_arc(self, x1, y1, x2, y2, large, clockwise, start_distance, end_distance):
""" Your function signature will ONLY work if it is a class method
The alternative is to change the signature and explicitly pass in a Circle() object to the function
"""
s_x, s_y = get_new_point(start=(x1, y1), distance=start_distance, direction='clockwise')
e_x, e_y = get_new_point(start=(x2, y2), distance=end_distance, direction='widdershins')
return s_x, s_y, e_x, e_y
class Circle:
def __init__(self, rds):
'''rds:radius delete:ctr:center of circle'''
self.rds = rds
def isoncircle(self, point):
'''a check if point is on circle'''
if point_on_circle:
return True
return False
def get_point(self, start=[0, 0], distance=2, direction='c'):
'''start:bottom point distance:distance to move up or down direction:move up or down'''
if direction == 'c':
cme = [start[0]+abs(distance-self.rds), start[1]+abs(distance-self.rds)]
elif direction == 'cou':
start = [start[0], start[1]-(self.rds*2)]
cme = [start[0]-abs(distance-self.rds), start[1]-abs(distance-self.rds)]
if self.isoncircle(cme):
return cme
def get_points(self, sd, ed):
s_x, s_y = self.get_point(distance=sd, direction='c')
e_x, e_y = self.get_point(distance=ed, direction='cou')
return s_x, s_y, e_x, e_y
circle = Circle(4)
circle.get_points(4, 4)