英文:
Determine the coefficients of the line
问题
确定平面上的N个点。确定通过第一个点和剩下的一个点之间的线y=kx+b的系数,以便所有N个点都位于这条线的同一侧,也可能位于该线上。我需要至少得到问题解决方案的数学描述。
英文:
There are N points on the plane. Determine the coefficients of the line y=kx+b passing through the first and one of the remaining points so that all N points lie on the same side of this line, and perhaps on the line itself.
I need help with at least a mathematical description of the solution to the problem
答案1
得分: 0
计算你的第一个点与其他每个点之间的角度。选择形成最小或最大角度的点,并将该点用作你线的第二个点。
英文:
Calculate the angle between your first point and every other point. Take the point that forms the lowest or the highest angle and use that point as the second point of your line.
答案2
得分: -1
根据Michael Cao的解释,你需要考虑第一个点和其他点之间的角度。假设所有点都具有正的 x
和 y
坐标,具有最高角度的点也将具有最高的角度的 tan
。这个切线可以很容易地计算出来:
def tangent(x1, y1, x2, y2):
return (y2-y1) / (x2-x1)
因此,你只需要找到具有最高切线的点:
def highest_point(points):
x1, y1 = points[0] # 第一个点的坐标
maxi = (0, None)
for x, y in points[1:]:
if tangent(x1, y1, x, y) > maxi[0]:
maxi = (tangent(x1, y1, x, y), (x, y))
return maxi[1]
最后,只需计算通过第一个点和最高点的直线的系数:
def coefficients(points):
x1, y1 = points[0]
x2, y2 = highest_point(points)
print("k =", (y2-y1) / (x2-x1)) # 系数就是切线
print("b =", y1 - (x1 * (y2-y1) / (x2-x1)))
(最后一个公式中的 b
可以在几何上验证:原点处的顺序是在 x1
、y1
处的顺序,减去了“从 0
到 x1
的斜率上升了多少”)
英文:
As explained by Michael Cao, you need to think in term of angles between the first and the other points. Assuming all the points have positive x
and y
coordinates, the point with the highest angle will also have the highest tan
of the angle. This tangent can be calculated easily:
def tangent(x1, y1, x2, y2):
return (y2-y1) / (x2-x1)
Hence, you just need to find the point with the highest tangent:
def highest_point(points):
x1, y1 = points[0] # coordinates of the first point
maxi = (0, None)
for x, y in points[1:]:
if tangent(x1, y1, x, y) > maxi[0]:
maxi = (tangent(x1, y1, x, y), (x, y))
return maxi[1]
And finally, just compute the coefficients of the line passing through the first and highest points:
def coefficients(points):
x1, y1 = points[0]
x2, y2 = highest_point(points)
print("k =", (y2-y1) / (x2-x1)) # the coefficient is the tangent
print("b =", y1 - (x1 * (y2-y1) / (x2-x1)))
(The last formula for b
can be verified geometrically: the ordered at the origin is the ordered in x1
, y1
, minus "how much the slope went up from 0
to x1
")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论