js外公切线切点的计算

image.png

方法一

令 圆心之间的距离 d = C1C2
∴ ∠P1C1E = arccos((P1C1 - P3C2) / C1C2 ) = arccos( (r1 -r2 ) / d)
计算 C2C1 与水平线夹角,然后加上∠P1C1E就是P1的角度

parti.gif

let canvas =  document.getElementById("target");
   canvas.width = document.body.clientWidth;
   canvas.height = document.body.clientHeight;
   let ctx = canvas.getContext("2d");
   let isDrawing = false;
   //大圆圆心
   let pointC1 = {x:500,y:500};
   //大圆半径
   let radius1 = 250;

   //小圆圆心
   let pointC2 =  {x:800,y:300};
   //小圆半径
   let radius2 = 50;

   let eventC1 = false;



   canvas.addEventListener('mousedown', e => {
       let x = e.offsetX;
       let y = e.offsetY;
       isDrawing = true;
       if(Math.pow(x - pointC1.x,2) + Math.pow(y - pointC1.y,2) <= radius1*radius1){
           //移动大圆
           eventC1 = true;
           pointC1.x = x;
           pointC1.y = y;
       }else{
           pointC2.x = x;
           pointC2.y = y;
       }
       update();
   });

   canvas.addEventListener('mousemove', e => {
       if (isDrawing === true) {
           let x = e.offsetX;
           let y = e.offsetY;
           if(eventC1){
               pointC1.x = x;
               pointC1.y = y;
           }else{
               pointC2.x = x;
               pointC2.y = y;
           }
           update();
       }
   });

   canvas.addEventListener('mouseup', e => {
       if (isDrawing === true) {
           isDrawing = false;
           eventC1 = false;
       }
   });


   /**
    * @param c1 大圆
    * @param c2 小圆
    * @param r1 大圆半径
    * @param r2 小圆半径
    * */
   function calcQieDian(c1,c2,r1,r2) {
       //圆心之间的距离
       let d = Math.sqrt(Math.pow(c1.x - c2.x,2) + Math.pow(c1.y - c2.y,2));
       //外公切线与连心线夹角的角度
       let theta = Math.acos((r1 - r2)/d);
       let vc1c2 = {x:c2.x - c1.x,y:-c2.y + c1.y}; //屏幕坐标系与笛卡尔坐标系是y轴是反着的
       let radC1C2 = Math.acos(vc1c2.x / Math.sqrt(Math.pow(vc1c2.x,2) + Math.pow(vc1c2.y,2)));

       if(c2.y <= c1.y){
           //大圆上的切点
           let p1 = {x:c1.x + Math.cos(theta + radC1C2) * r1,y:c1.y - Math.sin(theta + radC1C2)* r1};
           let p2 = {x:c1.x + Math.cos(theta - radC1C2) * r1,y:c1.y + Math.sin(theta - radC1C2)* r1};
           //小圆上的切点
           let p3 = {x:c2.x + Math.cos(theta + radC1C2) * r2,y:c2.y - Math.sin(theta + radC1C2)* r2};
           let p4 = {x:c2.x + Math.cos(theta - radC1C2) * r2,y:c2.y + Math.sin(theta - radC1C2)* r2};
           return {p1:p1,p2:p2,p3:p3,p4:p4};
       }else{
           radC1C2 = Math.PI - radC1C2;
           //大圆上的切点
           let p1 = {x:c1.x + Math.cos(Math.PI - theta - radC1C2) * r1,y:c1.y + Math.sin(Math.PI - theta - radC1C2)* r1};
           let p2 = {x:c1.x + Math.cos(Math.PI - (theta - radC1C2)) * r1,y:c1.y - Math.sin(Math.PI - (theta - radC1C2))* r1};
           //小圆上的切点
           let p3 = {x:c2.x + Math.cos(Math.PI - theta - radC1C2) * r2,y:c2.y + Math.sin(Math.PI - theta - radC1C2)* r2};
           let p4 = {x:c2.x + Math.cos(Math.PI - (theta - radC1C2)) * r2,y:c2.y - Math.sin(Math.PI - (theta - radC1C2))* r2};
           return {p1:p1,p2:p2,p3:p3,p4:p4};
       }
   }


   function update() {
       ctx.clearRect(0,0,canvas.width,canvas.height);

       //大圆的圆心
       drawPoint(pointC1.x,pointC1.y,"#097734");
       //大圆
       drawCircle(pointC1.x,pointC1.y,radius1,"#097734");

       //大圆的圆心
       drawPoint(pointC2.x,pointC2.y,"#0a6292");
       //大圆
       drawCircle(pointC2.x,pointC2.y,radius2,"#0a6292");


       let p = calcQieDian(pointC1,pointC2,radius1,radius2);

       drawText("C1",pointC1.x,pointC1.y);
       drawText("C2",pointC2.x,pointC2.y);
       drawText("p1",p.p1.x,p.p1.y);
       drawText("p2",p.p2.x,p.p2.y);
       drawText("p3",p.p3.x,p.p3.y);
       drawText("p4",p.p4.x,p.p4.y);
       drawPoint(p.p1.x,p.p1.y,"#097734");
       drawPoint(p.p2.x,p.p2.y,"#097734");
       drawPoint(p.p3.x,p.p3.y,"#097734");
       drawPoint(p.p4.x,p.p4.y,"#097734");

       drawLine(pointC1,p.p1);
       drawLine(pointC1,p.p2);
       drawLine(pointC2,p.p3);
       drawLine(pointC2,p.p4);
       drawLine(p.p1,p.p2);
       drawLine(p.p3,p.p4);

       drawLine(p.p1,p.p3);
       drawLine(p.p2,p.p4);
       drawLine(pointC1,pointC2);
   }


   function drawText(text,x,y) {
       ctx.beginPath();
       ctx.font="40px Arial";
       ctx.fillText(text,x,y);
       ctx.stroke();
   }

   function drawCircle(x,y,radius,color){
       ctx.beginPath();
       ctx.fillStyle=color;
       ctx.arc(x,y,radius,0,2*Math.PI,true);
       ctx.stroke();
   }

   function drawPoint(x,y,color){
       ctx.beginPath();
       ctx.fillStyle=color;
       ctx.arc(x,y,5,0,2*Math.PI,true);
       ctx.fill();
   }

   function drawLine(pointStart,pointEnd) {
       ctx.beginPath();
       ctx.moveTo(pointStart.x,pointStart.y);
       ctx.lineTo(pointEnd.x,pointEnd.y);
       ctx.stroke();
   }
   update();

