2021年1月6日星期三

How to determine if a point is on a 2D triangle?

I'm making a code whether if a point is inside a triangle or not. How do I make an elif statement to determine if a point is on a 2D triangle using 3 points? So if the point is on the triangle it says not inside and if it's not on the lines it says inside.

This is the code I have so far

y1 = float(input("Input coordinate y1:"))  x2 = float(input("Input coordinate x2:"))  y2 = float(input("Input coordinate y2:"))  x3 = float(input("Input coordinate x3:"))  y3 = float(input("Input coordinate y3:"))  xm = float(input("Input coordinate xm:"))  ym = float(input("Input coordinate ym:"))     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, xm, ym):       # Calculate area of triangle ABC       A = area(x1, y1, x2, y2, x3, y3)           # Calculate area of triangle PBC        A1 = area(xm, ym, x2, y2, x3, y3)               # Calculate area of triangle PAC        A2 = area(x1, y1, xm, ym, x3, y3)               # Calculate area of triangle PAB        A3 = area(x1, y1, x2, y2, xm, ym)               # Check if sum of A1, A2 and A3        # is same as A       if (A == A1 + A2 + A3):           return True      else:           return False    if isInside(x1, y1, x2, y2, x3, y3, xm, ym):      print('Inside')   #elif:  else:      print('Not Inside')    
https://stackoverflow.com/questions/65585715/how-to-determine-if-a-point-is-on-a-2d-triangle January 06, 2021 at 03:58AM

没有评论:

发表评论