根据两点坐标和半径返回圆点坐标

GetCircleCenter(x1,y1,x2,y2,r){
        let c1 = (x2 * x2 - x1 * x1 + y2 * y2 - y1 * y1) / (2 * (x2 - x1));
        let c2 = (y2 - y1) / (x2 - x1);

        let A = c2 * c2 + 1;
        let B = (2 * x1 * c2 - 2 * c1 * c2 - 2 * y1);
        let C = x1 * x1 - 2 * x1 * c1 + c1 * c1 + y1 * y1 - r * r;
        //圆心坐标1
        let r_y = (-B + Math.sqrt(B * B - 4 * A * C)) / 2 / A;
        let r_x = c1 - c2 * r_y;
        //圆心坐标2
        // let r_y = (-B - Math.sqrt(B * B - 4 * A * C)) / 2 / A;
        // let r_x = c1 - c2 * r_y;

        return [r_x, r_y]
},

 

//根据圆心半径和两点坐标绘制圆弧
// 已知圆心坐标和半径
const centerX = 36;
const centerY = 493;
const x1=100;
const y1=200;
const x2=300;
const y2=350; const radius
= 300; // 计算向量差 const dx1 = x1- centerX; const dy1 = y1- centerY; const dx2 = x2- centerX; const dy2 = y2- centerY; // 计算起始角度和结束角度 const sAngle = Math.atan2(dy1, dx1); const eAngle = Math.atan2(dy2, dx2); // 绘制圆弧 context.arc(centerX, centerY, radius, sAngle, eAngle); // 将圆弧描边绘制出来 context.stroke();

 

posted @ 2024-01-10 10:17  旸神逆神旸  阅读(32)  评论(0)    收藏  举报