% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/has_overlap2d.R
\name{has_overlap2d}
\alias{has_overlap2d}
\alias{has_overlap2d.Polygon2D}
\alias{has_overlap2d.Ellipse2D}
\title{Whether two 2D shapes overlap}
\usage{
has_overlap2d(x, y, ...)

\method{has_overlap2d}{Polygon2D}(x, y, ..., n = 64L, tol = sqrt(.Machine$double.eps))

\method{has_overlap2d}{Ellipse2D}(x, y, ..., n = 64L, tol = sqrt(.Machine$double.eps))
}
\arguments{
\item{x, y}{The two shapes to check.  Supported classes are \link{Polygon2D}
and \link{Ellipse2D}.}

\item{...}{Ignored; only included for S3 method consistency.}

\item{n}{Number of vertices used to approximate non-circular \link{Ellipse2D}
objects (default \code{64L}).  Larger values give tighter bounds.}

\item{tol}{Numeric tolerance for overlap comparisons (default \code{0}).
A positive value requires projections to overlap by more than \code{tol}
before overlap is declared, which avoids false positives caused by
floating-point error accumulating in rotated coordinates.
\code{sqrt(.Machine$double.eps)} is a reasonable choice when shapes are
constructed via trigonometric transformations.}
}
\value{
A logical vector (or \code{NA}) of length equal to the longer of \code{x}
and \code{y} (for \link{Ellipse2D} objects; \link{Polygon2D} objects are always scalar).
}
\description{
\code{has_overlap2d()} is an S3 generic that returns whether two 2D shapes
have a non-zero-area overlap (i.e. their interiors intersect).
}
\details{
For two convex shapes the function uses the
\href{https://en.wikipedia.org/wiki/Hyperplane_separation_theorem}{Separating Axis Theorem (SAT)}.
For concave \link{Polygon2D} objects the function first checks whether the
axis-aligned bounding box and convex hull overlap; if neither rules out
overlap it returns \code{NA} with a warning because exact detection is not yet
supported.
For non-circular \link{Ellipse2D} objects the function approximates the ellipse
with inner and outer polygons: if the outer polygon does not overlap the
result is \code{FALSE}; if the inner polygon overlaps the result is \code{TRUE};
otherwise \code{NA} is returned with a warning.
}
\examples{
# Two overlapping squares
sq1 <- as_polygon2d(as_coord2d(
  x = c(0, 1, 1, 0),
  y = c(0, 0, 1, 1)
))
sq2 <- as_polygon2d(as_coord2d(
  x = c(0.5, 1.5, 1.5, 0.5),
  y = c(0.5, 0.5, 1.5, 1.5)
))
sq3 <- as_polygon2d(as_coord2d(
  x = c(2, 3, 3, 2),
  y = c(2, 2, 3, 3)
))
has_overlap2d(sq1, sq2)
has_overlap2d(sq1, sq3)

# Circle vs polygon
circ <- as_ellipse2d(as_coord2d(0.5, 0.5), rx = 0.4)
has_overlap2d(sq1, circ)
has_overlap2d(sq3, circ)

# Two circles
c1 <- as_ellipse2d(as_coord2d(0, 0), rx = 1)
c2 <- as_ellipse2d(as_coord2d(1.5, 0), rx = 1)
c3 <- as_ellipse2d(as_coord2d(3, 0), rx = 1)
has_overlap2d(c1, c2)
has_overlap2d(c1, c3)
}
