gpt4 book ai didi

python - 循环获取二维三角形中的点

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:05 25 4
gpt4 key购买 nike

我在 Python 中有一个函数来查找一个点是否在二维三角形中,如下所示:

def isInsideTriangle(P,p1,p2,p3): #is P inside triangle made by p1,p2,p3?
x,x1,x2,x3 = P[0],p1[0],p2[0],p3[0]
y,y1,y2,y3 = P[1],p1[1],p2[1],p3[1]
full = abs (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))
first = abs (x1 * (y2 - y) + x2 * (y - y1) + x * (y1 - y2))
second = abs (x1 * (y - y3) + x * (y3 - y1) + x3 * (y1 - y))
third = abs (x * (y2 - y3) + x2 * (y3 - y) + x3 * (y - y2))
return abs(first + second + third - full) < .0000000001

我的“P”是一堆(大约 15 个)点,来自这个(部分)文件:

PointXY
[387.9385, 200.0]
PointXY
[200.0, 387.9385]
PointXY
[200.0, 387.9385]
PointXY
[200.0, 353.2089]
PointXY

我的p1、p2和p3是三角形中的角坐标。它们来自这个(部分)文件:

TriangleXY
[193.0371, 0.1218346]
[193.0244, 0.1218346]
[186.0572, 0.4871899]
TriangleXY
[206.9799, 0.1218346]
[206.9756, 0.1218346]
[213.9428, 0.4871899]
TriangleXY
[193.0244, 0.1218346]
[193.0371, 0.1218346]
[200.0, 0.0]
TriangleXY
[206.9756, 0.1218346]

我希望我的脚本执行的操作是,如果任何点位于任何三角形(或侧面)中,则返回该三角形 p1、p2 和 p3 的 (3) 个特定坐标,以及其中的具体点 P。它现在仅适用于单个 P 和一组 p1、p2 和 p3,我希望它适用于所有点 P 和所有三角形角 p1、p2 和 p3。有人知道如何在我的脚本中相应地调整它吗?我的 P 和 p1、p2 和 p3 由这些脚本调用:

# triangle coordinates p1 p2 p3
g = open("spheretop1.stl", "r")
m = open("TriangleXYcoordinates.gcode", "w")
searchlines = g.readlines()
file = ""

for i, line in enumerate(searchlines):
if "outer loop" in line:
p1 = map(float, searchlines[i+1].split()[1:3])
p2 = map(float, searchlines[i+2].split()[1:3])
p3 = map(float, searchlines[i+3].split()[1:3])
m.write("TriangleXY" + "\n" + str(p1) + "\n" + str(p2) + "\n" + str(p3) + "\n")

# Point coordinates P
import json
h = open("PointXYcoordinates.gcode", "r")
searchlines = h.readlines()
file = ""

for i, line in enumerate(searchlines):
if "PointXY" in line:
P = json.loads(searchlines[i+1].strip())

最佳答案

也许你想要这样的东西:

def pointsInsideTriangles(points,triangles):
for (P in points):
for (t in triangles):
if isInsideTriangle(P,t[0],t[1],t[2]):
print("Point " + str(P) + " is inside traingle " + str(t))

总共你会有这样的东西:

def isInsideTriangle(P,p1,p2,p3): #is P inside triangle made by p1,p2,p3?
x,x1,x2,x3 = P[0],p1[0],p2[0],p3[0]
y,y1,y2,y3 = P[1],p1[1],p2[1],p3[1]
full = abs (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))
first = abs (x1 * (y2 - y) + x2 * (y - y1) + x * (y1 - y2))
second = abs (x1 * (y - y3) + x * (y3 - y1) + x3 * (y1 - y))
third = abs (x * (y2 - y3) + x2 * (y3 - y) + x3 * (y - y2))
return abs(first + second + third - full) < .0000000001

def pointsInsideTriangles(points,triangles):
for (P in points):
for (t in triangles):
if isInsideTriangle(P,t[0],t[1],t[2]):
print("Point " + str(P) + " is inside triangle " + str(t))


# triangle coordinates p1 p2 p3
points = [];
triangles = [];
g = open("spheretop1.stl", "r")
m = open("TriangleXYcoordinates.gcode", "w")
searchlines = g.readlines()
file = ""

for i, line in enumerate(searchlines):
if "outer loop" in line:
p1 = map(float, searchlines[i+1].split()[1:3])
p2 = map(float, searchlines[i+2].split()[1:3])
p3 = map(float, searchlines[i+3].split()[1:3])
m.write("TriangleXY" + "\n" + str(p1) + "\n" + str(p2) + "\n" + str(p3) + "\n")
triangles.append([p1,p2,p3])


# Point coordinates P
import json
h = open("PointXYcoordinates.gcode", "r")
searchlines = h.readlines()
file = ""

for i, line in enumerate(searchlines):
if "PointXY" in line:
P = json.loads(searchlines[i+1].strip())
points.append(P)

pointsInsideTriangles(points,triangles)

关于python - 循环获取二维三角形中的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36387928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com