Java examples for 2D Graphics:Path
get GeneralPath Intersect Point
//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 Point2D getIntersectPoint(Line2D l, GeneralPath p) { return getIntersectPoint(l.getX1(), l.getY1(), l.getX2(), l.getY2(), p);/*from w ww . ja v a2 s. com*/ } public static Point2D getIntersectPoint(double x1, double y1, double x2, double y2, GeneralPath p) { double x3 = 0, y3 = 0, x4 = 0, y4 = 0; int index = 0; double seg[] = new double[6]; for (PathIterator i = p.getPathIterator(null); !i.isDone(); i .next()) { int segType = i.currentSegment(seg); if (index == 0) { x3 = seg[0]; y3 = seg[1]; } else if (index == 1) { x4 = seg[0]; y4 = seg[1]; } else if (index > 1) { x3 = x4; y3 = y4; x4 = seg[0]; y4 = seg[1]; } if (index > 0 && Line2D .linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)) { double l1m, l2m; //if lines are vertical, cater for a divide by 0: if (x2 == x1) { l2m = (y4 - y3) / (x4 - x3); return (new Point2D.Double(x1, (l2m * x1) + y3)); } else if (x4 == x3) { l1m = (y2 - y1) / (x2 - x1); return (new Point2D.Double(x3, (l1m * x3) + y1)); } else { l1m = (y2 - y1) / (x2 - x1); l2m = (y4 - y3) / (x4 - x3); double x = ((-l1m) * x1) + y1 + (l2m * x3) - y3; x = x / (l2m - l1m); double y = l1m * (x - x1) + y1; return (new Point2D.Double(x, y)); } } index++; } return null; } public static Point2D getIntersectPoint(Point2D point, GeneralPath p1, GeneralPath p2) { Point2D intersectionPoint, returnPoint = null; double x1 = 0, y1 = 0, x2 = 0, y2 = 0, closestDist = 100000; int index = 0; double seg[] = new double[6]; for (PathIterator i = p1.getPathIterator(null); !i.isDone(); i .next()) { int segType = i.currentSegment(seg); if (index == 0) { x1 = seg[0]; y1 = seg[1]; x2 = x1; y2 = y1; } if (index > 0) { x1 = x2; y1 = y2; x2 = seg[0]; y2 = seg[1]; intersectionPoint = getIntersectPoint(x1, y1, x2, y2, p2); if (intersectionPoint != null) { double dist = intersectionPoint.distance(point); if (dist <= closestDist) { returnPoint = intersectionPoint; closestDist = dist; } } } index++; } return returnPoint; } }