向量点乘
向量点乘
概述
使用three.js画两条线,通过点乘来计算夹角
- 画两条线段
let v1 = new THREE.Vector3(0, 0, 0);
let v2 = new THREE.Vector3(10, 2, 0);
let v3 = new THREE.Vector3(0, 10, 0);
let geometry1 = new THREE.Geometry();
geometry1.vertices.push(v1, v2);
let material1 = new THREE.LineBasicMaterial({
color: 0x0000ff
});
let line1 = new THREE.Line(geometry1, material1);
scene.add(line1);
let geometry2 = new THREE.Geometry();
geometry2.vertices.push(v1, v3);
let material2 = new THREE.LineBasicMaterial({
color: 0xff0000
});
let line2 = new THREE.Line(geometry2, material2);
scene.add(line2);
效果:
2. 使用点乘来计算夹角余弦值和角度。
// 返回三角形顶点p1对应夹角余弦值
let dot = v2.clone().normalize().dot(v3.clone().normalize());
console.log(dot); // 结果为0
console.log(Math.acos(dot)*180/Math.PI); // 结果为90