[Python] Two-dimensional system point figure inside / outside problem

Click here for the idea

environment

python3.7

problem

Calculate whether the __ point P </ font> __ in the figure below is inside or outside the figure. triangle.jpg

__O (0,0) __ represents the __ origin __. Also, __A (2,2) __, __B (3,1) __, __C (3,3) __ represent the vertices __ of the figure. __P (x, y) __ is __ any point __.

Calculation

Calculate so that the directions of the __cross products are the same __ as shown in the figure below. triangle3.jpg

Definition of coordinates ↓

python


import numpy 

#Coordinates of point A
A = numpy.array((6, 5))
#Coordinates of point B
B = numpy.array((3, 1))
#Coordinates of point C
C = numpy.array((9, 1))

Cross product calculation ↓

python


#Whether the point P is in the shape
def p(x, y):
    #Coordinates of point P
    P = numpy.array((x, y))

    #Cross product of vector BP and vector BA
    abp = numpy.outer(P-B, A-B)
    #Cross product of vector BC and vector BP
    pbc = numpy.outer(C-B, P-B)
    #Cross product of vector CA and vector CP
    apc = numpy.outer(A-C, P-C)

    #Determinant of the outer product of vector BP and vector BA
    abp = numpy.linalg.det(abp)
    #Determinant of the outer product of vector BC and vector BP
    pbc = numpy.linalg.det(pbc)
    #Determinant of the outer product of vector CA and vector CP
    apc = numpy.linalg.det(apc)
    
    #Returns True if the determinant of the outer product of vector BP and vector BA matches the sign of another determinant
    if numpy.sign(abp)==numpy.sign(pbc) and numpy.sign(abp)==numpy.sign(pbc):
        return True
    #Returns False if the determinant of the outer product of vector BP and vector BA does not match the sign of the other determinant.
    else:
        return False 

All code and results

code

code.py


import numpy 

#Coordinates of point A
A = numpy.array((6, 5))
#Coordinates of point B
B = numpy.array((3, 1))
#Coordinates of point C
C = numpy.array((9, 1))


#Whether the point P is in the shape
def p(x, y):
    #Coordinates of point P
    P = numpy.array((x, y))

    #Cross product of vector BP and vector BA
    abp = numpy.outer(P-B, A-B)
    #Cross product of vector BC and vector BP
    pbc = numpy.outer(C-B, P-B)
    #Cross product of vector CA and vector CP
    apc = numpy.outer(A-C, P-C)

    #Determinant of the outer product of vector BP and vector BA
    abp = numpy.linalg.det(abp)
    #Determinant of the outer product of vector BC and vector BP
    pbc = numpy.linalg.det(pbc)
    #Determinant of the outer product of vector CA and vector CP
    apc = numpy.linalg.det(apc)
    
    #Returns True if the determinant of the outer product of vector BP and vector BA matches the sign of another determinant
    if numpy.sign(abp)==numpy.sign(pbc) and numpy.sign(abp)==numpy.sign(pbc):
        return True
    #Returns False if the determinant of the outer product of vector BP and vector BA does not match the sign of the other determinant.
    else:
        return False 


print(p(3, 1))#Output result:True
print(p(2, 2))#Output result:True
print(p(8,14))#Output result:False

__Supplement __ When the point P is on the line __, the point P is calculated as __ in the __ figure.

Recommended Posts

[Python] Two-dimensional system point figure inside / outside problem
[Python] Determine if any coordinate point is inside or outside the polygon
Inside / outside judgment with Python ➁: About donut-shaped polygons