简体   繁体   English

从给定的坐标(x,y)计算边界框的宽度和高度

[英]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()函数将输入数组转换为xy值数组。 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()以获得leftrightbottomtop来计算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 值。

  • The min-x coordinate of the bounding box ( => left corner ) is the minX value.边界框 ( => left corner ) 的 min-x 坐标是 minX 值。
  • The min-y coordinate of the bounding box ( => top ) is the minY value.边界框 ( => top ) 的 min-y 坐标是 minY 值。

  • The max-x coordinate of the bounding box ( => right corner ) is the maxX value.边界框 ( => right corner ) 的 max-x 坐标是 maxX 值。
  • The max-y coordinate of the bounding box ( => bottom ) is the maxY value.边界框 ( => bottom ) 的 max-y 坐标是 maxY 值。

Size (property w and h ) can be calulated by substracting maxX - minX and maxY - minY .大小(属性wh )可以通过减去maxX - minXmaxY - 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.

相关问题 通过JavaScript计算旋转元素的边界框的X,Y,高度和宽度 - Calculate the bounding box's X, Y, Height and Width of a rotated element via JavaScript 根据x和y坐标以及javascript中的宽度和高度值计算面积 - calculate an area based on x and y coordinates and width and height values in javascript 访问内联 SVG 转换后的组边界框(x 和 y 位置、宽度和高度)? - Access inline SVG's transformed group bounding box (x and y position, width and height)? 从 SVG 文本对象的坐标计算边界框坐标 - Calculate bounding box coordinates from the coordinates of an SVG text object 如何在使用 JSPDF 新 html API 从 html 生成 pdf 时给出宽度、高度、x 和 y 坐标 - How to give width, height, x and y coordinates in generating pdf from html using JSPDF new html API xy坐标和html5中sprite图片的宽度和高度 - x-y coordinates & width-height of an image from sprite in html5 如何根据xy坐标和width-height排列多个html元素? - How to arrange multiple html elements based on x-y coordinates and width-height? 从x,y,宽度,高度和旋转角度获取旋转的矩形点 - Get rotated rectangle points from x, y, width, height and rotation 计算 x,y 坐标在旋转中的分解 - Calculate x,y coordinates factoring in a rotation 如何使用jQuery通过X和Y坐标绘制框? - How to draw a box by X and Y coordinates with jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM