Java Distance between Point and Line distanceToLine3(Line2D l, Point2D p)

Here you can find the source of distanceToLine3(Line2D l, Point2D p)

Description

distance To Line

License

Apache License

Declaration

public static double distanceToLine3(Line2D l, Point2D p)
    
    
    

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static double distanceToLine3(Line2D l, Point2D p)
    // Calculate the distance between the point (nX, nY) and the line through the
    // points (nP1X, nP1Y), (nP2X, nP2Y).
    {/*  w  w w.j a va2  s.c  o m*/
        double dDist = 0;

        if (l.getX1() == l.getX2())
            // Vertical line
            dDist = p.getX() - l.getX1();
        else if (l.getY1() == l.getY2())
            // Horizontal line
            dDist = p.getY() - l.getY1();
        else {
            // Figure out the slope and Y intercept of the line
            double dM1 = ((double) l.getY2() - l.getY1()) / ((double) l.getX2() - l.getX1());
            double dB1 = l.getY1() - (dM1 * l.getX1());
            // Figure out the slope and Y intercept of the perpendicular line
            // through the third point
            double dM2 = -(1 / dM1);
            double dB2 = p.getY() - (dM2 * p.getX());

            // Find the intersection of the two lines
            double dXInt, dYInt;
            dXInt = (dB2 - dB1) / (dM1 - dM2);
            dYInt = (dM2 * dXInt) + dB2;

            // Now calulate the distance between the point and the intesection of
            // the two lines.
            dDist = Math.sqrt(Math.pow(dXInt - p.getX(), 2) + Math.pow(dYInt - p.getY(), 2));
        }

        return Math.abs(dDist);
    }
}

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)
  5. distanceToLine2(Line2D l, Point2D p)
  6. getDistance(double aX, double aY, double bX, double bY)
  7. getDistance(Line2D aSegment, Point aPoint)
  8. getDistanceToCenter(Point2D p, Line2D line)