vivid v0.4.2 Vivid.Polygon View Source

Describes a Polygon as a series of vertices.

Polygon 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(Polygon.init())
...> |> to_string()
"@@@@@@@@@@@@\n" <>
"@@@@@@@ @@@@\n" <>
"@@@@@@@   @@\n" <>
"@@@@@@ @@@ @\n" <>
"@@@@@@ @  @@\n" <>
"@@@@@ @ @@@@\n" <>
"@@@@@  @@@@@\n" <>
"@@@@ @@@@@@@\n" <>
"@@  @@@@@@@@\n" <>
"@ @@@@@@@@@@\n" <>
"@@@@@@@@@@@@\n"

Link to this section Summary

Functions

Remove a vertex from a Polygon

Remove a vertex at a specific index in the Polygon

Turn on or off filling for this polygon

Is the polygon filled or not?

Return the first vertex in the Polygon

Initialize an empty Polygon

Initialize a Polygon from a list of points

Insert a vertex at a specific index in the Polygon

Return the last vertext in the Polygon

Replace a vertex at a specific index in the Polygon

Convert a Polygon into a list of lines joined by the vertices

Link to this section Types

Link to this section Functions

Remove a vertex from a Polygon.

Example

iex> Vivid.Polygon.init([Vivid.Point.init(1,1), Vivid.Point.init(2,2)]) |> Vivid.Polygon.delete(Vivid.Point.init(2,2))
%Vivid.Polygon{vertices: [%Vivid.Point{x: 1, y: 1}]}
Link to this function delete_at(polygon, index) View Source
delete_at(Vivid.Polygon.t, integer) :: Vivid.Polygon.t

Remove a vertex at a specific index in the Polygon.

Example

iex> Vivid.Polygon.init([Vivid.Point.init(1,1), Vivid.Point.init(2,2)]) |> Vivid.Polygon.delete_at(1)
%Vivid.Polygon{vertices: [%Vivid.Point{x: 1, y: 1}]}

Turn on or off filling for this polygon.

Example

iex> use Vivid
...> Polygon.init([Point.init(1,1), Point.init(2,2), Point.init(1,2)])
...> |> Polygon.fill(true)
...> |> Polygon.filled?
true
Link to this function filled?(polygon) View Source
filled?(Vivid.Polygon.t) :: boolean

Is the polygon filled or not?

Example

iex> use Vivid
...> Polygon.init([Point.init(1,1), Point.init(2,2), Point.init(1,2)])
...> |> Polygon.filled?
false

iex> use Vivid
...> Polygon.init([Point.init(1,1), Point.init(2,2), Point.init(1,2)], true)
...> |> Polygon.filled?
true

iex> use Vivid
...> Polygon.init([Point.init(1,1), Point.init(2,2), Point.init(1,2)], false)
...> |> Polygon.filled?
false

Return the first vertex in the Polygon.

Example

iex> Vivid.Polygon.init([Vivid.Point.init(1,1), Vivid.Point.init(2,2)]) |> Vivid.Polygon.first
%Vivid.Point{x: 1, y: 1}

Initialize an empty Polygon.

Example

iex> Vivid.Polygon.init
%Vivid.Polygon{vertices: []}

Initialize a Polygon from a list of points.

Example

iex> Vivid.Polygon.init([Vivid.Point.init(1,1), Vivid.Point.init(1,2), Vivid.Point.init(2,2), Vivid.Point.init(2,1)])
%Vivid.Polygon{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}
]}
Link to this function insert_at(polygon, index, point) View Source

Insert a vertex at a specific index in the Polygon.

Example

iex> Vivid.Polygon.init([Vivid.Point.init(1,1), Vivid.Point.init(2,2)]) |> Vivid.Polygon.insert_at(1, Vivid.Point.init(3,3))
%Vivid.Polygon{vertices: [
  %Vivid.Point{x: 1, y: 1},
  %Vivid.Point{x: 3, y: 3},
  %Vivid.Point{x: 2, y: 2}
]}

Return the last vertext in the Polygon.

Example

iex> Vivid.Polygon.init([Vivid.Point.init(1,1), Vivid.Point.init(2,2)]) |> Vivid.Polygon.last
%Vivid.Point{x: 2, y: 2}
Link to this function replace_at(polygon, index, point) View Source
replace_at(Vivid.Polygon.t, integer, Vivid.Point.t) :: Vivid.Polygon.t

Replace a vertex at a specific index in the Polygon.

Example

iex> Vivid.Polygon.init([Vivid.Point.init(1,1), Vivid.Point.init(2,2), Vivid.Point.init(3,3)]) |> Vivid.Polygon.replace_at(1, Vivid.Point.init(4,4))
%Vivid.Polygon{vertices: [
  %Vivid.Point{x: 1, y: 1},
  %Vivid.Point{x: 4, y: 4},
  %Vivid.Point{x: 3, y: 3}
]}

Convert a Polygon into a list of lines joined by the vertices.

Examples

iex> Vivid.Polygon.init([Vivid.Point.init(1,1), Vivid.Point.init(1,2), Vivid.Point.init(2,2), Vivid.Point.init(2,1)]) |> Vivid.Polygon.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}},
 %Vivid.Line{origin: %Vivid.Point{x: 2, y: 1},
   termination: %Vivid.Point{x: 1, y: 1}}]