我有一个Line(P1, P2)
和一个点NP(x,y)
,我想要的是通过Line(A,B)
创建平行的NP
我需要JavaScript中的一个函数来获取Line(A,B)
已解决:我在@MBo的帮助下用JavaScript代码编写了函数
function parallel(pt1, pt2, newPoint) {
let NP = new THREE.Vector2(newPoint.x, newPoint.y)
let p1 = new THREE.Vector2(pt1.x, pt1.y)
let p2 = new THREE.Vector2(pt2.x, pt2.y)
let dir = new THREE.Vector2(p2.x-p1.x, p2.y-p1.y);
let W = new THREE.Vector2(NP.x-p1.x, NP.y-p1.y);
let dw = dir.clone().dot(W);
let dd = dir.clone().dot(dir);
let dmw = dir.clone().multiplyScalar(dw).divideScalar(dd);
let PP = p1.clone().add(dmw)
let V = NP.clone().sub(PP)
let pi1 = p1.add(V)
let pi2 = p2.add(V)
return {A:{x:pi1.x,y:pi1.y}, B:{x:pi2.x,y:pi2.y}}
}
答案 0 :(得分:1)
找到 NP 的投影到 P1P2 线上。
首先获得向量
Dir = (P2.X-P1.X, P2.Y-P1.Y)
W = (NP.X-P1.X, NP.Y-P1.Y)
使用标量(点)预测法将 NP 投影到 P1P2 的点是:
PP = P1 + Dir * (Dir.dot.W) / (Dir.dot.Dir)
和投影向量
V = NP - PP
现在并行段的末端是
P1' = P1 + V
P2' = P2 + V
答案 1 :(得分:0)
先让P1=[p1x,p1y]
,P2=[p2x,p2y]
,NP=[nx,ny]
然后
W = [wx,wy] = (P2-P1)/2 = [(p2x-p1x)/2, (p2y-p1y)/2]
和
A = NP-W = [nx-wx, ny-wy]
B = NP+W = [nx+wx, ny+wy]
JS代码
function parallel([p1x,p1y],[p2x,p2y],[nx,ny]) {
let wx = (p2x-p1x)/2;
let wy = (p2y-p1y)/2;
return [[nx-wx,ny-wy],[nx+wx,ny+wy]]
}
// TEST
let P1 = [2,3];
let P2 = [6,5];
let NP = [5,2];
let [A,B] = parallel(P1,P2,NP);
// print
console.log(`for P1=[${P1}], P2=[${P2}], NP=[${NP}]
result is A=[${A}], B=[${B}]`);