Here you can find the source of colinearPoint(Line2D line, Point2D point, double distance)
public static Point2D colinearPoint(Line2D line, Point2D point, double distance)
//package com.java2s; /*//from w w w .j av a 2 s.c om * GeoTools - The Open Source Java GIS Toolkit * http://geotools.org * * (C) 2001-2008, Open Source Geospatial Foundation (OSGeo) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; * version 2.1 of the License. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. */ import java.awt.geom.Line2D; import java.awt.geom.Point2D; import static java.lang.Math.*; public class Main { public static Point2D colinearPoint(Line2D line, Point2D point, double distance) { return colinearPoint(line.getX1(), line.getY1(), line.getX2(), line.getY2(), point.getX(), point.getY(), distance); } public static Point2D colinearPoint(double x1, double y1, double x2, double y2, double x, double y, double distance) { final double ox1 = x1; final double oy1 = y1; final double ox2 = x2; final double oy2 = y2; distance *= distance; if (x1 == x2) { double dy = x1 - x; dy = sqrt(distance - dy * dy); y1 = y - dy; y2 = y + dy; } else if (y1 == y2) { double dx = y1 - y; dx = sqrt(distance - dx * dx); x1 = x - dx; x2 = x + dx; } else { final double m = (y1 - y2) / (x2 - x1); final double y0 = (y2 - y) + m * (x2 - x); final double B = m * y0; final double A = m * m + 1; final double C = sqrt(B * B + A * (distance - y0 * y0)); x1 = (B + C) / A; x2 = (B - C) / A; y1 = y + y0 - m * x1; y2 = y + y0 - m * x2; x1 += x; x2 += x; } boolean in1, in2; if (oy1 > oy2) { in1 = y1 <= oy1 && y1 >= oy2; in2 = y2 <= oy1 && y2 >= oy2; } else { in1 = y1 >= oy1 && y1 <= oy2; in2 = y2 >= oy1 && y2 <= oy2; } if (ox1 > ox2) { in1 &= x1 <= ox1 && x1 >= ox2; in2 &= x2 <= ox1 && x2 >= ox2; } else { in1 &= x1 >= ox1 && x1 <= ox2; in2 &= x2 >= ox1 && x2 <= ox2; } if (!in1 && !in2) return null; if (!in1) return new Point2D.Double(x2, y2); if (!in2) return new Point2D.Double(x1, y1); x = x1 - ox1; y = y1 - oy1; final double d1 = x * x + y * y; x = x2 - ox1; y = y2 - oy1; final double d2 = x * x + y * y; if (d1 > d2) return new Point2D.Double(x2, y2); else return new Point2D.Double(x1, y1); } }