本文介绍了通过点的平行线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Line(P1, P2)和一个点NP(x,y),我想要的是通过NP创建一个平行的Line(A,B)我需要JavaScript中的一个函数来获取Line(A,B)

I have a Line(P1, P2) and a point NP(x,y), what I want is to create a parallel Line(A,B) pass through NPI need a function in JavaScript for getting Line (A, B)

已解决:我在@MBo的帮助下用JavaScript代码编写了函数

Solved: I write the function in JavaScript code with help of @MBo

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}}
}

推荐答案

找到 NP 的投影到 P1P2 线上.

首先获得向量

Dir = (P2.X-P1.X, P2.Y-P1.Y)
W =   (NP.X-P1.X, NP.Y-P1.Y)

使用标量(点)预测值将 NP 投影到 P1P2 上的点是:

Point of projection of NP onto P1P2 using scalar(dot) prodict is:

PP = P1 + Dir * (Dir.dot.W) / (Dir.dot.Dir)

和投影向量

V = NP - PP

现在并行段的末端是

P1' = P1 + V
P2' = P2 + V

这篇关于通过点的平行线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 01:21