c# - 检查一个点是否在旋转的矩形内

标签 c# math graphics xna geometry

我知道这个问题之前已经被问过几次了,我也阅读了很多关于这个的帖子。但是我正在努力让它发挥作用。

    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;
    }

最佳答案

设点P(x,y),矩形A(x1,y1)B(x2,y2)C(x3,y3), D(x4,y4)

  • 计算△APD, △DPC, △CPB, △PBA的面积和>.

  • 如果这个总和大于矩形的面积:

    • 然后点P(x,y)在矩形外面
    • 否则它在矩形内或上

每个三角形的面积可以使用以下公式仅使用坐标来计算:

假设三点是:A(x,y), B(x,y), C(x,y) ...

Area = abs( (Bx * Ay - Ax * By) + (Cx * By - Bx * Cy) + (Ax * Cy - Cx * Ay) ) / 2

关于c# - 检查一个点是否在旋转的矩形内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17136084/

相关文章:

Android:如何覆盖位图并在位图上绘制?

c# - 如何更改 TemplateField 的颜色

Python:如何编写指数移动平均线?

algorithm - 非常长的整数的乘法

php - 如何计算一年是否有53周

c - 在普通 C 中测量字符串大小

graphics - 画一个不完美的圆

c# - 从枚举中获取整数值

c# - 如何在没有定义 T 类型的情况下存储类型为 'Class<T>' 的类的集合

javascript - 自动刷新页面,数据不更新