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

Merge recipes for polygons and add polytopes #602

Merged
merged 4 commits into from
Sep 3, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 20 additions & 91 deletions src/plot_recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,22 @@ julia> plot([B1, B2], 1e-4);
end
end

# =========================================
# Plot recipes for polygons or 2D polytopes
# =========================================
# ==============================
# Plot recipes for 2D polytopes
# ==============================

"""
plot_polygon(P::Union{AbstractPolygon, HPolytope, VPolytope}; ...)
plot_polygon(P::Union{AbstractPolytope}; ...)
Copy link
Member

Choose a reason for hiding this comment

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

without Union (same below)


Plot a polygon or a 2D polytope.
Plot a 2D polytope as the convex hull of its vertices.

### Input

- `P` -- polygon or polytope

### Examples

```jldoctest plotting_polygons
```jldoctest plotting_polytope
julia> using LazySets, Plots;

julia> P = HPolygon([LinearConstraint([1.0, 0.0], 0.6),
Expand All @@ -191,17 +191,18 @@ julia> plot(P);

This recipe also applies if the polygon is given in vertex representation:

```jldoctest plotting_polygons
```jldoctest plotting_polytope
julia> P = VPolygon([[0.6, 0.6], [0.4, 0.6], [0.4, 0.4], [0.6, 0.4]]);

julia> plot(P);

```
"""
@recipe function plot_polygon(P::Union{AbstractPolygon, HPolytope, VPolytope};
@recipe function plot_polytope(P::Union{AbstractPolytope,};
color="blue", label="", grid=true, alpha=0.5)

@assert dim(P) == 2 "this recipe can only be used to plot two-dimensional sets" # for polytopes
# for polytopes
@assert dim(P) == 2 "this recipe can only be used to plot two-dimensional sets"
seriestype := :shape

vlist = transpose(hcat(vertices_list(P)...))
Expand All @@ -211,19 +212,17 @@ julia> plot(P);
end

"""
plot_polygons(P::Union{Vector{<:AbstractPolygon},
Vector{<:HPolytope},
Vector{<:VPolytope}}; ...)
plot_polytopes(P::Vector{<:AbstractPolytope}; ...)

Plot an array of polygons in constraint representation.
Plot an array of 2D polytopes.

### Input

- `P` -- array of polygons in constraint representation
- `P` -- array of polytopes

### Examples

```jldoctest plotting_polygons_vector
```jldoctest plotting_polytopes
julia> using LazySets, Plots;

julia> P1 = HPolygon([LinearConstraint([1.0, 0.0], 0.6),
Expand All @@ -240,23 +239,24 @@ julia> plot([P1, P2]);

```

```jldoctest plotting_polygons_vector
```jldoctest plotting_polytopes
julia> P1 = VPolygon([[0.6, 0.6], [0.4, 0.6], [0.4, 0.4], [0.6, 0.4]]);

julia> P2 = VPolygon([[0.3, 0.3], [0.2, 0.3], [0.2, 0.2], [0.3, 0.2]]);

julia> plot([P1, P2]);

```

### Notes

It is assumed that the given vector of polytopes is two-dimensional.
"""
@recipe function plot_polygons(P::Union{Vector{<:AbstractPolygon},
Vector{<:HPolytope},
Vector{<:VPolytope}};
@recipe function plot_polytopes(P::Vector{<:AbstractPolygon};
seriescolor="blue", label="", grid=true,
alpha=0.5)

# it is assumed that the polytopes are two-dimensional

seriestype := :shape

for Pi in P
Expand Down Expand Up @@ -333,77 +333,6 @@ julia> plot([Singleton(a), Singleton(b), Singleton(c)]);
[Tuple(element(point)) for point in arr]
end

# ============================
# Plot recipes for zonotopes
# ============================

"""
plot_polygon(Z::Zonotope; ...)

Plot a zonotope by enumerating its vertices.

### Input

- `Z` -- zonotope

### Examples

```jldoctest
julia> using LazySets, Plots;

julia> Z = Zonotope(ones(2), 0.2*[[1., 0], [0., 1], [1, 1]]);

julia> plot(Z);

```
"""
@recipe function plot_zonotope(Z::Zonotope;
color="blue", label="", grid=true, alpha=0.5)

seriestype := :shape

# we have to take the convex hull for the shape
vlist = transpose(hcat(vertices_list(Z)...))
(x, y) = vlist[:, 1], vlist[:, 2]

x, y
end

"""
plot_zonotopes(Z::Vector{<:Zonotope}; ...)

Plot an array of zonotopes.

### Input

- `Z` -- linear array of zonotopes

### Examples

```jldoctest
julia> using LazySets, Plots;

julia> Z1 = Zonotope(zeros(2), [[0.6, 0.6], [0.4, 0.6], [0.4, 0.4], [0.6, 0.4]]);

julia> Z2 = Zonotope(zeros(2), [[0.3, 0.3], [0.2, 0.3], [0.2, 0.2], [0.3, 0.2]]);

julia> plot([Z1, Z2]);

```
"""
@recipe function plot_zonotopes(Z::Vector{<:Zonotope};
seriescolor="blue", label="", grid=true,
alpha=0.5)

seriestype := :shape

for Zi in Z
# we have to take the convex hull for the shape
vlist = transpose(hcat(vertices_list(Zi)...))
@series (x, y) = vlist[:, 1], vlist[:, 2]
end
end

# =====================================
# Plot recipes for lines and intervals
# =====================================
Expand Down