方法二

利用atan2计算,先计算二个圆心之间的距离,利用atan2计算角度,剩下的就是求角度了,这种方法也是最简单的一种
atan2

let d = Math.sqrt(Math.pow(pointC1.x - pointC2.x,2) + Math.pow(pointC1.y - pointC2.y,2));
const angleBetweenCenters = angle(pointC2,pointC1);
const spread = Math.acos((radius1 - radius2) / d);
const angle1 = angleBetweenCenters + spread;
const angle2 = angleBetweenCenters - spread;
const angle3 = angleBetweenCenters + (Math.PI - (Math.PI - spread));
const angle4 = angleBetweenCenters - (Math.PI - (Math.PI - spread));
const p1 = getVector(pointC1.x,pointC1.y, angle1, radius1);
const p2 = getVector(pointC1.x,pointC1.y, angle2, radius1);
const p3 = getVector(pointC2.x,pointC2.y, angle3, radius2);
const p4 = getVector(pointC2.x,pointC2.y, angle4, radius2);
let canvas =  document.getElementById("target");
    canvas.width = document.body.clientWidth;
    canvas.height = document.body.clientHeight;
    let ctx = canvas.getContext("2d");
    let isDrawing = false;
    //大圆圆心
    let pointC1 = {x:500,y:500};
    //大圆半径
    let radius1 = 250;

    //小圆圆心
    let pointC2 =  {x:800,y:300};
    //小圆半径
    let radius2 = 50;

    let eventC1 = false;



    canvas.addEventListener('mousedown', e => {
        let x = e.offsetX;
        let y = e.offsetY;
        isDrawing = true;
        if(Math.pow(x - pointC1.x,2) + Math.pow(y - pointC1.y,2) <= radius1*radius1){
            //移动大圆
            eventC1 = true;
            pointC1.x = x;
            pointC1.y = y;
        }else{
            pointC2.x = x;
            pointC2.y = y;
        }
        update();
    });

    canvas.addEventListener('mousemove', e => {
        if (isDrawing === true) {
            let x = e.offsetX;
            let y = e.offsetY;
            if(eventC1){
                pointC1.x = x;
                pointC1.y = y;
            }else{
                pointC2.x = x;
                pointC2.y = y;
            }
            update();
        }
    });

    canvas.addEventListener('mouseup', e => {
        if (isDrawing === true) {
            isDrawing = false;
            eventC1 = false;
        }
    });

    function angle(c1,c2) {
        return Math.atan2(c1.y - c2.y, c1.x - c2.x);
    }

    function getVector(cx, cy, a, r) {
        return {x:cx + r * Math.cos(a), y:cy + r * Math.sin(a)};
    }

    function update() {
        ctx.clearRect(0,0,canvas.width,canvas.height);

        //大圆的圆心
        drawPoint(pointC1.x,pointC1.y,"#097734");
        //大圆
        drawCircle(pointC1.x,pointC1.y,radius1,"#097734");

        //大圆的圆心
        drawPoint(pointC2.x,pointC2.y,"#0a6292");
        //大圆
        drawCircle(pointC2.x,pointC2.y,radius2,"#0a6292");

        let d = Math.sqrt(Math.pow(pointC1.x - pointC2.x,2) + Math.pow(pointC1.y - pointC2.y,2));
        const angleBetweenCenters = angle(pointC2,pointC1);
        const spread = Math.acos((radius1 - radius2) / d);
        const angle1 = angleBetweenCenters + spread;
        const angle2 = angleBetweenCenters - spread;
        const angle3 = angleBetweenCenters + (Math.PI - (Math.PI - spread));
        const angle4 = angleBetweenCenters - (Math.PI - (Math.PI - spread));
        const p1 = getVector(pointC1.x,pointC1.y, angle1, radius1);
        const p2 = getVector(pointC1.x,pointC1.y, angle2, radius1);
        const p3 = getVector(pointC2.x,pointC2.y, angle3, radius2);
        const p4 = getVector(pointC2.x,pointC2.y, angle4, radius2);

        drawText("C1",pointC1.x,pointC1.y);
        drawText("C2",pointC2.x,pointC2.y);

        drawText("p1",p1.x,p1.y);
        drawText("p2",p2.x,p2.y);
        drawText("p3",p3.x,p3.y);
        drawText("p4",p4.x,p4.y);

        drawPoint(p1.x,p1.y,"#097734");
        drawPoint(p2.x,p2.y,"#097734");
        drawPoint(p3.x,p3.y,"#097734");
        drawPoint(p4.x,p4.y,"#097734");

        drawLine(pointC1,p1);
        drawLine(pointC1,p2);
        drawLine(pointC2,p3);
        drawLine(pointC2,p4);
        drawLine(p1,p2);
        drawLine(p3,p4);

        drawLine(p1,p3);
        drawLine(p2,p4);
        drawLine(pointC1,pointC2);
    }


    function drawText(text,x,y) {
        ctx.beginPath();
        ctx.font="30px Arial";
        ctx.fillText(text,x,y);
        ctx.stroke();
    }

    function drawCircle(x,y,radius,color){
        ctx.beginPath();
        ctx.fillStyle=color;
        ctx.arc(x,y,radius,0,2*Math.PI,true);
        ctx.stroke();
    }

    function drawPoint(x,y,color){
        ctx.beginPath();
        ctx.fillStyle=color;
        ctx.arc(x,y,5,0,2*Math.PI,true);
        ctx.fill();
    }

    function drawLine(pointStart,pointEnd) {
        ctx.beginPath();
        ctx.moveTo(pointStart.x,pointStart.y);
        ctx.lineTo(pointEnd.x,pointEnd.y);
        ctx.stroke();
    }
    update();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现两圆外公切线交点计算,可以按照以下步骤进行: 1. 计算两圆心之间的距离,如果两圆相离或相切,则不存在外公切线; 2. 计算两圆心的连线与两圆的切点,得到切线的斜率; 3. 根据切线斜率和两圆心的位置,求出切线的方程; 4. 求出两圆心到切点的距离,即切线的截距; 5. 求解切线方程组,得到两条外公切线的交点。 以下是使用 C++ 实现两圆外公切线交点计算的代码示例: ```c++ #include <iostream> #include <cmath> using namespace std; struct Point { double x, y; }; struct Circle { Point center; double radius; }; // 计算两点之间的距离 double distance(Point p1, Point p2) { return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2)); } // 计算两圆的公切线的交点 void calcOuterTangent(Circle c1, Circle c2) { // 计算两圆心之间的距离 double d = distance(c1.center, c2.center); // 如果两圆相离或相切,则不存在外公切线 if (d > c1.radius + c2.radius || d < abs(c1.radius - c2.radius)) { cout << "No outer tangent." << endl; return; } // 计算两圆心的连线与两圆的切点,得到切线的斜率 double angle = atan2(c2.center.y - c1.center.y, c2.center.x - c1.center.x); double sinAngle = sin(angle); double cosAngle = cos(angle); // 根据切线斜率和两圆心的位置,求出切线的方程 double m1 = c1.radius * sinAngle - c2.radius * sinAngle; double n1 = c1.radius * cosAngle - c2.radius * cosAngle; double m2 = c1.radius * sinAngle + c2.radius * sinAngle; double n2 = c1.radius * cosAngle + c2.radius * cosAngle; // 求出两圆心到切点的距离,即切线的截距 double b1 = c1.center.y - m1 / n1 * c1.center.x; double b2 = c2.center.y - m2 / n2 * c2.center.x; // 求解切线方程组,得到两条外公切线的交点 double x = (b2 - b1) / (m1 / n1 - m2 / n2); double y = m1 / n1 * x + b1; cout << "The intersection point of outer tangents is: (" << x << ", " << y << ")." << endl; } int main() { Circle c1 = {{0, 0}, 2}; Circle c2 = {{5, 0}, 3}; calcOuterTangent(c1, c2); return 0; } ``` 上述代码中,我们定义了两个结构体 Point 和 Circle,分别表示点和圆。distance 函数用于计算两点之间的距离。calcOuterTangent 函数用于计算两圆的外公切线的交点。在主函数中,我们定义了两个圆,并调用 calcOuterTangent 函数计算它们的外公切线的交点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值