python - determine whether a coordinate point is inside a triangle

Mainly used knowledge points :

   

It is best to take the three points A, B, and C from the upper right corner in counterclockwise order, because the results obtained in this way are generally positive values. If you do not follow this rule, you may get negative values, but as long as the absolute value is taken, Yes, it will not affect the size of the triangle area.

  • The vector product can be defined as:
    Modular length: (here θ represents the angle between two vectors (under the premise of a common starting point) (0° ≤ θ ≤ 180°), which lies on the plane defined by these two vectors.)
    Direction: The direction of the vector product of the a vector and the b vector is perpendicular to the plane where the two vectors are located, and obeys the right-hand rule. (A simple way to determine the direction of the resulting vector that satisfies the "right-hand rule" is as follows: if the coordinate system satisfies the right-hand rule, when the four fingers of the right hand turn from a to b at an angle of no more than 180 degrees , The thumbs up points in the direction of c .)
    It can also be defined like this (equivalently):
    Vector product | c| =| a × b| =| a | | b |sin< a,b >
    That is , the length of c is numerically equal to the area of ​​a parallelogram formed by a , b, and the included angle θ.

  • Using the area method, as shown in the figure above, if the point P is inside the triangle ABC, the sum of the areas of the three small triangles PAB, PBC, PAC = the area of ​​ABC, and vice versa.

  • According to the requirements of the title, there are four coordinate points which are P(x,y), A(x1,y1), B(x2,y2), C(x3,y3). According to the area formula of the triangle, it can be judged whether a triangle is formed ,which is:
    def IsTrangleOrArea(x1,y1,x2,y2,x3,y3):
        return abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0)


         Next, calculate the areas of PAB, PAC, PBC and ABC in turn according to the area formula: ABC==PAB+PAC+PBC?. If it is true, it means that point P is inside the triangle, otherwise it is not
  • The detailed code is as follows, welcome to correct
    #-*- coding: utf8 -*-
    #First determine whether it is a triangle
    import math
    corA = raw_input("Please input the coordinate value of point A:").split(",")
    x1,y1 = int(corA[0]),int(corA[1])
    
    corB = raw_input("Please input the coordinate value of point B:").split(",")
    x2, y2 = int (corB [0]), int (corB [1])
    
    corC = raw_input("Please input the coordinate value of point C:").split(",")
    x3, y3 = int (corC [0]), int (corC [1])
    
    corP = raw_input("请输入P点的坐标值:").split(",")
    x,y = int(corP[0]),int(corP[1])
    
    def IsTrangleOrArea(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):
    
        #三角形ABC的面积
        ABC = IsTrangleOrArea(x1,y1,x2,y2,x3,y3)
    
        # 三角形PBC的面积
        PBC = IsTrangleOrArea(x,y,x2,y2,x3,y3)
    
        # 三角形ABC的面积
        PAC = IsTrangleOrArea(x1,y1,x,y,x3,y3)
    
        # 三角形ABC的面积
        PAB = IsTrangleOrArea(x1,y1,x2,y2,x,y)
    
        return (ABC == PBC + PAC + PAB)
    
    if __name__ =="__main__":
       #if IsInside(10, 30, 20, 0, 10, 30, 10, 15):
       if IsInside(x1,y1,x2,y2,x3,y3,x,y):
          print "Inside"
       else:
          print "Outside"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325745289&siteId=291194637