2009-06-02 10 views
3

、私の脳は、サービスの判明...(それはトルコの23: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); 
} 

私はどのように行うことができます。..オリジナルの境界内に装着するものの最大サイズに回転処理の前に与えられた長方形のサイズを変更やろうとしていますか?

おかげで、あなたの事前

EDIT:イゴールKrivokonのソリューションの問題はここにイゴール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; 
のためにどのような作品をそのソリューションの修正版です

ありがとう

+0

クイック明確化、あなたは矩形Rを持っていて、矩形Rを生成するために、その中心の周りにそれを回転させ、スケールしますRの中のできるだけ多くの部分を占めているのですか? –

+0

こんにちはTolgahan、 少なくとも矩形の辺の関係を知らなくても、これに答えることはできません。 – tekBlues

+0

はい、私は回転前にR 'を持っています –

答えて

5

時間とワット、そしてあなたが回しは、角度ファイである、あなたのオリジナルのサイズは、新しい高さ

h1 = h*h/(w*sin(phi) + h*cos(phi)) 

h2 = h*w/(w*cos(phi) + h*sin(phi)) 

に計算し、HEWの高さh」とを選択しようとした場合h1とh2の最小値。

明らかに、新しい幅w' = h' * w/h

それを試してみてください - 私は私の数学をテストする時間がありませんでした:)

+0

あなたの数式は、角度がa => 0> = a <= 90の場合、今、私はこの方法の他のバリエーションをしようとしている場合はOKです –

+0

私は方法はわかりませんが、それは超甘いです。ありがとう! – Budius

0

4つの小さい矩形に矩形を分割します。これを斜めに半分にカットしてください(コーナーから中心点まで回転する前に)、8つの三角形があります。あなたはそれらの4つだけ必要です。あなたの回転の後、これらの三角形の斜辺が元の境界ボックスからはみ出しています。

斜辺の公式(元の角度45、-45,135、-135、開始点はmx + bなので)を決定し、これらの線を変換します(回転を追加することによって勾配を変更します)。 &は、どの斜辺が最短であるかを計算します(コーナーでの中心から壁までの距離)。境界の壁と交差します(y = 0、y = w、x = 0、x = h、距離式、無限の場合のテスト) 。すべての斜辺が同じ長さだったので、それらのすべてをこの新しい値にリサイズするだけで新しい長方形ができました。

私はそれを正しくやっていますか?

+0

私はそれがより簡単な方法を持っていると思う:) –

+0

私は方法を想像することはできません:私はそれを言い換えることができるかもしれません "どのくらい遠く突き出るコーナーが出て、それは – ryansstack

0
function resize_factor() 
{ 
    /* Find how far the upper-left corner sticks up beyond the top */ 
    overtop = gety(0, 0); 
    /* Compute a vertical resize factor that would put that point at the top */ 
    /* (be sure to use floating point arithmetic) */ 
    vertical_resize = _cy/(_cy - overtop); 

    /* Do the same for the lower-left corner sticking out beyond the left */ 
    /* (using 2*_cy for the height of the rectangle) */ 
    overleft = getx(0, 2*_cy);  
    horizontal_resize = _cx/(_cx - overleft); 

    /* Return whichever resize constraint is stricter */ 
    return min(vertical_resize, horizontal_resize); 
} 

function resize_x(x) 
{ 
    /* To get location of a point, after resize, before rotation... */ 
    /* ...multiply its resize factor by its distance from the center. */ 
    return resize_factor()*(x - _cx) + _cx; 
} 

function resize_y(y) 
{ 
    return resize_factor()*(y - _cy) + _cy; 
} 

/* These resized coordinates can be used inside any other code you want: */ 
function getx_after_resize_and_rotate(x, y) 
{ 
    return getx(resized_x(x), resized_y(y)); 
} 

注:このコードでは、時計回りに時計回りに90度未満回転していることを前提としています(これは写真のものです)。あなたのアングルが他のものであれば、4つのコーナーすべてをチェックして、最も遠いオーバートップとオーバーレイを決定する必要があります。

0
fitRect: function(rw,rh,radians){ 
      var x1 = -rw/2, 
       x2 = rw/2, 
       x3 = rw/2, 
       x4 = -rw/2, 
       y1 = rh/2, 
       y2 = rh/2, 
       y3 = -rh/2, 
       y4 = -rh/2; 

      var x11 = x1 * Math.cos(radians) + y1 * Math.sin(radians), 
       y11 = -x1 * Math.sin(radians) + y1 * Math.cos(radians), 
       x21 = x2 * Math.cos(radians) + y2 * Math.sin(radians), 
       y21 = -x2 * Math.sin(radians) + y2 * Math.cos(radians), 
       x31 = x3 * Math.cos(radians) + y3 * Math.sin(radians), 
       y31 = -x3 * Math.sin(radians) + y3 * Math.cos(radians), 
       x41 = x4 * Math.cos(radians) + y4 * Math.sin(radians), 
       y41 = -x4 * Math.sin(radians) + y4 * Math.cos(radians); 

      var x_min = Math.min(x11,x21,x31,x41), 
       x_max = Math.max(x11,x21,x31,x41); 

      var y_min = Math.min(y11,y21,y31,y41); 
       y_max = Math.max(y11,y21,y31,y41); 

      return [x_max-x_min,y_max-y_min]; 
     } 
関連する問題