Box2.distanceToPoint() 方法用于计算一个 Vector2 在 Box2 中最近点的距离。
distanceToPoint( point: Vector2 ): Number
point : Vector2类型表示要计算距离的点。Number类型表示点到 Box2 中最近点的距离。import { Box2, Vector2 } from 'three';
const box = new Box2(new Vector2(-10, -10), new Vector2(10, 10));
const point = new Vector2(5, 5);
const distance = box.distanceToPoint(point);
console.log(`The distance between (${point.x}, ${point.y}) and the box is ${distance}.`);
// The distance between (5, 5) and the box is 0.
Box2 的最小和最大坐标必须在适当的轴上进行设置,例如:
// 设置一个 "x" 和 "y" 轴的范围
const box = new Box2(new Vector2(-10, -10), new Vector2(10, 10));
// 设置一个 "y" 和 "z" 轴的范围
const box = new Box2(new Vector2(0, -10), new Vector2(0, 10));
如果 Box2 中的点与参数 point 重合,则返回值为 0。