Here you can find the source of distance(int px, int py, int x1, int y1, int x2, int y2)
Parameter | Description |
---|---|
px | x-coordinate of the point |
py | y-coordinate of the point |
x1 | x-coordinate of segment point 1 |
y1 | y-coordinate of segment point 1 |
x2 | x-coordinate of segment point 2 |
y2 | y-coordinate of segment point 2 |
public static double distance(int px, int py, int x1, int y1, int x2, int y2)
//package com.java2s; /***************************************************************************** * Copyright (c) 2007, 2014 Intel Corporation, Ericsson * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:// w w w.j ava 2 s . com * Intel Corporation - Initial API and implementation * Ruslan A. Scherbakov, Intel - Initial API and implementation * Alvaro Sanchez-Leon - Udpated for TMF * Patrick Tasse - Refactoring * Marc-Andre Laperle - Add time zone preference *****************************************************************************/ public class Main { /** * Calculates the distance between a point and a line segment. If the point * is in the perpendicular region between the segment points, return the * distance from the point to its projection on the segment. Otherwise * return the distance from the point to its closest segment point. * * @param px * x-coordinate of the point * @param py * y-coordinate of the point * @param x1 * x-coordinate of segment point 1 * @param y1 * y-coordinate of segment point 1 * @param x2 * x-coordinate of segment point 2 * @param y2 * y-coordinate of segment point 2 * * @return the distance in pixels */ public static double distance(int px, int py, int x1, int y1, int x2, int y2) { double length2 = distance2(x1, y1, x2, y2); if (length2 == 0) { return Math.sqrt(distance2(px, py, x1, y1)); } // 'r' is the ratio of the position, between segment point 1 and segment // point 2, of the projection of the point on the segment double r = ((px - x1) * (x2 - x1) + (py - y1) * (y2 - y1)) / length2; if (r <= 0.0) { // the projection is before segment point 1, return distance from // the point to segment point 1 return Math.sqrt(distance2(px, py, x1, y1)); } if (r >= 1.0) { // the projection is after segment point 2, return distance from // the point to segment point 2 return Math.sqrt(distance2(px, py, x2, y2)); } // the projection is between the segment points, return distance from // the point to its projection on the segment int x = (int) (x1 + r * (x2 - x1)); int y = (int) (y1 + r * (y2 - y1)); return Math.sqrt(distance2(px, py, x, y)); } /** * Calculates the square of the distance between two points. * * @param x1 * x-coordinate of point 1 * @param y1 * y-coordinate of point 1 * @param x2 * x-coordinate of point 2 * @param y2 * y-coordinate of point 2 * * @return the square of the distance in pixels^2 */ public static double distance2(int x1, int y1, int x2, int y2) { int dx = x2 - x1; int dy = y2 - y1; int d2 = dx * dx + dy * dy; return d2; } }