该方法用于计算一个给定点到一个线段上最近点在该线段上的位置参数,范围为0到1。
LineSegment.prototype.closestPointToPointParameter(point, clampToLine)
point
: {Vector3} 一个 THREE.Vector3 实例,表示目标点的位置。clampToLine
: {boolean} 可选参数,表示计算的最近点是否被限制在线段范围内。默认值为 true
。from
到 to
点之间的相对位置。const from = new THREE.Vector3(-10, 0, 0);
const to = new THREE.Vector3(10, 0, 0);
const segment = new THREE.LineSegment(from, to);
const pointA = new THREE.Vector3(5, 5, 0);
const parameterA = segment.closestPointToPointParameter(pointA); // 0.5
const pointB = new THREE.Vector3(-20, 0, 0);
const parameterB = segment.closestPointToPointParameter(pointB); // 0
const pointC = new THREE.Vector3(0, 10, 0);
const parameterC = segment.closestPointToPointParameter(pointC); // 1
const pointD = new THREE.Vector3(-20, -20, 0);
const parameterD = segment.closestPointToPointParameter(pointD, false); // -1
clampToLine
为 false
,则计算的最近点可能位于线段范围之外,此时位置参数可能小于0或大于1。