我有一个数组数组,其坐标代表折线:
array = [
[3004.9999999999986, -500.0],
[5733.5, -500.0],
[5733.5, 300.0],
[8462.0, 300.0],
[8462.0, 1026.163313244362],
[7254.568694406579, 1026.163313244362],
[7254.568694406579, 1500.0],
[3004.9999999999914, 1500.0],
[3004.9999999999914, 1224.9602566586254],
[5.0, 1224.9602566586254],
[5.00000000000015, 300.0],
[3004.9999999999986, 300.0]
]
我必须找到具有最低x值和最高y值的坐标,其中较低的x值优先于较高的y值。
对于上面的数组,预期结果如下(最低x然后最高y):
[5.0, 1224.9602566586254]
答案 0 :(得分:4)
您可以使用Enumerable#min_by
:
array.min_by { |x, y| [x, -y] }
#=> [5.0, 1224.9602566586254]
array.max_by { |x, y| [-x, y] }
#=> [5.0, 1224.9602566586254]