此功能应该采取哪些将被用来找到最接近点它,就在于线段对象上的点参数。 在该示例中断言码函数getClosestPoint(Point())
取Point(10, 0)
作为参数,并且应该返回Point(5,5)
是最接近的点至Point(10, 0)
即上的线l1 = Line(Point(5,5), Point(20,35))
随着终点为A Point(5,5), B Point(20,35)
我不知道如何去解决这个问题。 我目前的解决方案将返回(4,3),这是不是就行了段,但就行了。
from point import Point
import math
class Line:
def __init__(self,aPoint=Point(), bPoint=Point()):
self.firstPoint = aPoint
self.secondPoint = bPoint
def getClosestPoint(self,point=Point()):
m1 = self.getSlope()
m2 = -1 / float(m1)
b1 = self.p1.y - m1 * self.p1.x
b2 = point.y - m2 * point.x
x = float(b2 - b1) / float(m1 - m2)
y = m1 * x + b1
return Point(x, y)
if __name__ == "__main__":
p1 = Point(5,5)
p2 = Point(20,35)
l1 = Line(p1,p2)
assert l1.getClosestPoint(Point(10,0)) == Point(5,5)
assert l2.getClosestPoint(Point(25/2,25/2)
class Point:
def __init__(self,x=0,y=0):
self.x = x
self.y = y