Java Distance between Point and Line DistanceToLine(double x, double y, double x1, double y1, double x2, double y2)

Here you can find the source of DistanceToLine(double x, double y, double x1, double y1, double x2, double y2)

Description

Distance To Line

License

BSD License

Declaration

public static double DistanceToLine(double x, double y, double x1,
            double y1, double x2, double y2) 

Method Source Code

//package com.java2s;
// LICENSE:       This file is distributed under the BSD license.

import java.awt.Point;
import java.awt.geom.Point2D;

public class Main {
    public static double DistanceToLine(double x, double y, double x1,
            double y1, double x2, double y2) {
        return Math.sqrt(DistanceSquareToLine(x, y, x1, y1, x2, y2));
    }// w  w w.ja v  a  2s .co m

    public static double DistanceToLine(Point pt, Point2D.Float LinePt1,
            Point2D.Float LinePt2) {
        return DistanceToLine(pt.x, pt.y, LinePt1.x, LinePt1.y, LinePt2.x,
                LinePt2.y);
    }

    public static double DistanceSquareToLine(double x, double y,
            double x1, double y1, double x2, double y2) {
        double d1 = x1 - x2;
        double d2 = y1 - y2;
        double d = d1 * d1 + d2 * d2;
        if (d <= 0)
            return 0;
        double n = x * y1 - x1 * y + x1 * y2 - x2 * y1 + x2 * y - x * y2;
        return n * n / d;
    }

    /**
     *
     * @param pt
     * @param LinePt1
     * @param LinePt2
     * @return
     */
    public static double DistanceSquareToLine(Point pt,
            Point2D.Float LinePt1, Point2D.Float LinePt2) {
        return DistanceSquareToLine(pt.x, pt.y, LinePt1.x, LinePt1.y,
                LinePt2.x, LinePt2.y);
    }
}

Related

  1. distanceFromLine(int xa, int ya, int xb, int yb, int xc, int yc)
  2. distancePointToBot(Point2D.Double sourceLocation, Point2D.Double botLocation)
  3. distancePointToLine(final Point2D point, final Line2D line)
  4. distanceToLine(Point2D p, Point2D endA, Point2D endZ)
  5. distanceToLine2(Line2D l, Point2D p)
  6. distanceToLine3(Line2D l, Point2D p)
  7. getDistance(double aX, double aY, double bX, double bY)