Polylines with 2 sides can only intersect in cases where points are collinear (we don't support detecting these). Cannot have intersection when < 3 line segments (< 4 points)
498352
function (extraPoints) {
		var points = this._getProjectedPoints(),
			len = points ? points.length : 0;
		// Increment length by extraPoints if present
		len += extraPoints || 0;

		return !points || len <= 3;
	}
1097836
function doesIntersect (rect1, rect2) {
  let intersectLeftRight;
  let intersectTopBottom;

  const rect1Points = rectToPoints(rect1);
  const rect2Points = rectToPoints(rect2);

  if (rect1.width >= 0) {
    if (rect2.width >= 0) {
      intersectLeftRight = !((rect1Points.bottomRight.x <= rect2Points.topLeft.x) || (rect2Points.bottomRight.x <= rect1Points.topLeft.x));
    } else {
      intersectLeftRight = !((rect1Points.bottomRight.x <= rect2Points.bottomRight.x) || (rect2Points.topLeft.x <= rect1Points.topLeft.x));
    }
  } else if (rect2.width >= 0) {
    intersectLeftRight = !((rect1Points.topLeft.x <= rect2Points.topLeft.x) || (rect2Points.bottomRight.x <= rect1Points.bottomRight.x));
  } else {
    intersectLeftRight = !((rect1Points.topLeft.x <= rect2Points.bottomRight.x) || (rect2Points.topLeft.x <= rect1Points.bottomRight.x));
  }

  if (rect1.height >= 0) {
    if (rect2.height >= 0) {
      intersectTopBottom = !((rect1Points.bottomRight.y <= rect2Points.topLeft.y) || (rect2Points.bottomRight.y <= rect1Points.topLeft.y));
    } else {
      intersectTopBottom = !((rect1Points.bottomRight.y <= rect2Points.bottomRight.y) || (rect2Points.topLeft.y <= rect1Points.topLeft.y));
    }
  } else if (rect2.height >= 0) {
    intersectTopBottom = !((rect1Points.topLeft.y <= rect2Points.topLeft.y) || (rect2Points.bottomRight.y <= rect1Points.bottomRight.y));
  } else {
    intersectTopBottom = !((rect1Points.topLeft.y <= rect2Points.bottomRight.y) || (rect2Points.top <= rect1Points.bottomRight.y));
  }

  return intersectLeftRight && intersectTopBottom;
}
243145
private static boolean polygonCrossesPolyline_(Polygon polygon_a,
			Polyline polyline_b, double tolerance,
			ProgressTracker progress_tracker) {
		// Quick rasterize test to see whether the the geometries are disjoint,
		// or if one is contained in the other.
		int relation = tryRasterizedContainsOrDisjoint_(polygon_a, polyline_b,
				tolerance, false);

		if (relation == Relation.disjoint || relation == Relation.contains)
			return false;

		return polygonCrossesPolylineImpl_(polygon_a, polyline_b, tolerance,
				null);
	}
410728
private boolean isCrossed(Point2d beg1, Point2d end1, Point2d beg2, Point2d end2) {
        return Line2D.linesIntersect(beg1.x, beg1.y, end1.x, end1.y, beg2.x, beg2.y, end2.x, end2.y);
    }
1222388
function snapRound (points, edges, useColor) {
  // 1. find edge crossings
  var edgeBounds = boundEdges(points, edges)
  var crossings = getCrossings(points, edges, edgeBounds)

  // 2. find t-junctions
  var vertBounds = boundPoints(points)
  var tjunctions = getTJunctions(points, edges, edgeBounds, vertBounds)

  // 3. cut edges, construct rational points
  var ratPoints = cutEdges(points, edges, crossings, tjunctions, useColor)

  // 4. dedupe verts
  var labels = dedupPoints(points, ratPoints, vertBounds)

  // 5. dedupe edges
  dedupEdges(edges, labels, useColor)

  // 6. check termination
  if (!labels) {
    return (crossings.length > 0 || tjunctions.length > 0)
  }

  // More iterations necessary
  return true
}
1155947
def is_intersect(line_a, line_b):
    
    # Find the four orientations needed for general and special cases
    orientation_1 = orientation(line_a.endpoint_a, line_a.endpoint_b,
                                line_b.endpoint_a)
    orientation_2 = orientation(line_a.endpoint_a, line_a.endpoint_b,
                                line_b.endpoint_b)
    orientation_3 = orientation(line_b.endpoint_a, line_b.endpoint_b,
                                line_a.endpoint_a)
    orientation_4 = orientation(line_b.endpoint_a, line_b.endpoint_b,
                                line_a.endpoint_b)

    # General case
    if (orientation_1 != orientation_2 and orientation_3 != orientation_4):
        return True

    # Special cases
    if (orientation_1 == 0 and on_segment(line_a.endpoint_a, line_b.endpoint_a,
                                          line_a.endpoint_b)):
        return True
    if (orientation_2 == 0 and on_segment(line_a.endpoint_a, line_b.endpoint_b,
                                          line_a.endpoint_b)):
        return True
    if (orientation_3 == 0 and on_segment(line_b.endpoint_a, line_a.endpoint_a,
                                          line_b.endpoint_b)):
        return True
    if (orientation_4 == 0 and on_segment(line_b.endpoint_a, line_a.endpoint_b,
                                          line_b.endpoint_b)):
        return True

    return False
1175763
public boolean intersects(Geometry geometry) {
		if (geometry == null || isEmpty() || geometry.isEmpty()) {
			return false;
		}

		if (geometry.getNumGeometries() > 1) {
			for (int n = 0; n < geometry.getNumGeometries(); n++) {
				if (intersects(geometry.getGeometryN(n))) {
					return true;
				}
			}
		} else {
			final Coordinate[] coords = geometry.getCoordinates();
			if (coords.length > 1 && coordinates.length > 1) {
				for (int i = 0; i < coordinates.length - 1; i++) {
					for (int j = 0; j < coords.length - 1; j++) {
						if (Mathlib.lineIntersects(coordinates[i], coordinates[i + 1], coords[j], coords[j + 1])) {
							return true;
						}
					}
					if (Mathlib.touches(geometry, coordinates[i])) {
						return true;
					}
				}
			} else {
				// TODO implement me
			}
		}

		return false;
	}
1274805
private static boolean polylineWithinEnvelope_(Polyline polyline_a,
			Envelope envelope_b, double tolerance,
			ProgressTracker progress_tracker) {
		Envelope2D env_a = new Envelope2D(), env_b = new Envelope2D();
		polyline_a.queryEnvelope2D(env_a);
		envelope_b.queryEnvelope2D(env_b);

		if (!envelopeInfContainsEnvelope_(env_b, env_a, tolerance))
			return false;

		if (env_b.getHeight() <= tolerance && env_b.getWidth() <= tolerance)
			return false;

		if (env_b.getHeight() <= tolerance || env_b.getWidth() <= tolerance)
			return envelopeInfContainsEnvelope_(env_b, env_a, tolerance);

		SegmentIterator seg_iter_a = polyline_a.querySegmentIterator();
		Envelope2D env_b_deflated = new Envelope2D();
		env_b_deflated.setCoords(env_b);
		env_b_deflated.inflate(-tolerance, -tolerance);

		boolean b_interior = false;
		Envelope2D env_segment_a = new Envelope2D();
		Envelope2D env_inter = new Envelope2D();

		while (seg_iter_a.nextPath()) {
			while (seg_iter_a.hasNextSegment()) {
				Segment segment_a = seg_iter_a.nextSegment();
				segment_a.queryEnvelope2D(env_segment_a);

				if (env_b_deflated.containsExclusive(env_segment_a)) {
					b_interior = true;
					continue;
				}

				env_inter.setCoords(env_b_deflated);
				env_inter.intersect(env_segment_a);

				if (!env_inter.isEmpty()
						&& (env_inter.getHeight() > tolerance || env_inter
								.getWidth() > tolerance))
					b_interior = true;
			}
		}

		return b_interior;
	}
934030
int filterClosePoints(double tolerance, boolean b_remove_last_vertices, boolean only_polygons) {
		int res = 0;
		for (int geometry = getFirstGeometry(); geometry != -1; geometry = getNextGeometry(geometry)) {
			int gt = getGeometryType(geometry);
			if (!Geometry.isMultiPath(gt))
				continue;
			if (only_polygons && gt != GeometryType.Polygon)
				continue;

			boolean b_polygon = getGeometryType(geometry) == Geometry.GeometryType.Polygon;

			for (int path = getFirstPath(geometry); path != -1;) {
				// We go from the start to the half of the path first, then we
				// go from the end to the half of the path.
				int vertex_counter = 0;
				for (int vertex = getFirstVertex(path); vertex_counter < getPathSize(path) / 2;) {
					int next = getNextVertex(vertex);
					if (next == -1)
						break;
					int vindex = getVertexIndex(vertex);
					Segment seg = getSegmentFromIndex_(vindex);
					double length = 0;
					if (seg != null) {
						length = seg.calculateLength2D();
					} else {
						int vindex_next = getVertexIndex(next);
						length = m_vertices._getShortestDistance(vindex,
								vindex_next);
					}

					if (length <= tolerance) {
						if (length == 0) {
							if (res == 0)
								res = -1;
						} else
							res = 1;

						if (next != getLastVertex(path)) {
							transferAllDataToTheVertex(next, vertex);
							removeVertex(next, true);
						}
					} else {
						vertex = getNextVertex(vertex);
					}
					vertex_counter++;
				}

				int first_vertex = getFirstVertex(path);
				for (int vertex = isClosedPath(path) ? first_vertex
						: getLastVertex(path); getPathSize(path) > 0;) {
					int prev = getPrevVertex(vertex);
					if (prev != -1) {
						int vindex_prev = getVertexIndex(prev);
						Segment seg = getSegmentFromIndex_(vindex_prev);
						double length = 0;
						if (seg != null) {
							length = seg.calculateLength2D();
						} else {
							int vindex = getVertexIndex(vertex);
							length = m_vertices._getShortestDistance(vindex,
									vindex_prev);
						}

						if (length <= tolerance) {
							if (length == 0) {
								if (res == 0)
									res = -1;
							} else
								res = 1;

							transferAllDataToTheVertex(prev, vertex);
							removeVertex(prev, false);
							if (first_vertex == prev)
								first_vertex = getFirstVertex(path);
						} else {
							vertex = getPrevVertex(vertex);
							if (vertex == first_vertex)
								break;
						}
					} else {
						removeVertex(vertex, true);// remove the last vertex in
													// the path
						if (res == 0)
							res = -1;
						break;
					}
				}

				int path_size = getPathSize(path);
				if (b_remove_last_vertices
						&& (b_polygon ? path_size < 3 : path_size < 2)) {
					path = removePath(path);
					res = path_size > 0 ? 1 : (res == 0 ? -1 : res);
				} else
					path = getNextPath(path);
			}
		}

		return res;
	}
932961
private static Point getShapeIntersection(Shape shape,
                                              Line2D.Double line) {
        PathIterator it = shape.getPathIterator(null);
        double[] coords = new double[6];
        double[] pos = new double[2];
        Line2D.Double l = new Line2D.Double();
        while (!it.isDone()) {
            int type = it.currentSegment(coords);
            switch (type) {
                case PathIterator.SEG_MOVETO:
                    pos[0] = coords[0];
                    pos[1] = coords[1];
                    break;
                case PathIterator.SEG_LINETO:
                    l = new Line2D.Double(pos[0],
                                          pos[1],
                                          coords[0],
                                          coords[1]);
                    if (line.intersectsLine(l)) {
                        return getLinesIntersection(line,
                                                    l);
                    }
                    pos[0] = coords[0];
                    pos[1] = coords[1];
                    break;
                case PathIterator.SEG_CLOSE:
                    break;
                default:
                    // whatever
            }
            it.next();
        }
        return null;
    }
926913
function lineRectangleIntersection(line, rect) {
  const intersections = [];

  Object.keys(rect).forEach(function(side) {
    const segment = rect[side];
    const intersection = line.intersectLine(segment);

    if (intersection) {
      intersections.push(intersection);
    }
  });

  return intersections;
}
1322252
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());
    }
