Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


#ActualEndurion

Posted 03 February 2013 - 08:15 AM

This is a snippet I found on the internet a while ago, seemed to work fine for everything I threw at it.

Don't ask me about the math behind it ;)

xp and yp are point to an array of corners, x,y is the point you want to check:

template < typename T >
  bool is_point_in_poly( int num_edges, T *xp, T *yp, T x, T y)
  {
    int i, j;
    bool c = 0;
    for (i = 0, j = num_edges-1; i < num_edges; j = i++) {
      if ((((yp[i]<=y) && (y<yp[j])) ||
           ((yp[j]<=y) && (y<yp[i]))) &&
          (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))

 
        c = !c;
    }
    return c;
  }

#3Endurion

Posted 03 February 2013 - 08:15 AM

This is a snippet I found on the internet a while ago, seemed to work fine for everything I threw at it.

Don't ask me about the math behind it ;)

xp and yp are point to an array of corners, x,y is the point you want to check:
template < typename T >  bool is_point_in_poly( int num_edges, T *xp, T *yp, T x, T y)  {    int i, j;    bool c = 0;    for (i = 0, j = num_edges-1; i < num_edges; j = i++) {	  if ((((yp[i]<=y) && (y<yp[j])) ||		   ((yp[j]<=y) && (y<yp[i]))) &&		  (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i])) 	    c = !c;    }    return c;  }

#2Endurion

Posted 03 February 2013 - 08:14 AM

This is a snippet I found on the internet a while ago, seemed to work fine for everything I threw at it.

Don't ask me about the math behind it ;)

xp and yp are point to an array of corners, x,y is the point you want to check:
template < typename T >  bool is_point_in_poly( int num_edges, T *xp, T *yp, T x, T y)  {    int i, j;    bool c = 0;    for (i = 0, j = num_edges-1; i < num_edges; j = i++) {	  if ((((yp[i]<=y) && (y<yp[j])) ||		   ((yp[j]<=y) && (y<yp[i]))) &&		  (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i])) 	    c = !c;    }    return c;  }

#1Endurion

Posted 03 February 2013 - 08:13 AM

This is a snippet I found on the internet a while ago, seemed to work fine for everything I threw at it:

 

xp and yp are point to an array of corners, x,y is the point you want to check:

 

template < typename T >
  bool is_point_in_poly( int num_edges, T *xp, T *yp, T x, T y)
  {
    int i, j;
    bool c = 0;
    for (i = 0, j = num_edges-1; i < num_edges; j = i++) {
      if ((((yp[i]<=y) && (y<yp[j])) ||
           ((yp[j]<=y) && (y<yp[i]))) &&
          (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))

 
        c = !c;
    }
    return c;
  }

PARTNERS