Vivid.Line (vivid v0.4.5)

Copy Markdown View Source

Represents a line segment between two Points in 2D space.

Example

iex> use Vivid ...> Line.init(Point.init(0,0), Point.init(5,5)) ...> |> to_string() "@@@@@@@@\n" <> "@@@@@@ @\n" <> "@@@@@ @@\n" <> "@@@@ @@@\n" <> "@@@ @@@@\n" <> "@@ @@@@@\n" <> "@ @@@@@@\n" <> "@@@@@@@@\n"

Summary

Functions

Calculates the absolute Y (vertical) distance between the origin and termination points.

Returns true if a line is horizontal.

Create a Line from a two-element list of points.

Create a Line given an origin and termination point.

Calculates straight-line distance between the two ends of the line segment using Pythagoras' Theorem

Returns whether a point is on the line.

Returns the origin (starting) point of the line segment.

Returns the termination (ending) point of the line segment.

Returns true if a line is vertical.

Calculates the absolute X (horizontal) distance between the origin and termination points.

Calculates the X (horizontal) distance between the origin and termination points.

Find the point on the line where it intersects with the specified x axis.

Calculates the Y (vertical) distance between the origin and termination points.

Find the point on the line where it intersects with the specified y axis.

Types

t()

@type t() :: %Vivid.Line{origin: Vivid.Point.t(), termination: Vivid.Point.t()}

Functions

height(line)

@spec height(t()) :: number()

Calculates the absolute Y (vertical) distance between the origin and termination points.

Example

iex> Vivid.Line.init(Vivid.Point.init(1,1), Vivid.Point.init(4,14)) |> Vivid.Line.height
13

horizontal?(line)

@spec horizontal?(t()) :: boolean()

Returns true if a line is horizontal.

Example

iex> use Vivid
...> Line.init(Point.init(10,10), Point.init(20,10))
...> |> Line.horizontal?
true

iex> use Vivid
...> Line.init(Point.init(10,10), Point.init(20,11))
...> |> Line.horizontal?
false

init(list)

@spec init([Vivid.Point.t()]) :: t()

Create a Line from a two-element list of points.

init(origin, termination)

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

Create a Line given an origin and termination point.

Examples

iex> Vivid.Line.init(Vivid.Point.init(1,1), Vivid.Point.init(4,4))
%Vivid.Line{origin: %Vivid.Point{x: 1, y: 1}, termination: %Vivid.Point{x: 4, y: 4}}

length(line)

@spec length(t()) :: number()

Calculates straight-line distance between the two ends of the line segment using Pythagoras' Theorem

Example

iex> Vivid.Line.init(Vivid.Point.init(1,1), Vivid.Point.init(4,5)) |> Vivid.Line.length
5.0

on?(line, point)

@spec on?(t(), Vivid.Point.t()) :: boolean()

Returns whether a point is on the line.

Example

iex> use Vivid ...> Line.init(Point.init(1,1), Point.init(3,1)) ...> |> Line.on?(Point.init(2,1)) true

iex> use Vivid ...> Line.init(Point.init(1,1), Point.init(3,1)) ...> |> Line.on?(Point.init(2,2)) false

origin(line)

@spec origin(t()) :: Vivid.Point.t()

Returns the origin (starting) point of the line segment.

Example

iex> Vivid.Line.init(Vivid.Point.init(1,1), Vivid.Point.init(4,4)) |> Vivid.Line.origin
%Vivid.Point{x: 1, y: 1}

termination(line)

@spec termination(t()) :: Vivid.Point.t()

Returns the termination (ending) point of the line segment.

Example

iex> use Vivid
...> Line.init(Point.init(1,1), Point.init(4,4))
...> |> Line.termination
Vivid.Point.init(4, 4)

vertical?(line)

@spec vertical?(t()) :: boolean()

Returns true if a line is vertical.

Example

iex> use Vivid
...> Line.init(Point.init(10,10), Point.init(10,20))
...> |> Line.vertical?
true

iex> use Vivid
...> Line.init(Point.init(10,10), Point.init(11,20))
...> |> Line.vertical?
false

width(line)

@spec width(t()) :: number()

Calculates the absolute X (horizontal) distance between the origin and termination points.

Example

iex> Vivid.Line.init(Vivid.Point.init(1,1), Vivid.Point.init(14,4)) |> Vivid.Line.width
13

x_distance(line)

@spec x_distance(t()) :: number()

Calculates the X (horizontal) distance between the origin and termination points.

Example

iex> Vivid.Line.init(Vivid.Point.init(14,1), Vivid.Point.init(1,4)) |> Vivid.Line.x_distance
-13

x_intersect(line, x)

@spec x_intersect(t(), integer()) :: Vivid.Point.t() | nil

Find the point on the line where it intersects with the specified x axis.

Example

iex> use Vivid
...> Line.init(Point.init(25, 15), Point.init(5, 2))
...> |> Line.x_intersect(10)
Vivid.Point.init(10, 5.25)

y_distance(line)

@spec y_distance(t()) :: number()

Calculates the Y (vertical) distance between the origin and termination points.

Example

iex> Vivid.Line.init(Vivid.Point.init(1,14), Vivid.Point.init(4,1)) |> Vivid.Line.y_distance
-13

y_intersect(line, y)

@spec y_intersect(t(), integer()) :: Vivid.Point.t() | nil

Find the point on the line where it intersects with the specified y axis.

Example

iex> use Vivid
...> Line.init(Point.init(25, 15), Point.init(5, 2))
...> |> Line.y_intersect(10)
Vivid.Point.init(17.307692307692307, 10)