80963
private static boolean polylineDisjointMultiPoint_(Polyline polyline_a,
			MultiPoint multipoint_b, double tolerance,
			ProgressTracker progress_tracker) {
		// Quick rasterize test to see whether the the geometries are disjoint.
		if (tryRasterizedContainsOrDisjoint_(polyline_a, multipoint_b,
				tolerance, false) == Relation.disjoint)
			return true;

		return !linearPathIntersectsMultiPoint_(polyline_a, multipoint_b,
				tolerance, false);
	}
299392
public static final boolean collinear(final Point2D p1, final Point2D p2, final Point2D p3)
    {
        return Geometry.collinear(p1, p2, p3);
    }
101612
def line_intersection(self, point1, point2, tolerance=1e-8):
        
        b1 = self.bary_coords(point1)
        b2 = self.bary_coords(point2)
        l = b1 - b2
        # don't use barycentric dimension where line is parallel to face
        valid = np.abs(l) > 1e-10
        # array of all the barycentric coordinates on the line where
        # one of the values is 0
        possible = b1 - (b1[valid] / l[valid])[:, None] * l
        barys = []
        for p in possible:
            # it's only an intersection if its in the simplex
            if (p >= -tolerance).all():
                found = False
                # don't return duplicate points
                for b in barys:
                    if np.all(np.abs(b - p) < tolerance):
                        found = True
                        break
                if not found:
                    barys.append(p)
        assert len(barys) < 3
        return [self.point_from_bary_coords(b) for b in barys]
