Jump to content
  • Advertisement
Sign in to follow this  
rpiller

Mouse in region

This topic is 2144 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anyone know where I can find any code that checks if the mouse is in a region that can be defined as any kind of region? Checking for a rectangle is easy, but checking for any polygon region I'm not sure how to go about doing that.

Share this post


Link to post
Share on other sites
Advertisement

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;
  }
Edited by Endurion

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!