dongzhu3548 2015-06-26 23:00
浏览 49

根据包含的值与特定值的匹配程度对数组进行排序

The Scenario

I have a list of print formats, and an uploaded image.

The image may match certain formats well, and others less.

For instance, if the image is in ratio 4:3, it will match a format 40 x 30 cm.

However, there are also formats which do not fit the image exactly, for example: 40 x 35 cm.

The Problem

The formats need to be ordered by how well they fit the image.

Code

$formats = array(
    '30x40',
    '35x40',
    '40x60'
);

// Ratios in the format (such as 3:4) won't serve us in calculations.
// Therefore we use the images aspect factor, which we compute
// like this:

$imageAspectFactor = $imageX / $imageY;

At least that's how i think i could do this.

Question

From here on out, i have no idea where to go. Mathematically speaking, how can the aspect factor of the image be matched against the resulting aspect factor of the (current) format (in the loop), so that the resulting value can be used as an array key for building a new, ordered array of formats?

In order to understand this better, see the following, practical code snippet:

Practical Code

$formats = array(
    '30x40',
    '35x40',
    '40x60'
);

foreach($formats as $formatString){
    list($formatX, $formatY) = explode('x', $formatString);
    $formatAspectFactor = $formatX / $formatY;
    $output[/* NUMERIC THAT REPRESENTS HOW WELL FORMAT MATCHES IMAGE*/] = $formatString;
}

ksort($output);

Please take note: i don't need a complete solution, unless you feel overly motivated. What i look for is the mathematical understanding of how to calculate how well a number matches another one, and represent this, somehow, in a numeric.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?