Visualize.Polygon (Visualize v0.2.25)

Copy Markdown View Source

Utilities for working with polygons.

Provides functions for computing polygon properties like area, centroid, perimeter, and convex hull.

Examples

polygon = [{0, 0}, {100, 0}, {100, 100}, {0, 100}]

Visualize.Polygon.area(polygon)      # => 10000.0
Visualize.Polygon.centroid(polygon)  # => {50.0, 50.0}
Visualize.Polygon.perimeter(polygon) # => 400.0

# Convex hull of scattered points
points = [{0, 0}, {50, 50}, {100, 0}, {100, 100}, {0, 100}]
Visualize.Polygon.hull(points)  # => [{0, 0}, {100, 0}, {100, 100}, {0, 100}]

Summary

Functions

Computes the signed area of a polygon.

Computes the bounding box of a polygon.

Computes the centroid (center of mass) of a polygon.

Tests if a point is inside a polygon.

Computes the convex hull of a set of points.

Computes the length of a polyline (open path).

Computes the perimeter (total edge length) of a polygon.

Types

point()

@type point() :: {number(), number()}

polygon()

@type polygon() :: [point()]

Functions

area(polygon)

@spec area(polygon()) :: float()

Computes the signed area of a polygon.

Returns a positive value for counter-clockwise polygons, negative for clockwise. Use abs/1 if you need the absolute area.

Uses the shoelace formula.

bounds(polygon)

@spec bounds(polygon()) :: {number(), number(), number(), number()} | nil

Computes the bounding box of a polygon.

Returns {min_x, min_y, max_x, max_y}.

centroid(polygon)

@spec centroid(polygon()) :: point()

Computes the centroid (center of mass) of a polygon.

Returns the geometric center of the polygon.

contains?(polygon, arg2)

@spec contains?(polygon(), point()) :: boolean()

Tests if a point is inside a polygon.

Uses the ray casting algorithm.

hull(points)

@spec hull([point()]) :: polygon()

Computes the convex hull of a set of points.

Returns the vertices of the convex hull in counter-clockwise order. Uses Andrew's monotone chain algorithm.

path_length(points)

@spec path_length(polygon()) :: float()

Computes the length of a polyline (open path).

Unlike perimeter/1, this doesn't close the polygon.

perimeter(polygon)

@spec perimeter(polygon()) :: float()

Computes the perimeter (total edge length) of a polygon.