简体   繁体   中英

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)?

在此处输入图像描述

Code snippet doesn't seem to allow console.table , so here it is with console.log instead:

 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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