Line2D line1 = new Line2D.Float(100, 100, 200, 200); Line2D line2 = new Line2D.Float(150, 150, 150, 200); boolean result = line2.intersectsLine(line1); System.out.println(result); // => true // Also check out linesIntersect() if you do not need to construct the line objects // It will probably be faster due to putting less pressure on the garbage collector // if running it in a loop System.out.println(Line2D.linesIntersect(100,100,200,200,150,150,150,200));
Line2D myLine = getMyLine() // some imagine method giving your line Point2D[] polyPoints = getPolyPoints() // some imagine method giving point of your Polygon boolean intersect = false; for (int i = 0; i < polyPoints.length - 1; i++) { intersect = myLine.intersectsLine(polyPoints[i].getX(), polyPoints[i].getY(), polyPoints[i+1].getX(), polyPoints[i+1].getY()); if (intersect) { break; } } if (!intersect) { intersect = myLine.intersectsLine(polyPoints[polyPoints.length - 1].getX(), polyPoints[polyPoints.length - 1].getY(), polyPoints[0].getX(), polyPoints[0].getY()); }
Line2D line = new Line2D.Double(x1, y1, x2, y2); if (line.intersectsLine(Double.MIN_VALUE, k, Double.MAX_VALUE, k)) { double x = (x2 - x1) * (k - y1) / (y2 - y1); // intersection is at (x, k) } else { // intersection is outside extend of the line segment }
private static Point2D lineIntersection(Line2D line1, Line2D line2) { if (line1.intersectsLine(line2)) { return GeomUtil.getIntersectPoint(line1, line2); } return null; }
public static Set<Point2D> getLinePathCollisionPoints(Line2D line, LocalBoundedObject object) { Set<Point2D> result = new HashSet<Point2D>(); Iterator<Line2D> i = getLocalBoundedObjectLineSegments(object).iterator(); while (i.hasNext()) { Line2D lineSegment = i.next(); if (line.intersectsLine(lineSegment)) { Point2D intersectionPt = getLineIntersectionPoint(line, lineSegment); result.add(intersectionPt); } } return result; }
/** * Merge groups. * * @param groups * the groups */ private static void mergeGroups(Collection<Collection<Line2D>> groups) { int previousSize = 0; while (previousSize != groups.size()) { previousSize = groups.size(); outer: for (Collection<Line2D> outer : groups) { Iterator<Collection<Line2D>> it = groups.iterator(); while (it.hasNext()) { Collection<Line2D> inner = it.next(); if (outer == inner) { continue; } for (Line2D line1 : outer) { for (Line2D line2 : inner) { if (line1.intersectsLine(line2)) { outer.addAll(inner); it.remove(); break outer; } } } } } } }
/** * Collect intersecting lines. * * @param lines * the lines * @return the collection */ private static Collection<Collection<Line2D>> collectIntersectingLines(Collection<Line2D> lines) { Collection<Collection<Line2D>> groups = new ArrayList<>(); for (Line2D newLine : lines) { boolean addedToGroup = false; for (Collection<Line2D> group : groups) { for (Line2D line : group) { if (newLine.intersectsLine(line)) { group.add(newLine); addedToGroup = true; break; } } } if (!addedToGroup) { ArrayList<Line2D> newGroup = new ArrayList<>(); groups.add(newGroup); newGroup.add(newLine); } } mergeGroups(groups); return groups; }
/** * @return median line or null if any argument is invalid */ public static Line2D.Double getMedianLine(Line2D line1, Line2D line2) { if (line1 == null || line2 == null) { return null; } Point2D ptA = line1.getP1(), ptB = line1.getP2(); Point2D ptC = line2.getP1(), ptD = line2.getP2(); Line2D line3 = new Line2D.Double(ptA, ptC); Line2D line4 = new Line2D.Double(ptB, ptD); Point2D ptM, ptN; if (line3.intersectsLine(line4)) { ptM = new Point2D.Double((ptA.getX() + ptD.getX()) / 2, (ptA.getY() + ptD.getY()) / 2); ptN = new Point2D.Double((ptB.getX() + ptC.getX()) / 2, (ptB.getY() + ptC.getY()) / 2); } else { ptM = new Point2D.Double((ptA.getX() + ptC.getX()) / 2, (ptA.getY() + ptC.getY()) / 2); ptN = new Point2D.Double((ptB.getX() + ptD.getX()) / 2, (ptB.getY() + ptD.getY()) / 2); } return new Line2D.Double(ptM, ptN); }
/** * Determine the minimum intersection of a line and the outline of a shape (specified as a list * of points). * * @param outline the outline of a shape * @param line the line * @return the intersection */ private Point2D intersect(List<Point2D> outline, Line2D line) { Point2D previousPoint = outline.get(outline.size() - 1); for (Point2D point : outline) { Line2D currentLine = new Line2D.Double(point.getX(), point.getY(), previousPoint.getX(), previousPoint.getY()); if (line.intersectsLine(currentLine)) { return lineLineIntersect(currentLine, line); } previousPoint = point; } return new Point2D.Double(line.getX1(), line.getY1()); }
for (Line2D l : bboxBorder1) { for (Point2D p : getCorner(boundingBox2)) { if (l.intersectsLine(p.getX(), p.getY(), p.getX(), p.getY())) { touches = true; for (Line2D l : getBorder(boundingBox2)) { for (Point2D p : filterBoxCorner) { if (l.intersectsLine(p.getX(), p.getY(), p.getX(), p.getY())) { touches = true;
if (segment.intersectsLine(line)) return true;
for (Line2D xline : xlines) { for (Line2D yline : ylines) { if (xline.intersectsLine(yline)) { Point2D intersectionPoint = intersectionPoint(xline, yline); Point2D roundedIntersectionPoint = new Point2D.Double(
/** * Return true if the two line are in touche relation. * i.e. one of the extremity of a line is a point of the other. * * if one of the parameters is null it return false. * * @param line1 The first line2D. * @param line2 The second line2D. * * @return true if one of the extremity of a line is a point of the other. */ public static boolean touches(final Line2D line1, final Line2D line2) { if (line1 == null || line2 == null) return false; if (line1.intersectsLine(new Line2D.Double(line2.getX1(), line2.getY1(), line2.getX1(), line2.getY1())) || line1.intersectsLine(new Line2D.Double(line2.getX2(), line2.getY2(), line2.getX2(), line2.getY2())) || line2.intersectsLine(new Line2D.Double(line1.getX1(), line1.getY1(), line1.getX1(), line1.getY1())) || line2.intersectsLine(new Line2D.Double(line1.getX2(), line1.getY2(), line1.getX2(), line1.getY2()))) return true; return false; }
/** * Return true if the specified point is located on the border of the specified envelope. * if one of the parameters is null it return false. * * @param boundingBox A GeneralEnvelope. * @param point A GeneralDirectPosition * * @return true if the point is on the border of the envelope. */ public static boolean touches(final GeneralEnvelope boundingBox, final GeneralDirectPosition point) { if (point == null || boundingBox == null) return false; final Line2D pointLine = new Line2D.Double(point.getOrdinate(0), point.getOrdinate(1), point.getOrdinate(0), point.getOrdinate(1)); final List<Line2D> border = getBorder(boundingBox); for (Line2D l: border) { if (l.intersectsLine(pointLine)) return true; } return false; }
aPts1.getBlockZ()); if (line1.intersectsLine( lastPt2.getBlockX(), lastPt2.getBlockZ(),
/** * Return true if the specified line and the envelope are in touche relation. * i.e. one of the extremity of the line is a point of the border of the envelope. * * if one of the parameters is null it return false. * * @param boundingBox The bounding box. * @param line The line2D. * * @return boolean */ public static boolean touches(final GeneralEnvelope boundingBox, final Line2D line) { if (line == null || boundingBox == null) return false; final List<Line2D> border = getBorder(boundingBox); for (Line2D l: border) { if (l.intersectsLine(line.getX1(), line.getY1(), line.getX1(), line.getY1()) || l.intersectsLine(line.getX2(), line.getY2(), line.getX2(), line.getY2())) return true; } return false; }
/** * Return true if the point crosses the envelope. * i.e. the point is on the border of the envelope. * * @param boundingBox An envelope. * @param point A GeneralDirectPosition. * * @return True if the point crosses the envelope. */ public static boolean crosses(final GeneralEnvelope boundingBox, final GeneralDirectPosition point) { for (Line2D l : getBorder(boundingBox)) { if (l.intersectsLine(point.getOrdinate(0), point.getOrdinate(1), point.getOrdinate(0), point.getOrdinate(1))) { return true; } } return false; }
/** * Return true if the intersection between the envelope and the line is not empty. * * @param boundingBox An envelope. * @param line A line2D * * @return True f the intersection between the envelope and the point is empty. */ public static boolean intersect(final GeneralEnvelope boundingBox, final Line2D line) { final GeneralDirectPosition tempPoint1 = new GeneralDirectPosition(line.getX1(), line.getY1()); final GeneralDirectPosition tempPoint2 = new GeneralDirectPosition(line.getX2(), line.getY2()); if (boundingBox.contains(tempPoint1) || boundingBox.contains(tempPoint2)) return true; for (Line2D l: getBorder(boundingBox)) { if (l.intersectsLine(line)) return true; } return false; }
/** * Return true if the intersection between the envelope and the line is empty. * * @param boundingBox An envelope. * @param line A line2D * * @return True f the intersection between the envelope and the point is empty. */ public static boolean disjoint(final GeneralEnvelope boundingBox, final Line2D line) { final GeneralDirectPosition tempPoint1 = new GeneralDirectPosition(line.getX1(), line.getY1()); final GeneralDirectPosition tempPoint2 = new GeneralDirectPosition(line.getX2(), line.getY2()); if (!boundingBox.contains(tempPoint1) && !boundingBox.contains(tempPoint2)) { final List<Line2D> border = getBorder(boundingBox); for (Line2D l : border) { if (l.intersectsLine(line)) { return false; } } return true; } return false; }
/** * Return the shortest distance between a line and a boundingBox. * If the line intersect or is contains by the bounding box it return 0; * * @param line A line2D. * @param boundingBox A bounding box in the same CRS than the line * @param units The units in which the distance will be expressed. * @return The shortest distance between the bounding box and the line. */ public static double lineToBBoxDistance(final Line2D line, final GeneralEnvelope boundingBox, final String units) { final CoordinateReferenceSystem crs = boundingBox.getCoordinateReferenceSystem(); final GeneralDirectPosition tempPoint1 = new GeneralDirectPosition(line.getX1(), line.getY1()); tempPoint1.setCoordinateReferenceSystem(crs); final GeneralDirectPosition tempPoint2 = new GeneralDirectPosition(line.getX2(), line.getY2()); tempPoint2.setCoordinateReferenceSystem(crs); if (boundingBox.contains(tempPoint1) || boundingBox.contains(tempPoint2)) return 0; final List<Line2D> border = getBorder(boundingBox); double distance = Double.MAX_VALUE; for (Line2D l: border) { if (l.intersectsLine(line)) return 0; double tempDistance = lineTolineDistance(l, line, units); if (tempDistance < distance) distance = tempDistance; } return distance; }