In the code below I have calculated which triangles cover a certain point. Now I am trying to let the function perimeter
calculate the perimeter of the left over triangles which can be found in triangle_fit
. And when I know the perimeters of these triangles I want to chose the smallest triangle. I did some attempts but the function will only recognize x1
, y1
, x2
, ...
.
import itertools
import math
def area(x1, y1, x2, y2, x3, y3):
return abs((x1 * (y2 - y3) + x2 * (y3 - y1)
+ x3 * (y1 - y2)) / 2.0)
def isInside(x1, y1, x2, y2, x3, y3, x, y):
A = area (x1, y1, x2, y2, x3, y3)
A1 = area (x, y, x2, y2, x3, y3)
A2 = area (x1, y1, x, y, x3, y3)
A3 = area (x1, y1, x2, y2, x, y)
if(A == A1 + A2 + A3):
return True
else:
return False
def perimeter (x1, y1, x2, y2, x3, y3):
return abs(math.sqrt((x1-x2)**2+(y1-y2)**2)+math.sqrt((x2-x3)^2
+(y2-y3)**2)+math.sqrt((x3-x1)**2+(y3-y1)**2))
points = [(0,10), (0,0), (10,0), (10,10), (5,2), (2,2)]
P = (5,1)
triangle_fit = []
for triangle in itertools.combinations(points, 3):
p1, p2, p3 = triangle
if isInside(*p1, *p2, *p3, *P):
triangle_fit.append(triangle)
print(triangle_fit)