Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Mouse in region


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 rpiller   Members   -  Reputation: 706

Like
0Likes
Like

Posted 03 February 2013 - 07:46 AM

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.



Sponsor:

#2 Endurion   Crossbones+   -  Reputation: 3694

Like
0Likes
Like

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.

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, 03 February 2013 - 08:15 AM.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#3 MrDaaark   Members   -  Reputation: 3555

Like
0Likes
Like

Posted 03 February 2013 - 08:22 AM

There is tons of example code for stuff like this in all kinds of different situations on the real time rendering book site.

 

http://www.realtimerendering.com/intersections.html


Edited by Daaark, 03 February 2013 - 08:22 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS