2016-04-03 90 views
1

我有Python中的函數找到,如果一個點是在2D的三角形,看起來像這樣:循環爲2D點三角形

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] 

我想我的腳本做的,就是如果有任何點(S)是在(或一側)任何三角形(S),返回我的(3)來自該三角形p1,p2和p3的特定座標以及其中的特定點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()) 
+0

@ idjaw在這個問題中的信息是否足夠清晰? – Henry

回答

1

也許你想是這樣的:

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) 
+0

當我嘗試運行代碼時出現錯誤,「for(P in Points)」:無效語法。 (def points pointsInsideTriangles(points,triangles))。] – Henry

+0

對'points'變量使用相同的大小寫。將行更改爲'for(P in points):' – Pedro

+0

謝謝,剩下的一件事,你知道如何刪除我的函數pointsInsideTriangles(點,三角形)給我的所有副本嗎?基本上在最後的打印給了我一個值的完整列表,但有些是重複的。 – Henry