How to determine which aspect ratios are closest

前端 未结 2 856
你的背包
你的背包 2021-02-15 17:03

Given a rectangular shape S, with aspect ratio sx/sy, and two other rectangular shapes A (with aspect ratio ax/ay) and B (with aspect ratio bx/by) how can I find out which of sh

相关标签:
2条回答
  • 2021-02-15 17:25

    Looking at the suggestion above, I'm not convinced:

    Think on the following example: A = 1:2 B = 2:1 and

    targetRatio = 1:1

    Clearly both A & B should be equally suitable, but with the comparison of

    ( 1 - GoalAR/CandiateAR) as suggested,

    aspectRatioCandidateA = 0.5 [ 1 : 2 ]

    aspectRatioCandidateB = 2 [ 2 : 1 ]

    you'd get

    closenessScoreA = 1

    closenessScoreB = 0.5

    The best way to compare aspect ratios is thinking of them as defining an angle:

    tan(o) = h/w

    o = atan( h/w )

    You can then simply compare the difference of the angles now.

    0 讨论(0)
  • 2021-02-15 17:30

    Is it just whichever of (sx/sy)/(ax/ay) and (sx/sy)/(bx/by) is closest to 1?

    That sounds reasonable. You could also just minimize the difference:

    let target_ratio = sx/sy
    let a_ratio = ax/ay
    let b_ration = bx/by
    
    if |target_ratio - a_ratio| < |target_ratio - b_ratio|
        a_ratio is closer to target
    else
        b_ratio is closer to target
    

    Update: the algorithm in this answer does not quite work, as explained in the comments below. The OP updated his question to include the algorithm that he used, which works seems to work fine.

    0 讨论(0)
提交回复
热议问题