Calculate largest rectangle in a rotated rectangle

后端 未结 8 2275
你的背包
你的背包 2020-11-28 03:20

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) i

8条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 04:02

    sorry for not giving a derivation here, but I solved this problem in Mathematica a few days ago and came up with the following procedure, which non-Mathematica folks should be able to read. If in doubt, please consult http://reference.wolfram.com/mathematica/guide/Mathematica.html

    The procedure below returns the width and height for a rectangle with maximum area that fits into another rectangle of width w and height h that has been rotated by alpha.

    CropRotatedDimensionsForMaxArea[{w_, h_}, alpha_] := 
    With[
      {phi = Abs@Mod[alpha, Pi, -Pi/2]},
      Which[
       w == h, {w,h} Csc[phi + Pi/4]/Sqrt[2],
       w > h, 
         If[ Cos[2 phi]^2 < 1 - (h/w)^2, 
           h/2 {Csc[phi], Sec[phi]}, 
           Sec[2 phi] {w Cos[phi] - h Sin[phi], h Cos[phi] - w Sin[phi]}],
       w < h, 
         If[ Cos[2 phi]^2 < 1 - (w/h)^2, 
           w/2 {Sec[phi], Csc[phi]}, 
           Sec[2 phi] {w Cos[phi] - h Sin[phi], h Cos[phi] - w Sin[phi]}]
      ]
    ]
    

提交回复
热议问题