Java中旋转矩形

发布于 2024-10-01 17:54:18 字数 318 浏览 6 评论 0原文

我需要创建围绕其中心旋转的矩形(因此它们不需要平行于坐标系的轴)。所以基本上每个矩形都可以由 center-Xcenter-Ywidthheight定义>角度。然后我想做的是计算这些矩形中是否包含某些点(因此不会涉及绘图)。我想我不能使用 Rectangle2D 类,因为这些矩形将始终平行于坐标系的 x 轴和 y 轴。是通过编写自己的矩形类来获得此功能的唯一方法还是有任何现有的东西(类似于 Rectangle2D )我可以使用?

I need to create rectangles that are rotated around their center (so they don't need to be parallel to the axes of the coordinate system). So basicelly each rectangle can be defined by center-X, center-Y, width, height and angle. What I want to do then is to perform calculations on whether certain points are contained in these rectangles or not (so no drawing will be involved). I guess I cant use the Rectangle2D class because these rectangles will always be parallel to the x and y-axis of the coordinate system. Is the only way to get this functionality by writing my own rectangle class or is there anything existing (similar to Rectangle2D) I can use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枯寂 2024-10-08 17:54:18

旋转所有要测试的点,并像 Mihai 那样使用 Rectangle2D 的 contains(Point) 方法。

但如果你真的想旋转矩形,你可以这样做(这是整数版本,但也许你也可以使用 Rectangle2D 来完成:))。

public class TestRotate {
    public static void main(String... args) {

        Rectangle r = new Rectangle(50, 50, 100, 100);
        Point check = new Point(100, 151); // clearly outside

        System.out.println("first: " + r.contains(check));

        AffineTransform at = AffineTransform.getRotateInstance(
                Math.PI/4, r.getCenterX(), r.getCenterY());

        Polygon p = new Polygon(); 

        PathIterator i = r.getPathIterator(at);
        while (!i.isDone()) {
            double[] xy = new double[2];
            i.currentSegment(xy);
            p.addPoint((int) xy[0], (int) xy[1]);
            System.out.println(Arrays.toString(xy));

            i.next();
        }

        // should now be inside :)
        System.out.println("second: " + p.contains(check));
    }
}

Rotate all the points you want to test and use contains(Point) method of the Rectangle2D as Mihai did.

But if you really want to rotate the rectangles you can do it like this (this is the integer version but probably you can do it with Rectangle2D aswell :)).

public class TestRotate {
    public static void main(String... args) {

        Rectangle r = new Rectangle(50, 50, 100, 100);
        Point check = new Point(100, 151); // clearly outside

        System.out.println("first: " + r.contains(check));

        AffineTransform at = AffineTransform.getRotateInstance(
                Math.PI/4, r.getCenterX(), r.getCenterY());

        Polygon p = new Polygon(); 

        PathIterator i = r.getPathIterator(at);
        while (!i.isDone()) {
            double[] xy = new double[2];
            i.currentSegment(xy);
            p.addPoint((int) xy[0], (int) xy[1]);
            System.out.println(Arrays.toString(xy));

            i.next();
        }

        // should now be inside :)
        System.out.println("second: " + p.contains(check));
    }
}
-残月青衣踏尘吟 2024-10-08 17:54:18

如果您不是将矩形旋转一个角度(例如逆时针),而是相对于矩形的中心顺时针旋转每个需要检查的点,则可以使用 Rectangle2D 来检查包含情况。像这样的东西

double dx = point.x - rectangleCenter.x;
double dy = point.y - rectangleCenter.y;
double newX = rectangleCenter.x - dx*Math.cos(angle) + dy*Math.sin(angle);
double newY = rectangleCenter.x - dx*Math.sin(angle) - dy*Math.cos(angle);

You can use Rectangle2D to check for containment, if instead of rotating your rectangle by an angle, say, counterclockwise, you rotate each of the points you need to check by the same angle clockwise, relative to the center of the rectangle. Something like

double dx = point.x - rectangleCenter.x;
double dy = point.y - rectangleCenter.y;
double newX = rectangleCenter.x - dx*Math.cos(angle) + dy*Math.sin(angle);
double newY = rectangleCenter.x - dx*Math.sin(angle) - dy*Math.cos(angle);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文