如何在javascript中找到两条切线的交点

How to find intersection of two tangent lines in javascript

本文关键字:两条 javascript      更新时间:2023-09-26

我在圆上有两个点(x1,y1和x2,y2),圆的中心(c1,c2)并且需要javascript代码来计算通过点x1、y1和x2、y2的两条切线的交点。

我用它将圆(实际上是由上述点定义的弧)转换为二次贝塞尔曲线。

切线的法线为:

n1x = x1 - c1
n1y = y1 - c2
n2x = x2 - c1
n2y = y2 - c2

使用以下参数:

d1 = n1x * x1 + n1y * y1
d2 = n2x * x2 + n2y * y2

切线的方程可以写成:

x * n1x + y * n1y = d1
x * n2x + y * n2y = d2

在一般情况下,求解线性方程组会得到以下结果:

x = (d2 * n1y - d1 * n2y) / (n1y * n2x - n1x * n2y)
y = (d1 * n2x - d2 * n1x) / (n1y * n2x - n1x * n2y)

在javascript:中

var x1,y1,x2,y2,c1,c2; // inputs
var x, y;              // outputs
... get the parameters somehow
var n1x = x1 - c1;
var n1y = y1 - c2;
var n2x = x2 - c1;
var n2y = y2 - c2;
var d1 = n1x * x1 + n1y * y1;
var d2 = n2x * x2 + n2y * y2;
var det = n1y * n2x - n1x * n2y;
if (det === 0) {
    // The lines are parallel
} else {
    x = (d2 * n1y - d1 * n2y) / det;
    y = (d1 * n2x - d2 * n1x) / det;
}

圆周上从中心(c₁, c₂)到点(xᵢ, yᵢ)的矢量为(xᵢ-c₁, yᵢ-c₂)

这意味着通过(c₁, c₂)(xᵢ, yᵢ)的线具有斜率sᵢ = (yᵢ-c₂)/(xᵢ-c₁)

tᵢ(xᵢ, yᵢ)上切线的斜率。tᵢ = -1/sᵢ = (c₁-xᵢ)/(yᵢ-c₂)

设CCD_ 10。则通过(xᵢ, yᵢ)的切线为

y = tᵢ(x-xᵢ) + yᵢ = tᵢx + oᵢ

对CCD_ 12这样做产生了一个线性方程组。

y = t₁x + o₁
y = t₂x + o₂

解决它给出切线的交点

    o₁-o₂
x = ─────
    t₂-t₁
       o₁-o₂
y = t₁ ───── + o₁
       t₂-t₁