There are a number of ways to do this. Most of them involve some basic linear algebra.
Since the shape is convex, you have more options available to you. The way I'd do it is to form lines connecting the four corners of that rectangle. Make sure the lines are all oriented so they go clockwise:
Lower Left - Upper Left
Upper Left - Upper Right
Upper Right - Lower Right
Lower Right - Lower Left
You can then use the perpendicular dot product to determine whether or not a point is to the "right" or to the "left" of a given line.
Since all lines are oriented clockwise, in order for the point to be inside the shape... it must be to the "right" of all 4 lines. Therefore you just check the point against all 4 lines and if it is to the left of any of them, you know it is outside the area.
Perpendicular dot product is pretty simple. Assuming you have a 'Vector' or 'Point' class (that has x,y components):
inline double perpDot(const Vector& a, const Vector& b) { return (a.y*b.x) - (a.x*b.y); }
perpDot gives you
sin(theta) * length(a) * length(b)
where 'theta' is the angle between vectors a,b.
This will be 0 if 'a' and 'b' are parallel. It will be > 0 if 'b' is clockwise of 'a' (assuming +Y is up), or < 0 if 'b' is ccw of 'a'.
Since +Y is down in your case, this will be reversed (>0 means 'b' is ccw. <0 is cw)
To apply:
1 2 3 4 5 6 7
|
bool isPointCwOfLine(const Vector& line1, const Vector& line2, const Vector& pt)
{
Vector a = line2 - line1;
Vector b = pt - line1;
return (perpDot(a,b) <= 0);
}
|
EDIT:
Another option (if you don't have a convex shape) is to form a ray from the target point, and count how many sides of the shape that ray intersects. Even number = point is outside the shape, odd number = point is inside the shape.
The math to do this efficiently is a little more involved so I won't get into it here. The above solution is easier.