Example usage for java.awt.geom Line2D.Double ptSegDistSq

List of usage examples for java.awt.geom Line2D.Double ptSegDistSq

Introduction

In this page you can find the example usage for java.awt.geom Line2D.Double ptSegDistSq.

Prototype

public double ptSegDistSq(double px, double py) 

Source Link

Document

Returns the square of the distance from a point to this line segment.

Usage

From source file:eu.transkribus.swt_canvas.util.GeomUtils.java

/**
 * Returns the distance and the closest segment of a point (x,y) to a polygon given as a series of points
 * @param isClosedShape True if this is a closes polygon or false if its a polyline
 *///from w ww  .ja v  a2s  .  c om
public static Pair<Double, java.awt.geom.Line2D.Double> getDistToPolygonAndClosestSegment(List<Point> pts,
        double x, double y, boolean isClosedShape) {
    double minDist = Integer.MAX_VALUE;
    java.awt.geom.Line2D.Double minLine = new java.awt.geom.Line2D.Double(0, 0, 0, 0);

    int N = isClosedShape ? pts.size() : pts.size() - 1;

    for (int i = 0; i < N; ++i) {
        java.awt.geom.Line2D.Double line = new java.awt.geom.Line2D.Double(pts.get(i),
                pts.get((i + 1) % pts.size()));
        double d = line.ptSegDistSq(x, y);
        //         logger.debug("d = "+d);
        if (d < minDist) {
            minDist = d;
            minLine = line;
        }
    }

    return Pair.of(minDist, minLine);
}