Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sutherland–Hodgman VPolygon intersection algorithm #2345

Merged
merged 9 commits into from
Aug 25, 2020
16 changes: 12 additions & 4 deletions src/ConcreteOperations/intersection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -690,18 +690,21 @@ function intersection(P1::VPolytope{N},
end

"""
intersection(P1::VPolygon, P2::VPolygon)
intersection(P1::VPolygon{N}, P2::VPolygon{N};
apply_convex_hull::Bool=true) where {N}

Compute the intersection of two polygons in vertex representation.

### Input

- `P1` -- polygon in vertex representation
- `P2` -- polygon in vertex representation
- `apply_convex_hull` -- (default, optional: `true`) use the flag to skip the
computation of the convex hull in the resulting `VPolygon`

### Output

A `VPolygon`.
A `VPolygon` or an `EmptySet` if the intersection is empty.

### Algorithm

Expand All @@ -710,11 +713,16 @@ clipping algorithm](https://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_alg
The implementation is based on the one found in
[rosetta code](http://www.rosettacode.org/wiki/Sutherland-Hodgman_polygon_clipping#Julia).
"""
function intersection(P1::VPolygon, P2::VPolygon)
function intersection(P1::VPolygon{N}, P2::VPolygon{N};
apply_convex_hull::Bool=true) where {N}
v1 = vertices_list(P1)
v2 = vertices_list(P2)
v12 = _intersection_vrep(v1, v2)
return VPolygon(v12)
if isempty(v12)
return EmptySet{N}(2)
else
return VPolygon(v12, apply_convex_hull=apply_convex_hull)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO VPolygons should always be created using a convex hull unless the algorithm guarantees that result anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes , i agree. i left the option as true by default.

end
end

"""
Expand Down