Skip to content

Commit ee790a3

Browse files
committed
Add distanceToPoint method to Ray3f class
- Introduced a new utility method `distanceToPoint(Vector3f point)` to the `Ray3f` class. - The method calculates the shortest distance from the ray to a given point in 3D space. - Handles points both in front and behind the ray's origin by returning either the perpendicular distance or the distance from the point to the origin if it lies behind the ray. - Ensured robust null-check for the input point and proper handling of edge cases (e.g., point behind ray's origin).
1 parent 25207c1 commit ee790a3

File tree

1 file changed

+57
-20
lines changed

1 file changed

+57
-20
lines changed

src/main/java/math/Ray3f.java

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,63 @@ public Ray3f(Vector3f origin, Vector3f direction) {
4242
this.directionInv = direction.reciprocal();
4343
}
4444

45+
/**
46+
* Computes the shortest distance from the ray to a point in 3D space.
47+
*
48+
* <p>This method calculates the perpendicular distance from the given point to the ray. If the
49+
* point lies behind the origin of the ray, the distance from the point to the ray's origin is
50+
* returned. If the point lies along the ray's path (in the direction of the ray), the
51+
* perpendicular distance is calculated. The ray is considered to extend infinitely in both
52+
* directions from the origin.
53+
*
54+
* @param point The point in 3D space to compute the distance to (non-null).
55+
* @return The shortest distance from the ray to the point. If the point is behind the ray's
56+
* origin, the distance from the point to the origin is returned.
57+
* @throws IllegalArgumentException if the point is null.
58+
*/
59+
public float distanceToPoint(Vector3f point) {
60+
if (point == null) {
61+
throw new IllegalArgumentException("Point cannot be null.");
62+
}
63+
64+
// Calculate vector from ray origin to the point
65+
Vector3f toPoint = point.subtract(origin);
66+
67+
// Project the vector to the ray's direction (dot product normalized)
68+
float projection = toPoint.dot(direction);
69+
70+
// If the projection is negative, the point is behind the ray's origin
71+
if (projection < 0) {
72+
// Return the distance from the origin to the point
73+
return toPoint.length();
74+
}
75+
76+
// Return the perpendicular distance (calculate as the distance from the point to the closest
77+
// point on the ray)
78+
Vector3f closestPoint = getPointAt(projection);
79+
return closestPoint.subtract(point).length();
80+
}
81+
82+
/**
83+
* Computes the point along the ray at a given parameter {@code t}.
84+
*
85+
* <p>The formula for the point is:
86+
*
87+
* <pre>{@code
88+
* point = origin + t * direction
89+
* }</pre>
90+
*
91+
* where {@code t} is a scalar representing the distance along the ray from the origin. Positive
92+
* values of {@code t} will give points in the direction the ray is pointing, while negative
93+
* values will give points in the opposite direction.
94+
*
95+
* @param t The parameter along the ray (can be negative, zero, or positive).
96+
* @return The point at parameter {@code t}.
97+
*/
98+
public Vector3f getPointAt(float t) {
99+
return origin.add(direction.mult(t));
100+
}
101+
45102
/**
46103
* Returns the origin of the ray.
47104
*
@@ -76,24 +133,4 @@ public Vector3f getDirection() {
76133
public Vector3f getDirectionInv() {
77134
return new Vector3f(directionInv);
78135
}
79-
80-
/**
81-
* Computes the point along the ray at a given parameter {@code t}.
82-
*
83-
* <p>The formula for the point is:
84-
*
85-
* <pre>{@code
86-
* point = origin + t * direction
87-
* }</pre>
88-
*
89-
* where {@code t} is a scalar representing the distance along the ray from the origin. Positive
90-
* values of {@code t} will give points in the direction the ray is pointing, while negative
91-
* values will give points in the opposite direction.
92-
*
93-
* @param t The parameter along the ray (can be negative, zero, or positive).
94-
* @return The point at parameter {@code t}.
95-
*/
96-
public Vector3f getPointAt(float t) {
97-
return origin.add(direction.mult(t));
98-
}
99136
}

0 commit comments

Comments
 (0)