Java Distance between Point and Line distanceFromLine(int xa, int ya, int xb, int yb, int xc, int yc)

Here you can find the source of distanceFromLine(int xa, int ya, int xb, int yb, int xc, int yc)

Description

distance From Line

License

Open Source License

Declaration

public static double distanceFromLine(int xa, int ya, int xb, int yb, int xc, int yc) 

Method Source Code

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

public class Main {
    public static double distanceFromLine(int xa, int ya, int xb, int yb, int xc, int yc) {
        int xdiff = xb - xa;
        int ydiff = yb - ya;
        long l2 = (long) (xdiff * xdiff + ydiff * ydiff);
        if (l2 == 0L) {
            return (double) length(xa, ya, xc, yc);
        } else {//from  w  w w  . jav  a  2s . c o m
            double rnum = (double) ((ya - yc) * (ya - yb) - (xa - xc) * (xb - xa));
            double r = rnum / (double) l2;
            if (r >= 0.0D && r <= 1.0D) {
                double xi = (double) xa + r * (double) xdiff;
                double yi = (double) ya + r * (double) ydiff;
                double xd = (double) xc - xi;
                double yd = (double) yc - yi;
                return Math.sqrt(xd * xd + yd * yd);
            } else {
                return 1.7976931348623157E308D;
            }
        }
    }

    public static long length(int x1, int y1, int x2, int y2) {
        return (long) Math.sqrt((double) length2(x1, y1, x2, y2));
    }

    public static double length(double x1, double y1, double x2, double y2) {
        return Math.sqrt(length2(x1, y1, x2, y2));
    }

    public static double length(java.awt.geom.Point2D.Double p1, java.awt.geom.Point2D.Double p2) {
        return Math.sqrt(length2(p1.x, p1.y, p2.x, p2.y));
    }

    public static long length2(int x1, int y1, int x2, int y2) {
        return (long) ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
    }

    public static double length2(double x1, double y1, double x2, double y2) {
        return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
    }
}

Related

  1. distancePointToBot(Point2D.Double sourceLocation, Point2D.Double botLocation)
  2. distancePointToLine(final Point2D point, final Line2D line)
  3. DistanceToLine(double x, double y, double x1, double y1, double x2, double y2)
  4. distanceToLine(Point2D p, Point2D endA, Point2D endZ)