View Source Vivid.Polygon (vivid v0.4.4)

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"

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.

Types

@type t() :: %Vivid.Polygon{fill: boolean(), vertices: [Vivid.Point.t()]}

Functions

@spec delete(t(), Vivid.Point.t()) :: t()

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
@spec delete_at(t(), integer()) :: 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}]}
@spec fill(t(), boolean()) :: t()

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
@spec filled?(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
@spec first(t()) :: Vivid.Point.t()

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}
@spec init() :: t()

Initialize an empty Polygon.

Example

iex> Vivid.Polygon.init
%Vivid.Polygon{vertices: []}
@spec init([Vivid.Point.t()]) :: t()

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
@spec insert_at(t(), integer(), Vivid.Point.t()) :: t()

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}
]}
@spec last(t()) :: Vivid.Point.t()

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
@spec replace_at(t(), integer(), Vivid.Point.t()) :: 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}
]}
@spec to_lines(t()) :: [Vivid.Line.t()]

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}}]