Search code examples
geometrycoordinatesarea

Check if coordinate in selected area


I have 4 coordinates of area: x1,y1 ... etc. And have one more position x0,y0.

How to check if my coordinate in selected area?

Area and point coordinates


Solution

  • I will explain you how to check that (x0,y0) lies "below" the line through (x1,y1) and (x2,y2). Essentially, you want that the vector (x0-x1,y0-y1) points "to the right" of (x2-x1, y2-y1). This is equivalent to saying that the matrix

    x0-x1      y0-y1
    
    x2-x1      y2-y1
    

    has a negative determinant. So your condition becomes

    (x0-x1)(y2-y1) < (y0-y1)(x2-x1).
    

    You get such a condition for any line bounding the area.