2

四角形の原点が異なる可能性がある場合、ポイントが回転した四角形の内側にあるかどうかを確認するにはどうすればよいですか? これは基本的に私が今使っているものです:

struct Point 
{
    float x;
    float y;
};

struct Rectangle
{
    float x;
    float y;
    float w;
    float h;
    float origin;
    float rotation; // In degrees
};

bool contains(const Rectangle& rect, const Point& point)
{
    float c = std::cos(toRadians(-rect.rotation));
    float s = std::sin(toRadians(-rect.rotation));

    float x  = rect.x;
    float y  = rect.y;
    float w  = rect.w;
    float h  = rect.h;

    float rotX = x + c * (point.x - x) - s * (point.y - y);
    float rotY = y + s * (point.x - x) + c * (point.y - y);

    float lx = x - w / 2.f;
    float rx = x + w / 2.f;
    float ty = y - h / 2.f;
    float by = y + h / 2.f;

    return lx <= rotX && rotX <= rx && ty <= rotY && rotY <= by;
}

このコードは、原点が長方形の中心にあるが、他の原点(私がテストしたもの)にない場合に機能します。たとえば、原点が長方形の左上隅にある場合にも機能するようにするにはどうすればよいですか?

4

0 に答える 0