Java examples for 2D Graphics:Path
get GeneralPath Nearest Line
//package com.java2s; import java.awt.geom.PathIterator; import java.awt.geom.GeneralPath; import java.awt.geom.Point2D; import java.awt.geom.Line2D; public class Main { public static Line2D getNearestLine(Point2D point, GeneralPath path) { Line2D currentLine, nearestLine = null; double currentDistance, nearestDistance = 100000.0; double seg[] = new double[6]; double x1 = 0, y1 = 0, x2 = 0, y2 = 0; int index = 0; for (PathIterator i = path.getPathIterator(null); !i.isDone(); i .next()) {//from w w w . j a v a2 s .co m int segType = i.currentSegment(seg); if (index == 0) { x1 = seg[0]; y1 = seg[1]; x2 = seg[0]; y2 = seg[1]; } else if (index > 0) { x1 = x2; y1 = y2; x2 = seg[0]; y2 = seg[1]; currentLine = new Line2D.Double(x1, y1, x2, y2); currentDistance = currentLine.ptSegDist(point); if (currentDistance < nearestDistance) { nearestDistance = currentDistance; nearestLine = currentLine; } } index++; } return nearestLine; } }