# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .jl
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Julia 0.6.0
#     language: julia
#     name: julia-0.6
# ---

# These examples illustrate common operations on polyhedra using [Polyhedra.jl](https://github.com/JuliaPolyhedra/Polyhedra.jl):
#
# - [Intersection](#Intersection).
# - [Convex hull](#Convex-hull).

using Polyhedra, CDDLib

# ### Intersection <a name="Intersection"></a> 
#
# Intersection of polyhedra is obtained with the [`intersect`](https://juliapolyhedra.github.io/Polyhedra.jl/latest/utilities.html#Base.intersect) function.
#
# Below we compute the intersection of two randomly generated polygons in V-representation.

P1 = polyhedron(SimpleVRepresentation(randn(15, 2)), CDDLibrary())

P2 = polyhedron(SimpleVRepresentation(randn(15, 2)), CDDLibrary())

Pint = intersect(P1, P2)

# Notice that the H-representation of the resulting polyhedron has been computed, but not the V-representation:

hrepiscomputed(Pint)

vrepiscomputed(Pint)

npoints(Pint)

# We can plot the polygons and their intersection using the `plot` function. For further plotting options see the [Plots.jl](http://docs.juliaplots.org/latest/) documentation.

using Plots

plot(P1, alpha=0.2, color="blue")
plot!(P2, color="red", alpha=0.2)
plot!(Pint, color="yellow", alpha=0.6)

# ### Convex hull <a name="Convex-hull"></a> 
#
# The binary convex hull operation between two polyhedra is obtained with the [`convexhull`](https://juliapolyhedra.github.io/Polyhedra.jl/latest/utilities.html#Polyhedra.convexhull) function.
#
# Below we compute the convex hull of the two randomly generated polygons in V-representation from the previous example.

Pch = convexhull(P1, P2)

npoints(Pch)

plot(P1, alpha=0.2, color="blue")
plot!(P2, color="red", alpha=0.2)
plot!(Pch, color="green", alpha=0.1)

StarcoderdataJupyterScriptsDedupFiltered