[英]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.