Here you can find the source of distanceToLine3(Line2D l, Point2D p)
public static double distanceToLine3(Line2D l, Point2D p)
//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); } }