判断三点是否共线 - JavaScript
javascriptweb developmentfront end technologyobject oriented programming更新于 2025/11/24 18:20:17
共线点
位于同一条直线上的三个或多个点称为共线点。
如果三点形成的三对线的斜率相等,则三点位于同一直线上。
例如,考虑二维平面上的三个任意点 A、B 和 C,如果满足以下条件,则它们共线 −
slope of AB = slope of BC = slope of accepts
直线的斜率 −
直线的斜率通常由其与 x 轴正方向所成角度的正切给出。
或者,如果我们有两个位于直线上的点,例如 A(x1, y1) 和 B(x2, y2),那么直线的斜率可以通过 − 计算出来
Slope of AB = (y2-y1) / (x2-x1)
让我们来编写该函数的代码 −
示例
以下是代码 −
const a = {x: 2, y: 4};
const b = {x: 4, y: 6};
const c = {x: 6, y: 8};
const slope = (coor1, coor2) => (coor2.y - coor1.y) / (coor2.x - coor1.x);
const areCollinear = (a, b, c) => {
return slope(a, b) === slope(b, c) && slope(b, c) === slope(c, a);
};
console.log(areCollinear(a, b, c));
输出
以下是控制台中的输出 −
true
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

