bool IsInRect(int x, int y, Rectangle rect)?

Last touched on Saturday, Oct 07, 2006

Something which I could not find in the XNA framework, but which whilst working with 2D textures for in example menu’s is something you would definately want to see to make your life easier. To prevent myself from having to write the same stuff over and over, checking if a mouseclick was made within the [...]

Something which I could not find in the XNA framework, but which whilst working with 2D textures for in example menu’s is something you would definately want to see to make your life easier. To prevent myself from having to write the same stuff over and over, checking if a mouseclick was made within the X and Y of the texture rectangle of the button or field I have written the following function:

bool IsInRect(int x, int y, Rectangle rect)
{
if ((x >= rect.Left) &
(x <= rect.Right) &
(y >= rect.Top) &
(y <= rect.Bottom))
return true;

return false;
}

The function is quite easy to understand, you simply pass on a value for x, in example from msMouseState and do the same for y. Last but not least you pass on the rectangle you want to compare it with, now if the x and y values are located within the rectangle true is returned, otherwise it will return a false.

A simple sollution, but very effective :).

Have your say

Like many others I'm a slave to modern society dominated by mass media, I can be hooked on the latest series for months without missing a single episode or simply having to see a certain movie.

And if you thought music wouldn't be included there, you're dead wrong! Practically non stop I listen to music from various artists, I just can't stand the silence anymore.

Recently watched movies

  • Indiana Jones and the Last Crusade
  • Tomb Raider
  • Shanghai Knights
  • Constantine
  • The Hitchhiker's Guide to the Galaxy

Series I watch

  • Heroes
  • Jericho

Last scrobbled tracks

Why not drop by the entertainment section!

Entertainment

Picture of the Week