What is the fastest way to find if a point is in a rectangle given in this form:
I have two points which are the centers of opposite sides of the rectangle, and a number which is the height of those sides. I hope this is clear.
The rectangle is (probably) not aligned with the axis. I am wondering if there may be a faster algorithm given this data then calculating the four corners, rotating, etc.
An idea I though of but am not sure how to implement (having trouble with the math) was to find the distance from the point to the line traced between the two centers, and if it is less then half the length of the side of the rectangle and also on the line then it is in the rectangle. I don't know how to explain this better.
Maybe the picture will help explain:
A, B, C are given, as well as the length of side A/B. Basically I thought that if CD is less then half of side A and D is on AB, the point is in the rectangle. But how do I do this?
Another thought: Instead of finding D to see if it is on AB, check if angles ABC and BAC are acute, but I still don't know how to do this.
Not entirely sure it's the fastest, but this would likely work:
Rather than placing D half in between the two centers, project C as D along the AB axis. Then check that a) D is between A and B and b) CD is less or equal to half of the rectangle's height.
Re your idea of using the angles... using Pythagorus and or Al Kashi's theorem might actually make sense:
http://en.wikipedia.org/wiki/Law_of_cosines
ABC acute and BAC acute would both be a prerequisite, and given them you place C2 on the rectangle, with the same alpha/beta (see wiki page). From there you also know gamma (pi/2, since on the rectangle) and beta/alpha (pi/2 - beta), which leads to wondering whether the [A,C]
or [B,C]
distance is lesser or equal to that of [A,C2]
or [B,C2]
respectively.