检查点是否在旋转的矩形内 [英] Checking if a point is inside a rotated rectangle
问题描述
我知道这个问题以前曾被问过几次,而且我已阅读过有关这方面的各种帖子。不过,我正努力让这个工作。
bool isClicked()
{
Vector2 origLoc = Location;
Matrix rotationMatrix = Matrix.CreateRotationZ(-Rotation);
Location = new Vector2(0 - (Texture.Width / 2),0 - (Texture.Height / 2));
Vector2 rotatedPoint = new Vector2(Game1.mouseState.X,Game1.mouseState.Y);
rotatePoint = Vector2.Transform(rotatedPoint,rotationMatrix);
if(Game1.mouseState.LeftButton == ButtonState.Pressed&&
rotatedPoint.X> Location.X&&
rotatePoint.X< Location .X + Texture.Width&&
rotatePoint.Y> Location.Y&&
rotatePoint.Y< Location.Y + Texture.Height)
{
Location = origLoc;
返回true;
}
Location = origLoc;
返回false;
(x1,y1), B(x2,y2) (x4,y4)
,
-
计算
△APD
,△DPC
,△CPB
,△PBA
。 -
如果这个总和大于矩形的面积:
- 然后点
P(x,y)
在 外 。 em>矩形。
- 然后点
每个三角形的面积可以仅使用使用此公式进行坐标:
假设三点是: A(x,y)
, B(x,y)
, C(x,y)
。
<$ (Bx * Ay-Ax * By)+(Cx * Bx-Bx * Cx)+(Ax * Cy-Cx * Ay))/ 2
I know this question has been asked a few times before, and I have read various posts about this. However I am struggling to get this to work.
bool isClicked()
{
Vector2 origLoc = Location;
Matrix rotationMatrix = Matrix.CreateRotationZ(-Rotation);
Location = new Vector2(0 -(Texture.Width/2), 0 - (Texture.Height/2));
Vector2 rotatedPoint = new Vector2(Game1.mouseState.X, Game1.mouseState.Y);
rotatedPoint = Vector2.Transform(rotatedPoint, rotationMatrix);
if (Game1.mouseState.LeftButton == ButtonState.Pressed &&
rotatedPoint.X > Location.X &&
rotatedPoint.X < Location.X + Texture.Width &&
rotatedPoint.Y > Location.Y &&
rotatedPoint.Y < Location.Y + Texture.Height)
{
Location = origLoc;
return true;
}
Location = origLoc;
return false;
}
Let point P(x,y)
, and rectangle A(x1,y1)
, B(x2,y2)
, C(x3,y3)
, D(x4,y4)
.
Calculate the sum of areas of
△APD
,△DPC
,△CPB
,△PBA
.If this sum is greater than the area of the rectangle:
- Then point
P(x,y)
is outside the rectangle. - Else it is in or on the rectangle.
- Then point
The area of each triangle can be calculated using only coordinates with this formula:
Assuming the three points are: A(x,y)
, B(x,y)
, C(x,y)
.
Area = abs( (Bx * Ay - Ax * By) + (Cx * Bx - Bx * Cx) + (Ax * Cy - Cx * Ay) ) / 2
这篇关于检查点是否在旋转的矩形内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!