Line3.distance()
The Line3.distance()
method is a function provided by the three.js
library. This function is used to calculate the shortest distance between a given point and a given line segment that extends between two other points.
distance(point: Vector3, optionalPointOnLine: Vector3, optionalVector: Vector3): number
point
: The Vector3
object representing the point for which to calculate the distance. This is required.
optionalPointOnLine
: An optional Vector3
object representing a point on the line segment. If this is not provided, it will default to the origin (i.e. Vector3(0,0,0)
).
optionalVector
: An optional Vector3
object representing the vector that defines the line segment. If this is not provided, it will default to a vector in the positive x
direction (i.e. Vector3(1,0,0)
).
This method returns a number
representing the shortest distance between the given point and the line segment defined by the given points and vector.
The Line3.distance()
method is commonly used in 3D graphics applications to determine the distance between a point and a line segment. For example, this can be used to detect collisions between objects or to perform proximity testing.
To use this method, you'll need to create three Vector3
objects: one representing the point for which to calculate the distance, and two others defining the line segment. You can then call the Line3.distance()
method on the line segment object, passing in the point as the first argument:
const point = new THREE.Vector3(3, 2, 1);
const pointA = new THREE.Vector3(1, 0, 0);
const pointB = new THREE.Vector3(0, 1, 0);
const line = new THREE.Line3(pointA, pointB);
const distance = line.distance(point);
console.log(distance);
In this example, we create a point at coordinates (3, 2, 1)
and define a line segment that extends from (1, 0, 0)
to (0, 1, 0)
. We then create a Line3
object based on these two points and call distance()
on this object, passing in our point
object as the only argument. This will return the shortest distance between the given point and the line segment, which we log to the console.
The Line3.distance()
method is a powerful tool for 3D graphics programmers that can be used to perform proximity testing and detect collisions. By using this method with Vector3
objects, you can easily determine the shortest distance between a given point and a line segment defined by other points and vectors.