collision v0.1.0 Collision.Polygon.RegularPolygon

A regular polygon is equiangular and equilateral — all angles and all sides be equal. With enough sides, a regular polygon tends toward a circle.

Summary

Types

t()

A regular polygon is defined by a number of sides, a circumradius, a rotation angle, and a center point {x, y}

Functions

Determine the vertices, or points, of the polygon

Construct a regular polygon from a tuple

Rotate a regular polygon, rotation angle should be radians

Rotate a regular polygon using rotation angle in degrees

Rounds the x and y components of an {x, y} tuple

Translate a polygon in cartesian space

Translate a polygon’s vertices

Types

t :: Collision.Polygon.RegularPolygon.t

A regular polygon is defined by a number of sides, a circumradius, a rotation angle, and a center point {x, y}.

Functions

calculate_vertices(polygon)

Specs

calculate_vertices(t) :: [Collision.Polygon.Vertex.t]

Determine the vertices, or points, of the polygon.

Examples

iex> Collision.Polygon.RegularPolygon.calculate_vertices( …> %Collision.Polygon.RegularPolygon{ …> n_sides: 4, radius: 2, rotation_angle: 0, midpoint: %{x: 2, y: 0} …> }) [{4.0, 0.0}, {2.0, 2.0}, {0.0, 0.0}, {2.0, -2.0}]

from_tuple(arg)
from_tuple(arg, arg2)

Specs

from_tuple({integer, number, number, {number, number}}, atom) :: Collision.Polygon.RegularPolygon.t

Construct a regular polygon from a tuple.

A polygon must have at least three sides.

Example

iex> Collision.Polygon.RegularPolygon.from_tuple({3, 2, 0, {0, 0}}) {:ok, %Collision.Polygon.RegularPolygon{n_sides: 3, radius: 2, rotation_angle: 0.0, midpoint: %Collision.Polygon.Vertex{x: 0, y: 0}}}

rotate_polygon(vertices, radians, rotation_point \\ %{x: 0, y: 0})

Rotate a regular polygon, rotation angle should be radians.

Examples

iex(1)> p = Collision.two_dimensional_polygon(4, 3, 0, {0,0}) %Collision.Polygon.RegularPolygon{midpoint: %Collision.Polygon.Vertex{x: 0, y: 0}, n_sides: 4, radius: 3, rotation_angle: 0.0} iex(2)> v = Collision.Polygon.RegularPolygon.calculate_vertices(p) iex(3)> Collision.Polygon.RegularPolygon.rotate_polygon(v, 3.14) [{-3.0, 0.0}, {0.0, -3.0}, {3.0, 0.0}, {0.0, 3.0}]

rotate_polygon_degrees(vertices, degrees)

Rotate a regular polygon using rotation angle in degrees.

Examples

iex(1)> p = Collision.two_dimensional_polygon(4, 3, 0, {0,0}) %Collision.Polygon.RegularPolygon{midpoint: %Collision.Polygon.Vertex{x: 0, y: 0}, n_sides: 4, radius: 3, rotation_angle: 0.0} iex(2)> vertices = Collision.Polygon.RegularPolygon.calculate_vertices(p) [{3.0, 0.0}, {0.0, 3.0}, {-3.0, 0.0}, {0.0, -3.0}] iex(3)> Collision.Polygon.RegularPolygon.rotate_polygon_degrees(vertices, 180) [{-3.0, 0.0}, {0.0, -3.0}, {3.0, 0.0}, {0.0, 3.0}] iex(4)> Collision.Polygon.RegularPolygon.rotate_polygon_degrees(vertices, 360) [{3.0, 0.0}, {0.0, 3.0}, {-3.0, 0.0}, {0.0, -3.0}]

round_vertices(vertices)

Specs

round_vertices([{number, number}]) :: [{number, number}]

Rounds the x and y components of an {x, y} tuple.

Examples

iex> Collision.Polygon.RegularPolygon.round_vertices([{1.55555555, 1.2222222}]) [{1.55556, 1.22222}]

translate_polygon(p, c)

Translate a polygon in cartesian space.

translate_vertices(polygon_vertices, translation)

Translate a polygon’s vertices.

Examples

iex(1)> p = Collision.two_dimensional_polygon(4, 3, 0, {0,0}) %Collision.Polygon.RegularPolygon{midpoint: %Collision.Polygon.Vertex{x: 0, y: 0}, n_sides: 4, radius: 3, rotation_angle: 0.0} iex(2)> Collision.Polygon.RegularPolygon.translate_polygon(p, %{x: -2, y: 2}) %Collision.Polygon.RegularPolygon{midpoint: %Collision.Polygon.Vertex{x: -2, y: 2}, n_sides: 4, radius: 3, rotation_angle: 0.0}