关于:矩形旋转和拟合

Tol*_*rak 3 algorithm math

经过艰苦的努力,我的大脑停止服务..(土耳其时间是晚上11点40分)

我正在做轮换工作:

变量:

_cx = horizontal center of rect
_cy = vertical center of rect
_cos = cos value of current angle
_sin = sin value of current angle

to rotating any point in this rect :

function getx(x, y)
{
      return _cx + _cos * (x - _cx) - _sin * (y - _cy);
}
function gety(x, y)
{
      return _cy + _sin * (x - _cx) + _cos * (y - _cy);
}
Run Code Online (Sandbox Code Playgroud)

我试图在旋转过程之前调整给定矩形的大小以适应原始边界的最大尺寸..我该怎么办?

谢谢你的进步

编辑:Igor Krivokon的解决方案

这个问题由Igor Krivokon解决,这里是该解决方案的修改版本,适用于每个角度值

var h1:Number, h2:Number, hh:Number, ww:Number,
    degt:Number, d2r:Number, r2d:Number, deg:Number,
    sint:Number, cost:Number;
//@angle = given angle in radians
//@r is source/target rectangle
//@d2r is static PI / 180 constant for degree -> radian conversation
//@r2d is static 180 / PI constant for radian -> degree conversation
d2r = 0.017453292519943295769236907683141;
r2d = 57.295779513082320876798154814105;
deg = Math.abs(angle * r2d) % 360;
if(deg < 91)
{
    degt = angle;
}else if(deg < 181){
    degt = (180 - deg) * d2r;
}else if(deg < 271){
    degt = (deg - 180) * d2r;
}else{
    degt = (360 - deg) * d2r;
}

sint = Math.sin(degt);
cost = Math.cos(degt);

h1 = r.height * r.height / (r.width * sint + r.height * cost);
h2 = r.height * r.width / (r.width * cost + r.height * sint);
hh = Math.min(h1, h2);
ww = hh * r.width / r.height;
r.x = (r.width - ww) * .5;
r.y = (r.height - hh) * .5;
r.height = hh;
r.width = ww;
Run Code Online (Sandbox Code Playgroud)

谢谢

Igo*_*kon 5

如果您的原始尺寸为h和w,并且您转动的角度为phi,请尝试计算新的高度

h1 = h*h / (w*sin(phi) + h*cos(phi))
Run Code Online (Sandbox Code Playgroud)

h2 = h*w / (w*cos(phi) + h*sin(phi))
Run Code Online (Sandbox Code Playgroud)

并选择hew高度h'作为h1和h2中的最小值.

然后,很明显,新的宽度w' = h' * w / h.

请试试 - 我没有时间测试我的数学:)