如何确定哪些长宽比接近的 [英] How to determine which aspect ratios are closest

查看:169
本文介绍了如何确定哪些长宽比接近的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个长方形S,与纵横比SX /学年,和另外两个矩形A(宽高比为AX / AY)和B(与纵横比BX / BY),我怎么能找出形状A或B具有封闭宽高比至S?形状大小并不重要。

难道只是两者的(SX / SY)/(AX / AY)和(SX / SY)/(BX / BY)是最接近1?

我所真正想要做的是找出哪些形状上PPTX幻灯片将最适合将要调整的图像,然后裁剪以适合的形状。我想另一种办法是制定出哪些形状导致最少像素丢失,虽然在我的code这将是更容易,如果我可以通过比较宽比做到这一点。

在结束我的算法去上面,实现如下(感谢马特球对他的反馈意见):

  ShapeInPPTXLocation最接近;
双imageAR = a_imageDim.getWidth()/ a_imageDim.getHeight();
双aspectRatioCandidateA = a_candidateA.getWidth()/ a_candidateA.getHeight();
双aspectRatioCandidateB = a_candidateB.getWidth()/ a_candidateB.getHeight();
双closenessScoreA = 1-(imageAR / aspectRatioCandidateA);
双closenessScoreB = 1-(imageAR / aspectRatioCandidateB);

如果(Math.abs(closenessScoreA)< = Math.abs(closenessScoreB))
{
    最接近= a_candidateA;
}
其他
{
    最接近= a_candidateB;
}
 

解决方案
  

难道只是两者的(SX / SY)/(AX / AY)和(SX / SY)/(BX / BY)是最接近1?

这听起来很有道理。你也可以只减少差异:

 让target_ratio = SX / SY
让a_ratio = AX / AY
让b_ration = BX / BY

如果| target_ratio  -  a_ratio | < | target_ratio  -  b_ratio |
    a_ratio更接近目标
其他
    b_ratio更接近目标
 

更新:该算法在这个答案完全不是那么回事,如下面的注释说明。更新了他的问题,包括他所使用的算法,它的工作原理OP似乎很好地工作。

Given a rectangular shape S, with aspect ration 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 shape A or B has the closed aspect ratio to S? The shapes' sizes are unimportant.

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

What I am actually trying to do is find out which shape on a PPTX slide would best fit an image that will be resized and then cropped to fit that shape. I guess another approach would be to work out which shape results in the fewest pixels being lost, although in my code it will be easier if I can do it by comparing aspect ratios.

In the end I went with the algorithm above, implemented as follows (thanks to Matt Ball for his feedback):

ShapeInPPTXLocation closest;
double imageAR = a_imageDim.getWidth()/a_imageDim.getHeight();
double aspectRatioCandidateA = a_candidateA.getWidth()/a_candidateA.getHeight();
double aspectRatioCandidateB = a_candidateB.getWidth()/a_candidateB.getHeight();
double closenessScoreA=1-(imageAR/aspectRatioCandidateA);
double closenessScoreB=1-(imageAR/aspectRatioCandidateB);

if (Math.abs(closenessScoreA) <= Math.abs(closenessScoreB))
{
    closest=a_candidateA;
}
else
{
    closest=a_candidateB;
}

解决方案

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.

这篇关于如何确定哪些长宽比接近的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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