RSS Feed

Checking That A Point Or Coordinates Are In A Rectangle

In the example code below I create a rectangle. then I check if two sets of coordinates are within the bounds of this rectangle.

protected Rect bounds = new Rect(0,0,100,100);

if inRectangle(10.0f,10.0,f)   // This will return true
{
              ….. do something
}

if inRectangle(200.0f,10.0,f)   // This will return false
{
           … wont get here
}

 public boolean inRectangle(float x, float y)

{
     boolean result = false;

     if (bounds.contains((int)x,(int)y))            // Note conversion to integer as this is required by contains.
    { 
           result = true;
     }

    return result;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: