计算几何问题汇总–点与线的位置关系

点与点之间的距离

1. 点类的定义

class Point(object): “””Point are two-dimension””” def __init__(self, x, y): self.x = x
self.y = y

2. 两点之间距离的计算

import numpy as np # numpy help us do some vector calculation class Point(object): “””Point are two-dimension””” def __init__(self, x, y): self.x = x
self.y = y class Vector(object): “””start and end are two points””” def __init__(self, start, end): self.x = end.x – start.x
self.y = end.y – start.y def pointDistance(p1, p2): “””calculate the distance between point p1 and p2″”” # v: a Vector object v = Vector(p1, p2) # translate v to a ndarray object t = np.array([v.x, v.y]) # calculate the inner product of ndarray t return float(np.sqrt(t @ t))

点与线之间的位置关系

1. 线的分类

class Segment(object): “””the 2 points p1 and p2 are unordered””” def __init__(self, p1, p2): self.p1 = p1
self.p2 = p2 class Line(object): “””p1 and p2 are 2 points in straight line””” def __init__(self, p1, p2): self.p1 = p1
self.p2 = p2

2. 点与线的位置关系

(1) 计算点到直线的距离

cosCAB=ACAB|AC||AB|

|AC|cosCAB

import numpy as np # numpy help us do some vector calculation class Point(object): “””Point are two-dimension””” def __init__(self, x, y): self.x = x
self.y = y class Segment(object): “””the 2 points p1 and p2 are unordered””” def __init__(self, p1, p2): self.p1 = p1
self.p2 = p2 class Line(object): “””p1 and p2 are 2 points in straight line””” def __init__(self, p1, p2): self.p1 = p1
self.p2 = p2 class Vector(object): “””start and end are two points””” def __init__(self, start, end): self.x = end.x – start.x
self.y = end.y – start.y def pointDistance(p1, p2): “””calculate the distance between point p1 and p2″”” # v: a Vector object v = Vector(p1, p2) # translate v to a ndarray object t = np.array([v.x, v.y]) # calculate the inner product of ndarray t return float(np.sqrt(t @ t)) def pointToLine(C, AB): “””calculate the shortest distance between point C and straight line AB, return: a float value””” # two Vector object vector_AB = Vector(AB.p1, AB.p2)
vector_AC = Vector(AB.p1, C) # two ndarray object tAB = np.array([vector_AB.x, vector_AB.y])
tAC = np.array([vector_AC.x, vector_AC.y]) # vector AD, type: ndarray tAD = ((tAB @ tAC) / (tAB @ tAB)) * tAB # get point D Dx, Dy = tAD[0] + AB.p1.x, tAD[1] + AB.p1.y
D = Point(Dx, Dy) return pointDistance(D, C)

(2) 判断点是不是在直线上

def pointInLine(C, AB): “””determine whether a point is in a straight line””” return pointToLine(C, AB) <

1. 本站所有资源来源于用户上传和网络，如有侵权请邮件联系站长！
2. 分享目的仅供大家学习和交流，您必须在下载后24小时内删除！
3. 不得使用于非法商业用途，不得违反国家法律。否则后果自负！
4. 本站提供的源码、模板、插件等等其他资源，都不包含技术服务请大家谅解！
5. 如有链接无法下载、失效或广告，请联系管理员处理！
6. 本站资源售价只是赞助，收取费用仅维持本站的日常运营所需！
7. 如遇到加密压缩包，请使用WINRAR解压,如遇到无法解压的请联系管理员！