本文整理汇总了Python中Vector.Vector.distance_to方法的典型用法代码示例。如果您正苦于以下问题:Python Vector.distance_to方法的具体用法?Python Vector.distance_to怎么用?Python Vector.distance_to使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Vector.Vector
的用法示例。
在下文中一共展示了Vector.distance_to方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from Vector import Vector [as 别名]
# 或者: from Vector.Vector import distance_to [as 别名]
class Wall:
def __init__(self, ax, ay, bx=0.0, by=0.0):
"Start and end are Vectors"
if isinstance(ax, Vector) and isinstance(ay, Vector):
self.start = ax
self.end = ay
else:
self.start = Vector(ax, ay)
self.end = Vector(bx, by)
def projection(self, p):
"""Return the projection of a point on this wall.
If the point is outside the wall, return the nearest end point."""
# From http://www.codeguru.com/forum/showthread.php?t=194400:
#
# Let the point be C (Cx,Cy) and the line be AB (Ax,Ay) to (Bx,By).
# Let P be the point of perpendicular projection of C on AB. The parameter
# r, which indicates P's position along AB, is computed by the dot product
# of AC and AB divided by the square of the length of AB:
# (1) AC dot AB
# r = ---------
# ||AB||^2
# r has the following meaning:
# r=0 P = A
# r=1 P = B
# r<0 P is on the backward extension of AB
# r>1 P is on the forward extension of AB
# 0<r<1 P is interior to AB
# The point P can then be found:
# Px = Ax + r(Bx-Ax)
# Py = Ay + r(By-Ay)
# And the distance from A to P = r*L."""
AC = p-self.start
AB = self.end-self.start
r = AC.dot(AB)/self.length()**2
if r > 1:
# The point p is past the end of the wall
return self.end
if r < 0:
# The point p is past the beginning of the wall
return self.start
return Vector(self.start.x + r*(self.end.x-self.start.x),
self.start.y + r*(self.end.y-self.start.y))
def distance_to(self, p):
"""The shortest distance from the wall to point p."""
return self.projection(p).distance_to(p)
def length(self):
return self.start.distance_to(self.end)
def clone(self):
return Wall(self.start.clone(), self.end.clone())