Java Distance between Point and Line distancePointToLine(final Point2D point, final Line2D line)

Here you can find the source of distancePointToLine(final Point2D point, final Line2D line)

Description

Given a line based on two points, and a point away from the line, find the perpendicular distance from the point to the line.

License

Open Source License

Parameter

Parameter Description
point a parameter
line a parameter

Declaration

public static double distancePointToLine(final Point2D point, final Line2D line) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

public class Main {
    /**// www  .j av  a2  s  .  c om
     * Given a line based on two points, and a point away from the line, find
     * the perpendicular distance from the point to the line. see
     * http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html for
     * explanation and defination. Source:
     *
     * @see <a href="http
     *      ://www.java2s.com/Code/CSharp/Development-Class/DistanceFromPointToLine
     *      .htm">DistanceFromPointToLine</a>
     * @param point
     * @param line
     * @return
     */
    public static double distancePointToLine(final Point2D point, final Line2D line) {
        final Point2D l1 = line.getP1();
        final Point2D l2 = line.getP2();
        return Math
                .abs((l2.getX() - l1.getX()) * (l1.getY() - point.getY())
                        - (l1.getX() - point.getX()) * (l2.getY() - l1.getY()))
                / Math.sqrt(Math.pow(l2.getX() - l1.getX(), 2) + Math.pow(l2.getY() - l1.getY(), 2));
    }
}

Related

  1. distanceFromLine(int xa, int ya, int xb, int yb, int xc, int yc)
  2. distancePointToBot(Point2D.Double sourceLocation, Point2D.Double botLocation)
  3. DistanceToLine(double x, double y, double x1, double y1, double x2, double y2)
  4. distanceToLine(Point2D p, Point2D endA, Point2D endZ)
  5. distanceToLine2(Line2D l, Point2D p)
  6. distanceToLine3(Line2D l, Point2D p)