Macaw.Draw.Polygon (macaw_draw v0.1.0)

Copy Markdown View Source

Polygon measures (spec/07 §21), copied from visualize's Visualize.Polygon over lists of {x, y} tuples; a polygon is implicitly closed. Geographic polygons, contours and Voronoi cells (spec/09) measure through this module.

iex> square = [{0, 0}, {100, 0}, {100, 100}, {0, 100}]
iex> {Macaw.Draw.Polygon.area(square), Macaw.Draw.Polygon.centroid(square)}
{10000.0, {50.0, 50.0}}

hull/1 is Andrew's monotone chain, O(n log n); every other measure is one pass over the points (visualize read them by position, a square of their count). A list holding a term that is no pair of numbers is {[i], {:type, :point}} at its index.

Summary

Types

A refusal: faults with their paths.

Functions

The signed shoelace area: positive when the vertices wind counter-clockwise in a y-up frame (clockwise on screen, where y points down); 0.0 for fewer than three points.

{min_x, min_y, max_x, max_y}, or nil for [].

The area-weighted centroid: {0.0, 0.0} for [], the point itself for one, the midpoint for two, the vertex mean when |area| < 1.0e-10.

Whether point is inside, by even-odd ray casting; false for fewer than three points. A point exactly on an edge may answer either.

The convex hull by Andrew's monotone chain: counter-clockwise (y-up), collinear points and duplicates removed; two or fewer points are returned unchanged.

The length of the open polyline: no closing edge.

The sum of edge lengths, the closing edge included; 0.0 for fewer than two points.

Types

error()

@type error() :: {:error, [Macaw.Base.Fault.t()]}

A refusal: faults with their paths.

point()

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

polygon()

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

Functions

area(polygon)

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

The signed shoelace area: positive when the vertices wind counter-clockwise in a y-up frame (clockwise on screen, where y points down); 0.0 for fewer than three points.

bounds(polygon)

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

{min_x, min_y, max_x, max_y}, or nil for [].

centroid(polygon)

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

The area-weighted centroid: {0.0, 0.0} for [], the point itself for one, the midpoint for two, the vertex mean when |area| < 1.0e-10.

contains?(polygon, arg2)

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

Whether point is inside, by even-odd ray casting; false for fewer than three points. A point exactly on an edge may answer either.

hull(points)

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

The convex hull by Andrew's monotone chain: counter-clockwise (y-up), collinear points and duplicates removed; two or fewer points are returned unchanged.

path_length(points)

@spec path_length([point()]) :: float() | error()

The length of the open polyline: no closing edge.

perimeter(polygon)

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

The sum of edge lengths, the closing edge included; 0.0 for fewer than two points.