简体   繁体   English

在旋转的矩形内找到一个点

[英]Find a point inside a rotated rectangle

Ok so, this should be super simple, but I'm not a smart man. 好的,这应该超级简单,但是我不是一个聪明人。 Technically I want to know whether a point resides inside a rectangle, however the rectangle can be in different states. 从技术上讲,我想知道一个点是否位于矩形内,但是矩形可以处于不同的状态。 In my current context when I want to draw a rectangle rotated by, lets say, 45° clockwise, what I do is rotate the entire x,y axis centered at the top-left corner of the rectangle and then I just draw the rectangle as if nothing has happened. 在当前上下文中,当我想绘制一个顺时针旋转45°的矩形时,我要做的是旋转以该矩形的左上角为中心的整个x,y轴,然后我将其绘制为如果什么都没发生。 Same goes if I want to draw the rectangle at a random coordinate. 如果我想在随机坐标处绘制矩形,也是如此。 Given that is the coordinate system who gets tossed and rotated, the rectangle always thinks it's being drawn at (0,0) with 0°, therefore, the best way to find if a given point is inside the rectangle would be to find the projection for the point based on the translation + rotation of the rectangle. 考虑到坐标系被旋转并旋转,矩形始终认为它是在(0,0)处以0°绘制的,因此,找到给定点是否在矩形内的最佳方法是找到投影基于矩形的平移+旋转的点。 But I have no idea how to do that. 但是我不知道该怎么做。

This is what I currently do in order to find out if a point is inside a rectangle (not taking into consideration rotation): 这是我目前正在执行的操作,以便确定点是否在矩形内(不考虑旋转):

bool Image::isPointInsideRectangle(int x, int y, const ofRectangle & rectangle){
    return x - xOffset >= rectangle.getX() && x - xOffset <= rectangle.getX() + rectangle.getWidth() &&
            y - yOffset >= rectangle.getY() && y - yOffset <= rectangle.getY() + rectangle.getHeight();
}

I already have angleInDegrees stored, as long as I could use it to project the (x,y) point I receive I should be able find out if the point is inside the rectangle. 我已经存储了angleInDegrees ,只要我可以使用它来投影我收到的(x,y)点,我就应该能够知道该点是否在矩形内。

Cheers! 干杯!

Axel 阿克塞尔

The easiest way is to un-rotate x,y in the reverse direction relative to the origin and rotation of the rectangle. 最简单的方法是相对于矩形的原点和旋转沿相反的方向取消旋转x,y。

For example, if angleInDegrees is 45 degrees, you would rotate the point to test -45 degrees (or 315 degrees if your rotation routine only allows positive rotations). 例如,如果angleInDegrees为45度,则可以旋转该点以测试-45度(如果旋转例程仅允许正旋转,则为315度)。 This will plot the x,y on the same coordinate system as the unrotated rectangle. 这会将x,y绘制在与未旋转矩形相同的坐标系上。

Then, you can use the function you already provided to test whether the point is within the rectangle. 然后,您可以使用已经提供的功能来测试该点是否在矩形内。

Note that prior to rotating x,y, you will probably need to adjust the x,y relative to the point of rotation - the upper-left corner of the rectangle. 请注意,在旋转x,y之前,您可能需要相对于旋转点-矩形的左上角调整x,y。 Since the rotation is relative to that point rather than the overall coordinate origin 0,0. 由于旋转是相对于该点而不是相对于整个坐标原点0,0。 You can compute the difference between x,y and the upper-left corner of your rectangle (which won't change during rotation), then simply rotate the adjusted point by -angleToRotate, then add the origin point difference back into the unrotated point to get absolute coordinates on your coordinate system. 您可以计算x,y与矩形左上角之间的差(旋转时不会改变),然后只需通过-angleToRotate旋转调整后的点,然后将原点差添加回未旋转的点即可。获取坐标系上的绝对坐标。

Editted: 编辑:

#include <cmath>

bool Image::isPointInsideRectangle(int x, int y, const ofRectangle & rectangle){
    return x*cosd(deg) - y*sin(deg) + xOffset >= rectangle.getX()
        && x*cosd(deg) - y*sin(deg) + xOffset <= rectangle.getX() + rectangle.getWidth() 
        && x*sind(deg) + y*cosd(deg) + yOffset >= rectangle.getY()
        && x*sind(deg) + y*cosd(deg) + yOffset <= rectangle.getY() + rectangle.getHeight();

Like you have already told, you could translate the coordinates of your point into the space of the rectangle. 就像您已经说过的那样,您可以将点的坐标转换为矩形的空间。 This is a common task in many software products which work with geometry. 在许多使用几何图形的软件产品中,这是常见的任务。 Each object have it own coordinate space and works as it would be at position (0, 0) without rotation. 每个对象都有自己的坐标空间,并且可以不旋转地在位置(0,0)上工作 If your rectangle is at position v and rotated about b degree/radian , than you can translate your point P into the space of the rectangle with the following formula: 如果矩形位于位置v并绕b度/弧度旋转,则可以使用以下公式将点P转换为矩形的空间:

| cos(-b)  -sin(-b) |   | P_x - v_x |
|                   | ⋅ |           |
| sin(-b)   cos(-b) |   | P_y - v_y |

Many of the most important transformations can be represented as matrices. 许多最重要的变换可以表示为矩阵。 At least if you are using homogeneous coordinates . 至少如果您使用齐次坐标 It is also very common to do that. 这样做也是很常见的。 Depending of the complexity and the goals of your program you could consider to use some math library like glm and use the transformations of your objects in form of matrices. 根据程序的复杂性和目标,您可以考虑使用一些数学库(例如glm)并以矩阵形式使用对象的转换。 Then you could write something like inverse(rectangle.transformation()) * point to get point translated into the space of rectangle . 然后,您可以编写诸如inverse(rectangle.transformation()) * point以将point转换为rectangle的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM