Example usage for org.apache.commons.math3.geometry.euclidean.threed Vector3D distanceSq

List of usage examples for org.apache.commons.math3.geometry.euclidean.threed Vector3D distanceSq

Introduction

In this page you can find the example usage for org.apache.commons.math3.geometry.euclidean.threed Vector3D distanceSq.

Prototype

public static double distanceSq(Vector3D v1, Vector3D v2) 

Source Link

Document

Compute the square of the distance between two vectors.

Usage

From source file:org.esa.s2tbx.dataio.s2.l1b.CoordinateUtils.java

public static double distanceToSegment(Vector3D v, Vector3D w, Vector3D p) {
    // Return minimum distance between line segment vw and point p
    final double l2 = Vector3D.distanceSq(v, w); // i.e. |w-v|^2 -  avoid a sqrt
    if (l2 == 0.0)
        return Vector3D.distance(p, v); // v == w case
    // Consider the line extending the segment, parameterized as v + t (w - v).
    // We find projection of point p onto the line.
    // It falls where t = [(p-v) . (w-v)] / |w-v|^2
    double t = Vector3D.dotProduct(p.subtract(v), w.subtract(v)) / l2;
    if (t < 0.0)
        return Vector3D.distance(p, v); // Beyond the 'v' end of the segment
    else if (t > 1.0)
        return Vector3D.distance(p, w); // Beyond the 'w' end of the segment
    Vector3D projection = v.add(w.subtract(v).scalarMultiply(t)); // Projection falls on the segment
    return Vector3D.distance(p, projection);
}