[英]Calculate bounding box width and height from given coordinates(x,y)
I have a list of coordinates我有一个坐标列表
const coords = [{x:10, y:20}, {x:5, y:6}, {x:1, y:25}, {x:11, y:2}];
And I am wondering is there a way to calculate bounding box width and height having only these coordinates?我想知道有没有办法计算只有这些坐标的边界框宽度和高度?
Using the map()
function transform the input array into array of x
or y
values.使用map()
函数将输入数组转换为x
或y
值数组。 You can then feed these transformed arrays to Math.min()
and Math.max()
to get left
, right
, bottom
and top
to compute the bounding box
.然后,您可以将这些转换后的数组提供给Math.min()
和Math.max()
以获得left
、 right
、 bottom
和top
来计算bounding box
。 When you have the bounding box
, the width and height calculation is straight forward (subtract min from max).当你有bounding box
,宽度和高度的计算是直接的(从最大值中减去最小值)。 See code snippet below.请参阅下面的代码片段。
const coords = [{x:10, y:20}, {x:5, y:6}, {x:1, y:25}, {x:11, y:2}]; const xArr = coords.map(c => cx); const yArr = coords.map(c => cy); const l = Math.min(...xArr); const r = Math.max(...xArr); const b = Math.min(...yArr); const t = Math.max(...yArr); const width = r - l; const height = t - b; console.log(width, height);
I guess, that would be the difference between minimum and maximum values of x
(width) and y
(height).我想,这将是x
(宽度)和y
(高度)的最小值和最大值之间的差异。
While the obvious way to calculate those may seem to be using of Math.max()
/ Math.min()
over extracted arrays of coordinates, it requires looping source array several times unnecessarily, whereas single pass (eg with Array.prototype.reduce()
) is quite enough and may perform noticeably faster , when input array is relatively large:虽然计算这些的明显方法似乎是在提取的坐标数组上使用Math.max()
/ Math.min()
,但它需要不必要地多次循环源数组,而单次传递(例如使用Array.prototype.reduce()
) 就足够了,并且当输入数组相对较大时,执行速度可能会明显更快:
const points = [{x:10, y:20}, {x:5, y:6}, {x:1, y:25}, {x:11, y:2}], {width, height} = points.reduce((acc, {x,y}) => { const {min, max} = Math if(!acc.mX || x < acc.mX){ acc.mX = x } else if (!acc.MX || x > acc.MX){ acc.MX = x } if(!acc.mY || y < acc.mY){ acc.mY = y } else if (!acc.MY || y > acc.MY){ acc.MY = y } acc.width = acc.MX - acc.mX acc.height = acc.MY - acc.mY return acc }, {width: 0, height: 0}) console.log(`width: ${width}; height: ${height}`)
Have a look at this snippet.看看这个片段。 Guess this is an approach to start with.猜猜这是一种开始的方法。
Some explanations :一些解释:
We're calculating the minX, maxX, minY and maxY values from the coords set using Math.operationMinOrMax(...coords.map(c => c.propertyXorY))
.我们正在使用Math.operationMinOrMax(...coords.map(c => c.propertyXorY))
从坐标集中计算 minX、maxX、minY 和 maxY 值。
Size (property w and h ) can be calulated by substracting maxX - minX
and maxY - minY
.大小(属性w和h )可以通过减去maxX - minX
和maxY - minY
。
const boundingBox = (coords) => { const minX = Math.min(...coords.map(c => cx)), maxX = Math.max(...coords.map(c => cx)); const minY = Math.min(...coords.map(c => cy)), maxY = Math.max(...coords.map(c => cy)); return { x: minX, y: minY, w: maxX - minX, h: maxY - minY } } console.log( boundingBox( [ {x:10,y:5}, {x:100,y:0}, {x:100,y:100}, {x:12,y:50}, {x:0,y:100}, {x:27,y:22} ]) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.