How can I define lines so when I click on the screen, my program knows whether I clicked on A B or C?

  • magic_lobster_party@fedia.io
    link
    fedilink
    arrow-up
    7
    ·
    2 days ago

    Adding to this: if you can’t use a polygon library, it’s quite easy to check if a point is inside a polygon. Just count the number of polygon edges is above the point (have a separate count for each polygon).

    If the number is odd, then the point is inside the polygon. If it’s even, then it’s outside.

    If there are multiple candidate polygons (which will happen with A in the example), pick the one with the closest edge to the point. Alternatively, pick the smallest polygon.

    See ray casting algorithm in this page: https://en.wikipedia.org/wiki/Point_in_polygon

    Just beware of floating point precision when implementing the algoritm.

    But use a polygon library first and foremost.