Here you can find the source of distancePointToBot(Point2D.Double sourceLocation, Point2D.Double botLocation)
public static double distancePointToBot(Point2D.Double sourceLocation, Point2D.Double botLocation)
//package com.java2s; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.util.ArrayList; import java.util.Iterator; public class Main { public static double distancePointToBot(Point2D.Double sourceLocation, Point2D.Double botLocation) { if (sourceLocation.x > botLocation.x - 18 && sourceLocation.x < botLocation.x + 18 && sourceLocation.y > botLocation.y - 18 && sourceLocation.y < botLocation.y + 18) { return 0; } else {/* w ww . j av a 2 s .c om*/ ArrayList<Line2D.Double> botSides = new ArrayList<Line2D.Double>(); botSides.add(new Line2D.Double(botLocation.x - 18, botLocation.y - 18, botLocation.x + 18, botLocation.y - 18)); botSides.add(new Line2D.Double(botLocation.x + 18, botLocation.y - 18, botLocation.x + 18, botLocation.y + 18)); botSides.add(new Line2D.Double(botLocation.x + 18, botLocation.y + 18, botLocation.x - 18, botLocation.y + 18)); botSides.add(new Line2D.Double(botLocation.x - 18, botLocation.y + 18, botLocation.x - 18, botLocation.y - 18)); double distance = Double.POSITIVE_INFINITY; Iterator<Line2D.Double> sideIterator = botSides.iterator(); while (sideIterator.hasNext()) { distance = Math.min(distance, sideIterator.next() .ptSegDist(sourceLocation)); } return distance; } } }