595763
private static boolean polylineTouchesPoint_(Polyline polyline_a,
			Point point_b, double tolerance, ProgressTracker progress_tracker) {
		// Quick rasterize test to see whether the the geometries are disjoint.
		if (tryRasterizedContainsOrDisjoint_(polyline_a, point_b, tolerance,
				false) == Relation.disjoint)
			return false;

		Point2D pt_b = point_b.getXY();
		return linearPathTouchesPointImpl_(polyline_a, pt_b, tolerance);
	}
1154546
def intersect(self, other_seg, tol=1e-12):
        """"""
        if isinstance(other_seg, Line):
            return bezier_by_line_intersections(self, other_seg)
        elif (isinstance(other_seg, QuadraticBezier) or
              isinstance(other_seg, CubicBezier)):
            assert self != other_seg
            longer_length = max(self.length(), other_seg.length())
            return bezier_intersections(self, other_seg,
                                        longer_length=longer_length,
                                        tol=tol, tol_deC=tol)
        elif isinstance(other_seg, Arc):
            t2t1s = other_seg.intersect(self)
            return [(t1, t2) for t2, t1 in t2t1s]
        elif isinstance(other_seg, Path):
            raise TypeError(
                "other_seg must be a path segment, not a Path object, use "
                "Path.intersect().")
        else:
            raise TypeError("other_seg must be a path segment.")