rotation - Check if point is inside a rotated rectangle (with different rectangle origins) -
how can check if point inside rotaded rectangle when rectangle can have different origins? i'm using now:
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 = y + h / 2.f; return lx <= rotx && rotx <= rx && ty <= roty && roty <= by; }
code work when origin @ center of rectangle not in other origins (that have tested). how can make works when origin example @ top-left corner of rectangle?
Comments
Post a Comment