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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值