Is the point inside the rectangle?

I am trying to figure out if a specified point is inside of a rectangle. I am given the center of the rectangle (represented as (x,y)). And also the height and width of the rectangle. I tried a piece of code I found, but I think it's wrong, because the answer is not the way it should be. Here is an example of my code:

1
2
3
4
5
6
7
8
// Overloaded Constructor
Rectangle2D::Rectangle2D(double x, double y, double height, double width)
{
   setx(x);
   sety(y);
   setHeight(height);
   setWidth(width);
}       


1
2
3
4
5
6
7
8
9
10
11
bool Rectangle2D::contains(double x, double y) const
{
	double pointX = x;
	double pointY = y;
        // This is the piece of code I found
	if (pointX >= this->x && pointX <= this->x + this->width &&
pointY >= this->y && pointY <= this->y + this->height)
		return true;
	else
		return false;
}



I run the contains function, and it returns true if the specified point (x,y) is inside the rectangle. My rectangle is Rectangle2D r1(2, 2, 5.5, 4.9); and the instance function is r1.contains(3, 3). So with that information I have a rectangle with a center at (2,2), height=5.5, width=4.9, and a point at (3,3). Is it inside, upon, or outside?
I figured it out for anyone who still needs it.

1
2
3
4
5
6
7
8
9
10
11
bool Rectangle2D::contains(double x, double y) const
{
	double pointX = x;
	double pointY = y;
        // Just had to change around the math
	if (pointX < (this->x + (.5*this->width)) && pointX > (this->x - (.5*this->width)) &&
           pointY < (this->y + (.5*this->height)) && pointY > (this->y - (.5*this->height)))
		return true;
	else
		return false;
}
Topic archived. No new replies allowed.