计算最大矩形在一个旋转矩形 [英] Calculate largest rectangle in a rotated rectangle

查看:183
本文介绍了计算最大矩形在一个旋转矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到计算它可以包含一个旋转的矩形内最大(区)的矩形的最佳方式。

I'm trying to find the best way to calculate the biggest (in area) rectangle which can be contained inside a rotated rectangle.

有些照片应该帮助(我希望)的可视化我的意思:

Some pictures should help (I hope) in visualizing what I mean:

的宽度和输入矩形的高度给出,所以是角度旋转。输出矩形不旋转或倾斜。

The width and height of the input rectangle is given and so is the angle to rotate it. The output rectangle is not rotated or skewed.

我要下去了冗长的路线,我甚至不知道这是否会处理的情况(没有双关语意)。我敢肯定有一个优雅的解决了这一点。任何提示?

I'm going down the longwinded route which I'm not even sure if it will handle the corner cases (no pun intended). I'm certain there is an elegant solution to this. Any tips?

修改:输出矩形点不一定触摸输入矩形边缘。 (感谢到E先生)

EDIT: The output rectangle points don't necessarily have to touch the input rectangles edges. (Thanks to Mr E)

推荐答案

我刚刚来到这里寻找相同的答案。打了一个寒颤,在涉及这么多数学的思想后,我以为我会再打一个半猜测。涂鸦一个位I来到(直观的和可能不完全精确的)的结论,即最大矩形正比于外得到的矩形,它的两个相对的角部位于在该外矩形的对角线与的最长边的交点旋转矩形。对于广场,所有的对角线和边会做......我想我这个开心就好,现在将开始刷蜘蛛网了我的生锈的触发技能(可怜,我知道)。

I just came here looking for the same answer. After shuddering at the thought of so much math involved, I thought I would resort to a semi-educated guess. Doodling a bit I came to the (intuitive and probably not entirely exact) conclusion that the largest rectangle is proportional to the outer resulting rectangle, and its two opposing corners lie at the intersection of the diagonals of the outer rectangle with the longest side of the rotated rectangle. For squares, any of the diagonals and sides would do... I guess I am happy enough with this and will now start brushing the cobwebs off my rusty trig skills (pathetic, I know).

小更新......好不容易才做一些触发计算。这是因为当图象的高度比宽度大的情况下。

Minor update... Managed to do some trig calculations. This is for the case when the Height of the image is larger than the Width.

更新。得到了整个事情的工作。下面是一些JS code。它被连接到一个更大的程序,和大多数变量是的功能的范围之外,并直接从内的功能被修改。我知道这样不好,但我在一个孤立的情况下,哪里还会有其他的脚本不会混淆使用这样的:绝密

Update. Got the whole thing working. Here is some js code. It is connected to a larger program, and most variables are outside the scope of the functions, and are modified directly from within the functions. I know this is not good, but I am using this in an isolated situation, where there will be no confusion with other scripts: redacted

我把打扫code,并提取到一个函数的自由:

I took the liberty of cleaning the code and extracting it to a function:

function getCropCoordinates(angleInRadians, imageDimensions) {
    var ang = angleInRadians;
    var img = imageDimensions;

    var quadrant = Math.floor(ang / (Math.PI / 2)) & 3;
    var sign_alpha = (quadrant & 1) === 0 ? ang : Math.PI - ang;
    var alpha = (sign_alpha % Math.PI + Math.PI) % Math.PI;

    var bb = {
        w: img.w * Math.cos(alpha) + img.h * Math.sin(alpha),
        h: img.w * Math.sin(alpha) + img.h * Math.cos(alpha)
    };

    var gamma = img.w < img.h ? Math.atan2(bb.w, bb.h) : Math.atan2(bb.h, bb.w);

    var delta = Math.PI - alpha - gamma;

    var length = img.w < img.h ? img.h : img.w;
    var d = length * Math.cos(alpha);
    var a = d * Math.sin(alpha) / Math.sin(delta);

    var y = a * Math.cos(gamma);
    var x = y * Math.tan(gamma);

    return {
        x: x,
        y: y,
        w: bb.w - 2 * x,
        h: bb.h - 2 * y
    };
}

我遇到了一些问题与伽玛 -calculation,并修改了它考虑到在哪个方向原包装盒是最长的。

I encountered some problems with the gamma-calculation, and modified it to take into account in which direction the original box is the longest.

- 马格纳斯·霍夫

-- Magnus Hoff

这篇关于计算最大矩形在一个旋转矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