Java examples for java.lang:Math Geometry Line
find Closest Line
/*/*w ww. j ava 2 s . c o m*/ * Copyright (c) 2014 tabletoptool.com team. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl.html * * Contributors: * rptools.com team - initial implementation * tabletoptool.com team - further development */ import java.awt.geom.Area; import java.awt.geom.GeneralPath; import java.awt.geom.Line2D; import java.awt.geom.PathIterator; import java.awt.geom.Point2D; import java.util.HashSet; import java.util.List; import java.util.Set; public class Main{ public static Line2D findClosestLine(Point2D origin, PointNode pointList) { Line2D line = null; double distance = 0; PointNode node = pointList; do { Line2D newLine = new Line2D.Double(node.previous.point, node.point); double newDistance = getDistanceToCenter(origin, newLine); if (line == null || newDistance < distance) { line = newLine; distance = newDistance; } node = node.next; } while (node != pointList); return line; } public static double getDistanceToCenter(Point2D p, Line2D line) { Point2D midPoint = new Point2D.Double((line.getP1().getX() + line .getP2().getX()) / 2, (line.getP1().getY() + line.getP2() .getY()) / 2); return Math.hypot(midPoint.getX() - p.getX(), midPoint.getY() - p.getY()); } }