简体   繁体   English

Javascript - 从值数组中查找线段的长度

[英]Javascript - Finding length of line segments from an array of values

I'm working on a project that requires me to parse an array of an array of values, and work out the length of the line segments.我正在做一个项目,该项目需要我解析一组值的数组,并计算出线段的长度。

The array that I have is in the form where each element is a tuple of (xCoordinate, yCoordinate) :我拥有的数组的形式是每个元素都是(xCoordinate, yCoordinate)的元组:

[[1.4,2.8],[2.3,2.2],[3.2,1.6],[3.8,1.1]]

I would like to calculate the length of the line segment between, say, array element 1 (1.4,2.8) and 2 (2.3,2.2) , then 2 and 3, 3 and 4, etc. Also I want to avoid for-loops if possible, to try to enhance my knowledge of functional programming, coming from a Java background.我想计算数组元素 1 (1.4,2.8)和 2 (2.3,2.2)之间的线段长度,然后计算 2 和 3、3 和 4 等。我也想避免 for 循环如果可能的话,尝试增强我的函数式编程知识,来自 Java 背景。

I am aware that I probably have to parse in both values into a map function, and perform a calculation like sqrt((y2-y1)^2+(x2-x1)^2))我知道我可能必须将两个值解析为 map function,并执行类似sqrt((y2-y1)^2+(x2-x1)^2))的计算

I have an xyDistance() function written:我有一个xyDistance() function 写:

function xyDistance(from,to){
    var xy=from.split(",");
    var x1=xy[0];
    var y1=xy[1];
     
    var xy=to.split(",");
    var x2=xy[0];
    var y2=xy[1];
    var dt = Math.sqrt((x2-x1)**2+(y2-y1)**2);
    return dt;
}

Which seems to work fine, when I manually enter the co-ordinates.当我手动输入坐标时,这似乎工作正常。 Then I imagine I want something like然后我想我想要类似的东西

var map = arr.map(arr[x] => xyDistance(arr[x],arr[x+1]))

But this throws errors.但这会引发错误。

You can use Array#slice to remove the last element and then use Array#map along with Math.hypot to find the distance between adjacent line segments.您可以使用Array#slice删除最后一个元素,然后使用Array#mapMath.hypot来查找相邻线段之间的距离。

 const arr = [[1.4,2.8],[2.3,2.2],[3.2,1.6],[3.8,1.1]]; const res = arr.slice(0, -1).map(([x, y], idx)=>Math.hypot(x - arr[idx + 1][0], y - arr[idx + 1][1])); console.log(res);

First of all, your expression has syntax error:首先,您的表达式有语法错误:

var map = arr.map(arr[x] => xyDistance(arr[x],arr[x+1]))

Should be corrected as:应更正为:

var map = arr.map((element, x) => xyDistance(arr[x], arr[x+1]))

Noted that when you have n elements, you only get n - 1 distance.请注意,当您有n元素时,您只会得到n - 1距离。 That's why iota's answer slice off the last element before calling Array#map .这就是为什么 iota 的答案在调用Array#map之前切掉最后一个元素。

var map = arr.slice(0, -1).map((element, x) => xyDistance(arr[x], arr[x + 1]))

Second part of your problem is that xyDistance function needs some fix.您问题的第二部分是xyDistance function 需要一些修复。 Apparently the elements in your array are strings, not tuple.显然,数组中的元素是字符串,而不是元组。 You've given misleading example code in your post.您在帖子中提供了误导性示例代码。 So in order to parse those string elements into tuple of numbers, you need to cast string type to number type:因此,为了将这些字符串元素解析为数字元组,您需要将字符串类型转换为数字类型:

function xyDistance(from,to){
    var xy=from.split(",");
    // trim any excessive whitespaces, then convert to number type:
    var x1=Number(xy[0].trim());
    var y1=Number(xy[1].trim());
     
    var xy=to.split(",");
    var x2=Number(xy[0].trim());
    var y2=Number(xy[1].trim());
    var dt = Math.sqrt((x2-x1)**2+(y2-y1)**2);
    return dt;
}

And you're good to go now.你现在对 go 很好。

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

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