我正在尝试编写一个在弧上找到两个点的Python或JavaScript例程。
我将使用start_distance和end_distance进入例程。我想围绕弧的圆周移动给定量,然后返回新的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, e_x, e_y
这是一张可能对您有所帮助的图片。
关于如何写这个的任何想法?
由于
答案 0 :(得分:1)
首先你需要找到圆心。它位于垂直于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(StartDistance / R)角度,并按(-EndDistance / R)角度旋转P2。
答案 1 :(得分:0)
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
答案 2 :(得分:0)
我已经编写了这段代码:
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)
初始化 功能完成所有设置。
isoncircle 功能检查圆上是否有点。
get_point 函数获取终点。
get_points 函数将所有这些求和并得到两个端点。
仔细查看,您可以在get_point函数中更改距离甚至起点。
如果其中任何一个不清楚,请告诉我。