Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unless your array is huge, have you tried a simple linear search of the array while keeping track of the "best-match-so-far"? The best-match can be determined using a scoring/weighing/rating function that you apply to each item in the array in sequence. The item with the highest "score" wins. Initialize two variables called say bestMatchIndex and bestMatchScore. Use a for-loop to go through the array. For each item, call the scoring function to get its score. Compare that to the bestMatchScore so far. If the current item's score is higher, set the bestMatch variables to the current item. At the end of the loop, the bestMatch variables point you to the "best match".</p> <p>The scoring function can be as simple or complicated as you want.</p> <p>Here's an example of a scoring function (I know you said you're not looking for an implementation but sometimes some code is worth a thousand words). In the example, I am looking for items that deviate the least from both the width and height and ratio. Your requirements might be different.</p> <pre><code>-(double)itemScoreForWidth:(double)itemWidth height:(double)itemHeight { const double targetWidth = 400.0; const double targetHeight = 600.0; const double targetScore = 7.0; double widthDiff = (itemWidth / targetWidth); double heightDiff = (itemHeight / targetHeight); double ratioDiff = ((itemWidth/itemHeight) / (targetWidth/targetHeight)); double product = (4 * ratioDiff) + (2 * widthDiff) + heightDiff; return fabs(targetScore - product); } </code></pre> <p>(The implementation might be more complicated than necessary.) </p> <p>Running this with some sample dimensions, we get:</p> <pre><code>score for 400x600 : 0.000000 score for 401x600 : 0.015000 score for 401x601 : 0.009994 score for 401x598 : 0.025078 score for 500x700 : 0.952381 score for 602x400 : 5.706667 score for 200x300 : 1.500000 score for 800x1200: 3.000000 score for 410x610 : 0.099454 score for 600x400 : 5.666667 </code></pre> <p>So in this example, the item with the <strong>lowest</strong> score should "win". The "winner" depends on the scoring function. Above, if it was a choice between only the 500x700 and the 600x400, the 500x700 would win.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload