简体   繁体   English

沿着3D JavaScript数组的单轴查找最小值/最大值

[英]Find the min/max along a single axis of a 3D javascript array

I have a 3-dimensional array that contains sets of min/max latlng bounds for a collection of n polygons. 我有一个3维数组,其中包含一组n个多边形的最小/最大latlng范围。 I want to find the min and max coordinates from the set of all polygons. 我想从所有多边形集中找到最小和最大坐标。

My solution below works, but I find it ungainly. 我下面的解决方案有效,但是我发现它很麻烦。 My real question is: Is there a way to obtain the min/max along an axis so that it returns an array of form [ [lat_min, lon_min], [lat_max, lon_max] ] without performing a reduce function on each point separately? 我真正的问题是:是否有一种方法可以获取沿轴的最小值/最大值,从而返回格式为[ [lat_min, lon_min], [lat_max, lon_max] ]而无需分别对每个点执行reduce函数?

// Where bounds is an array of latlon bounds for n polygons:
// bounds = [
//   [ [min_lat_1, min_lon_1], [max_lat_1, max_lon_1] ],
//   ...
//   [ [min_lat_n, min_lon_n], [max_lat_n, max_lon_n] ]
// ]
const x1 = bounds.reduce((min, box) => {
  return box[0][0] < min ? box[0][0] : min;
}, bounds[0][0][0]);
const y1 = bounds.reduce((min, box) => {
  return box[0][1] < min ? box[0][1] : min;
}, bounds[0][0][1]);
const x2 = bounds.reduce((max, box) => {
  return box[1][0] > max ? box[1][0] : max;
}, bounds[0][1][0]);
const y2 = bounds.reduce((max, box) => {
  return box[1][1] > max ? box[1][1] : max;
}, bounds[0][1][1]);

EDIT: The responses I've gotten so far improve on my code, but so far nothing quite does what I'm hoping for. 编辑:到目前为止,我得到的响应对我的代码有所改进,但是到目前为止,我所期望的并没有做什么。

Some further background/specification: I'm more familiar with python/numpy, where you can specify to apply a function across any axis. 一些进一步的背景知识/规范:我对python / numpy更为熟悉,您可以在其中指定在任何轴上应用函数。 In this case, I would want to apply my function along axis 3, ie the depth axis. 在这种情况下,我想沿轴3(即深度轴)应用函数。 But, since I'm not just looking for min/max, the function I create would also need to return a function (Min or Max) depending on index. 但是,由于我不仅在寻找最小值/最大值,因此我创建的函数还需要根据索引返回一个函数(最小值或最大值)。 Is this simply not feasible in Javascript? 这在Javascript中根本不可行吗? It just seems like there should be some elegant combo in es6 that gets the job done. 似乎es6中应该有一些优雅的组合可以完成工作。

You can use .apply on Math.min or Math.max to pass an array of numbers to compare at once. 您可以在Math.minMath.max上使用.apply传递数字数组以立即进行比较。

You can also use .map to extract either X or Y of your coordinates. 您还可以使用.map提取坐标的X或Y。

 const bounds = [ [ [1, 2], [3, 999] ], [ [-999, 6], [7, 8] ], [ [9, 10], [11, 12] ] ] // `pos` determines whether it should get the min set or the max set const getXOf = pos => bounds.map(a => a[pos][0]); const getYOf = pos => bounds.map(a => a[pos][1]); // Using .apply, you can compare an array of numbers at once. const findMin = arr => Math.min.apply(null, arr); const findMax = arr => Math.max.apply(null, arr); // For clarity only const MIN = 0; const MAX = 1; const min_x = findMin(getXOf(MIN)); const min_y = findMin(getYOf(MIN)); const max_x = findMax(getXOf(MAX)); const max_y = findMax(getYOf(MAX)); console.log(min_x, min_y, max_x, max_y); 

You could combine all the reduces in just one and return the array [ [lat_min, lon_min], [lat_max, lon_max] ] like this: 您可以将所有的reduces合并为一个,然后返回数组[ [lat_min, lon_min], [lat_max, lon_max] ]如下所示:

 const bounds = [ [ [ 0.0, 0.1 ], [ 0.6, 0.7 ] ], [ [ 0.2, 0.3 ], [ 0.8, 0.9 ] ], [ [ 0.4, 0.5 ], [ 0.1, 0.2 ] ] ]; const minMax = bounds.reduce((current, box) => { const minLat = box[0][0] < current[0][0] ? box[0][0] : current[0][0]; const minLon = box[0][1] < current[0][1] ? box[0][1] : current[0][1]; const maxLat = box[1][0] > current[1][0] ? box[1][0] : current[1][0]; const maxLon = box[1][1] > current[1][1] ? box[1][1] : current[1][1]; return [ [ minLat, minLon ], [ maxLat, maxLon ] ] }, bounds[0]); console.log('combined reduce:', minMax); 

Below is your code for reference: 以下是您的代码以供参考:

 const bounds = [ [ [ 0.0, 0.1 ], [ 0.6, 0.7 ] ], [ [ 0.2, 0.3 ], [ 0.8, 0.9 ] ], [ [ 0.4, 0.5 ], [ 0.1, 0.2 ] ] ]; const x1 = bounds.reduce((min, box) => { return box[0][0] < min ? box[0][0] : min; }, bounds[0][0][0]); const y1 = bounds.reduce((min, box) => { return box[0][1] < min ? box[0][1] : min; }, bounds[0][0][1]); const x2 = bounds.reduce((max, box) => { return box[1][0] > max ? box[1][0] : max; }, bounds[0][1][0]); const y2 = bounds.reduce((max, box) => { return box[1][1] > max ? box[1][1] : max; }, bounds[0][1][1]); console.log('separate reduce:', [ [ x1, y1 ], [ x2, y2 ] ]); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM