javascript - 如何求两条交叉线的夹 Angular

标签 javascript php logic

我有四点喜欢

P1A (10,9)
P1B (10,10)

P2A (11,10)
P2B (11,9)

这显示了

LineA(从P1A ~ P2A)LineB(从P1B ~ P2B)

现在,我想得到两条线的 Angular 。

在这种情况下, Angular 必须为 90 度。

如何通过编程获取 Angular ?

如果我能在 php 或 javascript 上得到这个想法,我很高兴

我认为向量 a,b 的公式一定是这样的。

cosθ = a1 x b1 + a2 x b2/MathSqrt(a1 x a1 + a2 x a2) x MathSqrt(b1 x b1 + b2 x b2)

但是我怎样才能将P1A,P1B,P2A,P2B代入这个公式?

最佳答案

在 JavaScript 中你可以使用 Math.atan2()

The Math.atan2() method returns a numeric value between -π and π representing the angle theta of an (x, y) point.

let P1A = {
    x: 10,
    y: 9
};

let P1B = {
    x: 11,
    y: 10
};

// angle in radians
let angleRadians = Math.atan2(P1B.y - P1A.y, P1B.x - P1A.x);

// angle in degrees
let angleDeg = Math.atan2(P1B.y - P1A.y, P1B.x - P1A.x) * 180 / Math.PI;

关于javascript - 如何求两条交叉线的夹 Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49425796/

相关文章:

Javascript:如何使变量 "private"仅用于当前递归层?

javascript - 如何在 JavaScript 中访问 Rails 路由/URL?

JavaScript 传递元素但接收窗口?

php - 棘手的问题 : How to order results from a multiple regexes

javascript - Node.js 代码保护 - 如何使 Node.js 部署仅在 V8 汇编程序中可恢复

php - 正则表达式匹配任何 UTF 字符,不包括标点符号

javascript - 使用纯JS(Ajax)将数据发送到PHP文件

algorithm - 节省工作时间的实用位操作

c# - 将 1,2,3,4,5,6,8,10,11 显示为 1-6,8,10-11

c++ - 如何在 C++ 中将 10 位映射到 6 位(尽可能高效)?