Java Distance between Point and Line distancePointToBot(Point2D.Double sourceLocation, Point2D.Double botLocation)

Here you can find the source of distancePointToBot(Point2D.Double sourceLocation, Point2D.Double botLocation)

Description

distance Point To Bot

License

Open Source License

Declaration

public static double distancePointToBot(Point2D.Double sourceLocation,
            Point2D.Double botLocation) 

Method Source Code

//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;
        }
    }
}

Related

  1. distanceFromLine(int xa, int ya, int xb, int yb, int xc, int yc)
  2. distancePointToLine(final Point2D point, final Line2D line)
  3. DistanceToLine(double x, double y, double x1, double y1, double x2, double y2)
  4. distanceToLine(Point2D p, Point2D endA, Point2D endZ)
  5. distanceToLine2(Line2D l, Point2D p)