Is there a problem with just using the Euclidean distance? Zero represents equality:
def euclidean_distance(a, b)
dx = a[:width] - b[:width]
dy = a[:height] - b[:height]
Math.sqrt((dx * dx) + (dy * dy))
end
test_subject = { width: 200, height: 50 }
samples = [
{ width: 100, height: 100 },
{ width: 80, height: 200 },
{ width: 200, height: 50 },
{ width: 10, height: 10 }
]
distances = samples.map { |s| euclidean_distance(test_subject, s) }
samples.zip(distances) { |img, dist| puts "#{img[:width]}x#{img[:height]} => #{dist}" }
Output:
100x100 => 111.80339887498948
80x200 => 192.09372712298546
200x50 => 0.0
10x10 => 194.164878389476
You can then use sort
easily enough:
sorted = samples.sort { |a, b| euclidean_distance(test_subject, a) <=> euclidean_distance(test_subject, b) }