geometry.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:sketch-components 作者: ibhubs 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号