简体   繁体   English

计算图上两点的交点 Javascript

[英]Calculate intersection between two points on a graph Javascript

Given the below graphic, how would I provide a function pairs of x,y coordinates, and return a new x,y coordinate based on the pair's x,y intersection on the graph (preserving the order the original x,y pairs were given)?给定下图,我将如何提供 function 对 x、y 坐标,并根据该对在图上的 x、y 交点返回一个新的 x、y 坐标(保留原始 x、y 对的顺序) ?

在此处输入图像描述

Code snippet doesn't seem to allow console.table , so here it is with console.log instead:代码片段似乎不允许使用console.table ,所以这里使用console.log代替:

 var points=[ {name:"A", x:6, y:8}, {name:"B", x:-6, y:2}, {name:"C", x:4, y:-4} ]; function intersection(pntsArr) { var returnValue=[]; for(var i=0; i<pntsArr.length-1; i++) { for(var j=i+1; j<pntsArr.length; j++) { returnValue.push( { name:pntsArr[i].name+"->"+pntsArr[j].name, x:pntsArr[i].x, y:pntsArr[j].y } ); returnValue.push( { name:pntsArr[j].name+"->"+pntsArr[i].name, x:pntsArr[j].x, y:pntsArr[i].y } ); } } return returnValue; } console.log(intersection(points));

Should work with any number of points.应该适用于任意数量的点。
Could produce duplicate points, depending on input.可能会产生重复点,具体取决于输入。

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

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