Here you can find the source of DistanceToLine(double x, double y, double x1, double y1, double x2, double y2)
public static double DistanceToLine(double x, double y, double x1, double y1, double x2, double y2)
//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); } }