Here you can find the source of DistanceFromLine(float cx, float cy, float ax, float ay, float bx, float by)
public static Float DistanceFromLine(float cx, float cy, float ax, float ay, float bx, float by)
//package com.java2s; //License from project: Apache License public class Main { public static Float DistanceFromLine(float cx, float cy, float ax, float ay, float bx, float by) { float diffbax = bx - ax; float diffbay = by - ay; float diffcax = cx - ax; float diffcay = cy - ay; float diffcbx = cx - bx; float diffcby = cy - by; double r_numerator = diffcax * diffbax + diffcay * diffbay; double r_denomenator = square(diffbax) + square(diffbay); ////from www. j a v a 2s.c o m // (xx,yy) is the point on the lineSegment closest to (cx,cy) // if ((r_numerator >= 0) && (r_numerator <= r_denomenator)) { return (float) (Math.abs(diffcax * diffbay - diffcay * diffbax) / Math.sqrt(r_denomenator)); } else { double dist1 = square(diffcax) + square(diffcay); double dist2 = square(diffcbx) + square(diffcby); return (float) Math.sqrt(Math.min(dist1, dist2)); } } public static double square(double a) { return a * a; } public static float square(float a) { return a * a; } }