If we could transform the point and the rectangle such that the rectangle become axis-aligned, we could solve the problem in the direct way mentioned above. In other words, we are transforming the rectangle’s coordinate space to the x-y coordinate space.
First, the rectangle’s center is translated to the origin, then the rectangle is rotated such that it is axis-aligned with the major axes. The rotation equation can be written as x’ = ux*x + uy*y and y’ = vx*x + vy*y, where (x,y) is the original point, (x’,y’) is the rotated point, (ux,uy) and (vx,vy) are both the normalized orthogonal vector of the axes of the rectangle (see figure above). To understand why this is so, you need to understand the change of basis in linear algebra.
Translating and rotating points involve lots of multiplications. Can we do better than this?
The answer is yes, we can utilize dot product. The trick is to use dot product to find the projected line from point P onto the rectangle sides, and if its length is shorter than the sides, then the point must be inside the rectangle.
http://www.ihas1337code.com/2010/04/how-to-determine-if-point-is-inside.html
First, the rectangle’s center is translated to the origin, then the rectangle is rotated such that it is axis-aligned with the major axes. The rotation equation can be written as x’ = ux*x + uy*y and y’ = vx*x + vy*y, where (x,y) is the original point, (x’,y’) is the rotated point, (ux,uy) and (vx,vy) are both the normalized orthogonal vector of the axes of the rectangle (see figure above). To understand why this is so, you need to understand the change of basis in linear algebra.
Translating and rotating points involve lots of multiplications. Can we do better than this?
The answer is yes, we can utilize dot product. The trick is to use dot product to find the projected line from point P onto the rectangle sides, and if its length is shorter than the sides, then the point must be inside the rectangle.
http://www.ihas1337code.com/2010/04/how-to-determine-if-point-is-inside.html
No comments:
Post a Comment