How do I check if a point is inside a polygone?

0 favourites
  • 11 posts
From the Asset Store
Connect the dots in the correct order and draw happy animals!
  • I have a large number of points (x,y) forming a polygone, is there any simple way in construct 3,to check if a point is inside my polygone?

    polygone is a texte based coordinate x1,y1;x2,y2;......xn,yn;

    Thanks.

    Tagged:

  • There's probably a math method to do this, but I don't know it. And there are a couple of "dumb" methods, which I would probably use :)

    1. Create a collision polygon in a sprite with this addon. Then check if this sprite is overlapping a point.

    2. Draw the polygon on a drawing canvas, fill it with color. Then save a snapshot and read pixel color at point coordinates.

  • For the math method you can do it by looking at a line from the point going down. If it crosses an odd amount of lines it’s inside the polygon, otherwise it’s inside.

    Here is what the events would look like if you just had sprites making up the points. You could use an array or text list but I’ll leave that as something that would need to be adapted.

    Global number x=100

    Global number y=100

    Global number inside=0

    Global number i=0

    Global number j=0

    Repeat p.count times

    — set i to loopindex

    — set j to (i+1)%p.count

    — value (x-p(i).x)/(p(j).x-p(i).x) is between 0 and 1

    — y > (p(j).y-p(i).y)/(p(j).x-p(i).x)*(x-p(i).x)+p(i).y

    — — set inside to 1-inside

  • There's probably a math method to do this, but I don't know it. And there are a couple of "dumb" methods, which I would probably use :)

    1. Create a collision polygon in a sprite with this addon. Then check if this sprite is overlapping a point.

    2. Draw the polygon on a drawing canvas, fill it with color. Then save a snapshot and read pixel color at point coordinates.

    1) collision polygon in a sprite is not allowed when dealing with a large number of vertices.

    2) Also drawing canvas can't support drawing polygon with big number of vertices (I naaed solution for dealing with polygon with more than 4000 vertices)

    Thanks anyway, first time heard about an addon allowing sprite collision manipualtion

  • For the math method you can do it by looking at a line from the point going down. If it crosses an odd amount of lines it’s inside the polygon, otherwise it’s inside.

    Here is what the events would look like if you just had sprites making up the points. You could use an array or text list but I’ll leave that as something that would need to be adapted.

    Global number x=100

    Global number y=100

    Global number inside=0

    Global number i=0

    Global number j=0

    Repeat p.count times

    — set i to loopindex

    — set j to (i+1)%p.count

    — value (x-p(i).x)/(p(j).x-p(i).x) is between 0 and 1

    — y > (p(j).y-p(i).y)/(p(j).x-p(i).x)*(x-p(i).x)+p(i).y

    — — set inside to 1-inside

    will appreciate a lot if you can translate your math method in an example on construct.

  • "4000 vertices"

    That's not a polygon, that's a cactus.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Here's some examples:

    c2:

    dropbox.com/s/rpi4nzhd763b4l6/pointInPoly.capx

    dropbox.com/s/65neg3btcb6mdzr/pointInPoly2.capx

    c3:

    dropbox.com/s/ga41909z93g71nq/pointInPoly2.c3p

    I ended up converting the text point list to an array, but otherwise the math is the same.

  • 1) collision polygon in a sprite is not allowed when dealing with a large number of vertices.

    I tried adding about 300 points to the collision polygon with that addon and it was working fine. Of course, I wouldn't do it on every tick.

    4000 vertices is a bit too much. I'm curious why do you need that many?

  • > 1) collision polygon in a sprite is not allowed when dealing with a large number of vertices.

    I tried adding about 300 points to the collision polygon with that addon and it was working fine. Of course, I wouldn't do it on every tick.

    4000 vertices is a bit too much. I'm curious why do you need that many?

    I need for parcel managing in a map.

  • Here's some examples:

    c2:

    https://www.dropbox.com/s/rpi4nzhd763b4l6/pointInPoly.capx?dl=1

    https://www.dropbox.com/s/65neg3btcb6mdzr/pointInPoly2.capx?dl=1

    c3:

    https://www.dropbox.com/s/ga41909z93g71nq/pointInPoly2.c3p?dl=1

    I ended up converting the text point list to an array, but otherwise the math is the same.

    Thanks, this is what I'm looking for.

    My example without array:

    we.tl/t-2oO3RG9BoP

  • > 1) collision polygon in a sprite is not allowed when dealing with a large number of vertices.

    I tried adding about 300 points to the collision polygon with that addon and it was working fine. Of course, I wouldn't do it on every tick.

    4000 vertices is a bit too much. I'm curious why do you need that many?

    I need it for drawing a parcel on a map. 4000 is the average number of vertices.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)