Vivid.Polygon (vivid v0.4.5)

Copy Markdown 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"

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

t()

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

Functions

delete(polygon, point)

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

delete_at(polygon, index)

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

fill(polygon, fill)

@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

filled?(polygon)

@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

first(polygon)

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

init()

@spec init() :: t()

Initialize an empty Polygon.

Example

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

init(points)

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

insert_at(polygon, index, point)

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

last(polygon)

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

replace_at(polygon, index, point)

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

to_lines(polygon)

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