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

Java examples for java.lang:Math Geometry Distance

Description

Measures the 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 {
    /**/*ww  w.  j  a  v a  2  s .com*/
     * Measures the distance of a point to a line segment. If you just want to
     * check if a point lies beneath a line, use
     * {@link #distPointLineSqr(Point2D, Point2D, Point2D)} instead and check
     * against the square of the maximum distance.
     * 
     * @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 distance.
     */
    public static double distPointLine(final Point2D start,
            final Point2D end, final Point2D p) {
        return Math.sqrt(distPointLineSqr(start, end, p));
    }

    /**
     * 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