These examples illustrate common operations on polyhedra using Polyhedra.jl:
In [1]:
using Polyhedra, CDDLib
Intersection of polyhedra is obtained with the intersect function.
Below we compute the intersection of two randomly generated polygons in V-representation.
In [2]:
P1 = polyhedron(vrep(randn(15, 2)), CDDLib.Library())
Out[2]:
In [3]:
P2 = polyhedron(vrep(randn(15, 2)), CDDLib.Library())
Out[3]:
In [4]:
Pint = intersect(P1, P2)
Out[4]:
While P1 and P2 have been constructed from their V-representation, their H-representation has been computed to build the intersection Pint.
In [5]:
hrepiscomputed(P1), vrepiscomputed(P1)
Out[5]:
In [6]:
hrepiscomputed(P2), vrepiscomputed(P2)
Out[6]:
On the other hand, Pint is constructed from its H-representation hence its V-representation has not been computed yet.
In [7]:
hrepiscomputed(Pint), vrepiscomputed(Pint)
Out[7]:
We can obtain the number of points in the intersection with npoints as follows:
In [8]:
npoints(Pint)
Out[8]:
Note that this triggers the computation of the V-representation:
In [9]:
hrepiscomputed(Pint), vrepiscomputed(Pint)
Out[9]:
We can plot the polygons and their intersection using the plot function. For further plotting options see the Plots.jl documentation.
In [10]:
using Plots
In [11]:
plot(P1, alpha=0.2, color="blue")
plot!(P2, color="red", alpha=0.2)
plot!(Pint, color="yellow", alpha=0.6)
Out[11]:
The binary convex hull operation between two polyhedra is obtained with the convexhull function.
Below we compute the convex hull of the two randomly generated polygons in V-representation from the previous example.
In [12]:
Pch = convexhull(P1, P2)
Out[12]:
In [13]:
npoints(Pch)
Out[13]:
In [14]:
plot(P1, alpha=0.2, color="blue")
plot!(P2, color="red", alpha=0.2)
plot!(Pch, color="green", alpha=0.1)
Out[14]:
Note that the convex hull operation is done in the V-representation so no representation conversion is needed for this operation since P1 and P2 where constructed from their V-representation:
In [15]:
hrepiscomputed(P1), hrepiscomputed(P2), hrepiscomputed(Pint)
Out[15]:
Let us note that the convexhull of a V-representation contains points and rays and represents the convex hull
of the points together with the conic hull of the rays. So, convexhull(P1, P2) does the union of the vertices:
In [2]:
# each of Q1 and Q2 has 15 vertices
Q1 = polyhedron(vrep(randn(15, 2)), CDDLib.Library())
Q2 = polyhedron(vrep(randn(15, 2)), CDDLib.Library())
Out[2]:
In [3]:
# check that their convex hull has 30 vertices
Qch = convexhull(Q1, Q2)
npoints(Qch)
Out[3]:
However, if we want to remove the redundant points we can use removevredundancy!:
In [4]:
removevredundancy!(Qch)
npoints(Qch)
Out[4]: