You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JavaScript中从任意角度的两点计算等边三角形的第三个点?

已知两点求等边三角形第三个顶点的计算方法

假设已知等边三角形的两个顶点为 A(x₁, y₁) 和 B(x₂, y₂),第三个顶点有两种可能(分别在AB线段的两侧),可以通过向量旋转的方法计算:

核心原理

等边三角形的两边长度相等,且夹角为60°。将向量AB分别顺时针、逆时针旋转60°,再结合起点A的坐标,就能得到两个可能的第三个顶点坐标。

计算公式

情况1:逆时针旋转60°(得到AB一侧的顶点C₁)

x₃ = x₁ + 0.5*(x₂ - x₁) - (√3/2)*(y₂ - y₁)
y₃ = y₁ + (√3/2)*(x₂ - x₁) + 0.5*(y₂ - y₁)

情况2:顺时针旋转60°(得到AB另一侧的顶点C₂)

x₃ = x₁ + 0.5*(x₂ - x₁) + (√3/2)*(y₂ - y₁)
y₃ = y₁ - (√3/2)*(x₂ - x₁) + 0.5*(y₂ - y₁)

代码示例(JavaScript)

// 定义已知两点
const pointA = { x: 2, y: 2 };
const pointB = { x: 5, y: 2 };
const sqrt3 = Math.sqrt(3);

// 计算两个可能的第三个顶点
const pointC1 = {
  x: pointA.x + 0.5 * (pointB.x - pointA.x) - (sqrt3 / 2) * (pointB.y - pointA.y),
  y: pointA.y + (sqrt3 / 2) * (pointB.x - pointA.x) + 0.5 * (pointB.y - pointA.y)
};

const pointC2 = {
  x: pointA.x + 0.5 * (pointB.x - pointA.x) + (sqrt3 / 2) * (pointB.y - pointA.y),
  y: pointA.y - (sqrt3 / 2) * (pointB.x - pointA.x) + 0.5 * (pointB.y - pointA.y)
};

console.log("逆时针方向顶点:", pointC1);
console.log("顺时针方向顶点:", pointC2);

适配你的需求

如果要给每条边都绘制三角形,只需要对每条边的两个端点,统一选择其中一个旋转方向(比如全选逆时针)计算第三个顶点,就能得到规则的外围三角形图形。

内容的提问来源于stack exchange,提问作者jcubic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 04:31:05