def pdistance(self, p):
"""Perpendicular distance between this Segment and a given Point p"""
if not isinstance(p, Point):
return NotImplemented
if self.start == self.end:
# Distance from a Point to another Point is length of a segment
return Segment(self.start, p).length()
s = self.end - self.start
if s.x == 0:
# Vertical Segment => pdistance is the difference of abscissa
return abs(self.start.x - p.x)
else:
# That's 2-D perpendicular distance formulae (ref: Wikipedia)
slope = s.y / s.x
# intercept: Crossing with ordinate y-axis
intercept = self.start.y - (slope * self.start.x)
return abs(slope * p.x - p.y + intercept) / math.sqrt(
slope ** 2 + 1)
评论列表
文章目录