View Source Vivid.Path (vivid v0.4.4)
Describes a path as a series of vertices.
Path implements both the Enumerable
and Collectable
protocols.
Example
iex> use Vivid
...> 0..3
...> |> Stream.map(fn
...> i when rem(i, 2) == 0 -> Point.init(i * 3, i * 4)
...> i -> Point.init(i * 3, i * 2)
...> end)
...> |> Enum.into(Path.init())
...> |> to_string()
"@@@@@@@@@@@@\n" <>
"@@@@@@@ @@@@\n" <>
"@@@@@@@ @@\n" <>
"@@@@@@ @@@ @\n" <>
"@@@@@@ @@@@@\n" <>
"@@@@@ @@@@@@\n" <>
"@@@@@ @@@@@@\n" <>
"@@@@ @@@@@@@\n" <>
"@@ @@@@@@@@\n" <>
"@ @@@@@@@@@@\n" <>
"@@@@@@@@@@@@\n"
Summary
Functions
Remove a vertex from a Path.
Remove a vertex at a specific index in the Path.
Return the first vertex in the Path.
Initialize an empty path.
Initialize a path from a list of points.
Insert a vertex at a specific index in the Path.
Return the last vertex in the Path.
Replace a vertex at a specific index in the Path.
Convert a path into a list of lines joined by the vertices.
Types
@type t() :: %Vivid.Path{vertices: [Vivid.Shape.t()]}
Functions
@spec delete(t(), Vivid.Point.t()) :: t()
Remove a vertex from a Path.
Example
iex> Vivid.Path.init([Vivid.Point.init(1,1), Vivid.Point.init(2,2)]) |> Vivid.Path.delete(Vivid.Point.init(2,2))
%Vivid.Path{vertices: [%Vivid.Point{x: 1, y: 1}]}
Remove a vertex at a specific index in the Path.
Example
iex> Vivid.Path.init([Vivid.Point.init(1,1), Vivid.Point.init(2,2)]) |> Vivid.Path.delete_at(1)
%Vivid.Path{vertices: [%Vivid.Point{x: 1, y: 1}]}
@spec first(t()) :: Vivid.Point.t()
Return the first vertex in the Path.
Example
iex> Vivid.Path.init([Vivid.Point.init(1,1), Vivid.Point.init(2,2)]) |> Vivid.Path.first
%Vivid.Point{x: 1, y: 1}
@spec init() :: t()
Initialize an empty path.
Example
iex> Vivid.Path.init
%Vivid.Path{vertices: []}
@spec init([Vivid.Point.t()]) :: t()
Initialize a path from a list of points.
Example
iex> Vivid.Path.init([Vivid.Point.init(1,1), Vivid.Point.init(1,2), Vivid.Point.init(2,2), Vivid.Point.init(2,1)])
%Vivid.Path{vertices: [
%Vivid.Point{x: 1, y: 1},
%Vivid.Point{x: 1, y: 2},
%Vivid.Point{x: 2, y: 2},
%Vivid.Point{x: 2, y: 1}
]}
@spec insert_at(t(), integer(), Vivid.Point.t()) :: t()
Insert a vertex at a specific index in the Path.
Example
iex> Vivid.Path.init([Vivid.Point.init(1,1), Vivid.Point.init(2,2)]) |> Vivid.Path.insert_at(1, Vivid.Point.init(3,3))
%Vivid.Path{vertices: [
%Vivid.Point{x: 1, y: 1},
%Vivid.Point{x: 3, y: 3},
%Vivid.Point{x: 2, y: 2}
]}
@spec last(t()) :: Vivid.Point.t()
Return the last vertex in the Path.
Example
iex> Vivid.Path.init([Vivid.Point.init(1,1), Vivid.Point.init(2,2)]) |> Vivid.Path.last
%Vivid.Point{x: 2, y: 2}
@spec replace_at(t(), integer(), Vivid.Point.t()) :: t()
Replace a vertex at a specific index in the Path.
Example
iex> Vivid.Path.init([Vivid.Point.init(1,1), Vivid.Point.init(2,2), Vivid.Point.init(3,3)]) |> Vivid.Path.replace_at(1, Vivid.Point.init(4,4))
%Vivid.Path{vertices: [
%Vivid.Point{x: 1, y: 1},
%Vivid.Point{x: 4, y: 4},
%Vivid.Point{x: 3, y: 3}
]}
@spec to_lines(t()) :: [Vivid.Line.t()]
Convert a path into a list of lines joined by the vertices.
Examples
iex> Vivid.Path.init([Vivid.Point.init(1,1), Vivid.Point.init(1,2), Vivid.Point.init(2,2), Vivid.Point.init(2,1)]) |> Vivid.Path.to_lines
[%Vivid.Line{origin: %Vivid.Point{x: 1, y: 1},
termination: %Vivid.Point{x: 1, y: 2}},
%Vivid.Line{origin: %Vivid.Point{x: 1, y: 2},
termination: %Vivid.Point{x: 2, y: 2}},
%Vivid.Line{origin: %Vivid.Point{x: 2, y: 2},
termination: %Vivid.Point{x: 2, y: 1}}]