# Convex hull of a set of points¶

## Convex hull in a plane¶

This examples shows how to find the convex hull in the context of a plane. First we have to create an object representing the vertices. We have multiple ways of doing this

In :
using Polyhedra

v = convexhull([0, 0], [0, 1], [1, 0], [0.1, 0.1]) # list of points
v = vrep([[0, 0], [0, 1], [1, 0], [0.1, 0.1]]) # vector of points

Out:
V-representation Polyhedra.PointsHull{Float64, Vector{Float64}, Int64}:
4-element iterator of Vector{Float64}:
[0.0, 0.0]
[0.0, 1.0]
[1.0, 0.0]
[0.1, 0.1]

number of points × dimension matrix

In :
x = [0,0,1,0.1]
y = [0,1,0,0.1]
v = vrep([x y])

Out:
V-representation MixedMatVRep{Float64, Matrix{Float64}}:
4-element iterator of Vector{Float64}:
[0.0, 0.0]
[0.0, 1.0]
[1.0, 0.0]
[0.1, 0.1]

Then we can compute the hull of these points using the planar_hull function

In :
Polyhedra.planar_hull(v)

Out:
V-representation Polyhedra.Hull{Float64, Vector{Float64}, Int64}:
3-element iterator of Vector{Float64}:
[0.0, 0.0]
[0.0, 1.0]
[1.0, 0.0]

## Convex hull in higher dimension¶

In higher dimension, we can do it with a linear programming solver implementing the MathOptInterface, e.g.,

In :
import GLPK

removevredundancy(v, GLPK.Optimizer)

Out:
V-representation Polyhedra.Hull{Float64, Vector{Float64}, Int64}:
3-element iterator of Vector{Float64}:
[0.0, 0.0]
[0.0, 1.0]
[1.0, 0.0]

We can also use any Polyhedral library implementing the interface of this package. If we don't specify any library, it falls back to a default one implementing on this package which will use the planar_hull if the dimension is 2 (so it's equivalent to the first approach shown above):

In :
p = polyhedron(v)
removevredundancy!(p)
p

Out:
Polyhedron DefaultPolyhedron{Float64, MixedMatHRep{Float64, Matrix{Float64}}, MixedMatVRep{Float64, Matrix{Float64}}}:
3-element iterator of Vector{Float64}:
[0.0, 0.0]
[0.0, 1.0]
[1.0, 0.0]

We can also specify a library:

In :
using CDDLib

p = polyhedron(v, CDDLib.Library())
removevredundancy!(p)
p

Out:
Polyhedron CDDLib.Polyhedron{Float64}:
3-element iterator of Vector{Float64}:
[0.0, 0.0]
[0.0, 1.0]
[1.0, 0.0]

This notebook was generated using Literate.jl.