栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 如何判断点是否在多边形(三角形)内,或求点在3D面上的投影?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 如何判断点是否在多边形(三角形)内,或求点在3D面上的投影?

  • 方法1: 用shapely中的geometry包

1)polygon.covers(point)

如果point在多边形polygon上(包括边),返回True,否则False。

2)polygon.contains(point)

如果point在多边形polygon上(不包括边),返回True,否则False。

from shapely import geometry
import matplotlib.pyplot as plt

pts = [(0, 0), (1, 1), (0, 1), (0, 0)]
polygon = geometry.Polygon(pts)

pt = 0.1, 0.2
print(*pt)
point = geometry.Point(*pt)

plt.figure(0)
plt.plot([i[0] for i in pts], [i[1] for i in pts])
plt.scatter(*pt)
plt.show()
print(polygon.covers(point))

方法2: 用blender的内置python api。
将点投影到三角形平面上,并检查其是否在三角形内,如果在,则返回在面上的投影。
没试效果。地址 https://docs.blender.org/api/current/mathutils.geometry.html

mathutils.geometry.intersect_point_tri(pt, tri_p1, tri_p2, tri_p3)
  • 取4个向量:一个是点,下3个定义三角形。将点投影到三角形平面上,并检查其是否在三角形内。

  • Takes 4 vectors: one is the point and the next 3 define the triangle. Projects the point onto the triangle plane and checks if it is within the triangle.

Parameters

  • pt (mathutils.Vector) – Point

  • tri_p1 (mathutils.Vector) – First point of the triangle

  • tri_p2 (mathutils.Vector) – Second point of the triangle

  • tri_p3 (mathutils.Vector) – Third point of the triangle

Returns

Point on the triangles plane or None if its outside the triangle

mathutils.Vector or None
from mathutils import Vector
from mathutils.geometry import (intersect_point_tri, distance_point_to_plane)
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d


pt1 = Vector((0, 0,1))
pt2 = Vector((1, 1, 0))
pt3 = Vector((1, -1, 0))

plane_co = pt1
plane_no = (pt3 - pt1).cross(pt2 - pt1).normalized()

pt = Vector((0.5, 0, 1))
# find distance to plane
d = distance_point_to_plane(pt, plane_co, plane_no)
ptm = pt - d * plane_no
print("move", pt, d, "units to", ptm)
p = intersect_point_tri(pt, pt1, pt2, pt3)
if p:
    print("point on triangle", p)
else:
    print("point outside triangle")

fig = plt.figure()
ax = plt.axes(projection="3d")
ax.set_xlabel("x axis")
ax.set_ylabel("y axis")
ax.set_zlabel("z axis")
x0 = [pt1[0], pt2[0], pt3[0], pt1[0]]
y0 = [pt1[1], pt2[1], pt3[1], pt1[1]]
z0 = [pt1[2], pt2[2], pt3[2], pt1[2]]
ax.plot3D(x0, y0, z0)
ax.scatter(x0, y0, z0, color='b')
for x, y, z in zip(x0, y0, z0):
    text = "({},{},{})".format(x, y, z)
    ax.text(x, y, z, text, zdir=(1, 1, 1))
ax.scatter(pt[0], pt[1], pt[2], color='r')
ax.scatter(ptm[0], ptm[1], ptm[2], color='g')
plt.show()

  • 方法3: 还是blender的内置python api
 mathutils.geometry.intersect_point_tri_2d(pt, tri_p1, tri_p2, tri_p3)
  • 取4个向量(仅使用x和y坐标):一个是点,接下来的3个定义三角形。如果点位于三角形内,则返回1,否则返回0。
  • Takes 4 vectors (using only the x and y coordinates): one is the point and the next 3 define the triangle. Returns 1 if the point is within the triangle, otherwise 0.

Parameters

  • pt (mathutils.Vector) – Point

  • tri_p1 (mathutils.Vector) – First point of the triangle

  • tri_p2 (mathutils.Vector) – Second point of the triangle

  • tri_p3 (mathutils.Vector) – Third point of the triangle

Return type

int

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/993702.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号