在弧上找到一个点
我正在尝试写一个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, e_x, e_y
这里有一张可能对你有帮助的图片。
你有什么想法可以帮我写这个程序吗?
谢谢
3 个回答
解决方案
我写了这段代码:
代码
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)
类函数
init函数负责所有的初始化设置。
isoncircle函数用来检查一个点是否在圆上。
get_point函数用来获取一个结束点。
get_points函数把所有的内容总结起来,找出两个结束点。
总结
仔细看看,你可以在get_point函数中改变距离,甚至可以更改起始点。
如果有不明白的地方,告诉我哦。
在编程中,有时候我们需要把一些代码放在特定的地方,以便它们能正常工作。比如说,某些代码需要在网页加载的时候运行,而有些则需要在用户点击按钮时才执行。这样做的目的是为了让程序的运行更加高效和有序。
此外,编程语言通常会有一些规则,告诉我们在什么情况下可以使用哪些代码。这就像是一个游戏的规则,只有遵循这些规则,才能顺利进行游戏。
如果你在编写代码时遇到问题,不妨检查一下这些规则,看看你的代码是否放在了正确的位置,或者是否在合适的时机被调用。这样可以帮助你更快地找到问题所在。
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
(C-P1)^2 = R^2
首先,你需要找到圆心。圆心位于P1和P2这段线的中间垂直线的中点上。
M = (P1 + P2)/2
P12 = P2-P1 = (P12.X, P12.Y)
与P12向量垂直的线是
PP = (-P12.Y, P12.X)
圆心
C = M + PP * t
这里的t是一个参数。
你需要解这个方程来找出t的值,然后从两个可能的解中选择一个,来满足要求(比如选择大的顺时针方向)。
(例如,可以通过CP1和CP2的标量积的符号来判断)
当找到圆心后,剩下的事情就简单了:将P1绕圆心旋转(StartDistance/R)角度,将P2绕圆心旋转(-EndDistance/R)角度。