[英]How to get the intersection points of an Arc and a line?
好吧...我们走了。 以下代码段适用于任何圆弧(使用角度和半径定义)以及所需的任意数量的等距段。
当前,它假定圆弧完美地水平放置(如您的示例中所示),但是可以“轻松”扩展它以允许平移/旋转的圆弧。
getLinesCoords()
函数将返回一个对象,该对象的x
和y
属性包含每个段具有相应坐标的数组。
“ y”坐标是距中心(图像中的G)的线段的“高度”,“ x”是起始/结束位置,始终是距中心的位置(左/右取决于符号+/-)。
如有任何疑问,请询问。
// *** ARC *** const R = 100; // RADIUS const PHI = 180; // ANGLE (DEG) // *** LINES *** const LINES = 10; // NUMBER OF LINES TO BE PLACED // *** CALCULATIONS *** const coords = getLinesCoords(R, PHI, LINES); console.log(coords); function getLinesCoords(radius, angle, linesNum) { let i = 0; let arcAvailHeight = 0; let linesSep = 0; let linesYCoords = []; let linesXCoords = []; let angleRad = angle * Math.PI / 180; // GET AVAILABLE HEIGHT FOR PLACING LINES arcAvailHeight = radius * (1 - Math.cos(angleRad / 2)); // GET LINES SEPARATION linesSep = arcAvailHeight / (linesNum + 1); // GET Y COORDINATES FOR LINES for (i = 0; i < linesNum; i++) { linesYCoords[i] = linesSep * (i + 1); } // GET CORRESPONDING X COORDINATES FOR LINES linesYCoords.forEach((y) => { linesXCoords.push(Math.sqrt(radius**2 - (radius * Math.cos(angleRad / 2) + y)**2)); }); return ({x: linesXCoords, y: linesYCoords}); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.