Measures the squared distance of a point to a line segment. - Java java.lang

Java examples for java.lang:Math Geometry Distance

Description

Measures the squared distance of a point to a line segment.

Demo Code


//package com.java2s;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

public class Main {
    /**/*  w  w  w.j  ava2  s .  c  om*/
     * Measures the squared distance of a point to a line segment.
     * 
     * @param start The start point of the line segment.
     * @param end The end point of the line segment.
     * @param p The point to calculate the distance for.
     * @return The squared distance.
     */
    public static double distPointLineSqr(final Point2D start,
            final Point2D end, final Point2D p) {
        final Line2D line = new Line2D.Double(start, end);
        return line.ptSegDistSq(p);
    }
}

Related Tutorials