polygon

Macro polygon 

Source
macro_rules! polygon {
    ($ty:ty, $($line:literal),* $(,)?) => { ... };
}
Expand description

A macro for visually defining polygons using Unicode characters.

This macro allows you to create polygons by marking vertices with the (U+25CF BLACK CIRCLE) character on a 2D grid. Vertices are extracted in reading order (top to bottom, left to right). Each character position represents one unit in the coordinate system, with the origin (0, 0) at the top-left corner.

The macro validates the polygon using Polygon::new, so invalid polygons will panic at runtime.

§Syntax

polygon!(Type, "line1", "line2", ...)
  • Type: The numeric type for coordinates (e.g., i32, i64, f64)
  • Each string literal represents one row of the grid
  • marks vertex positions
  • Other characters (including Unicode box-drawing characters) are ignored

§Coordinate System

  • X-axis: increases from left to right (column index)
  • Y-axis: increases from top to bottom (row index)
  • Origin: (0, 0) at top-left

§Examples

// Simple triangle
let triangle = rgeometry::polygon!(i32,
    "  ●  ",
    " ● ● "
);
assert_eq!(triangle.iter().count(), 3);
// Pentagon with vertices in CCW order
let pentagon = rgeometry::polygon!(i64,
    "   ●   ",
    "      ●",
    "     ● ",
    " ●     ",
    "●      "
);
assert_eq!(pentagon.iter().count(), 5);
// Triangle with decorative box-drawing characters
let triangle = rgeometry::polygon!(i32,
    "●─────┐",
    "│  ●  │",
    "└────●"
);
assert_eq!(triangle.iter().count(), 3);

§Vertex Ordering

Vertices are extracted in reading order (top to bottom, left to right within each row). Place your symbols in the order you want them to appear in the polygon boundary.

§Panics

This macro will panic at runtime if:

  • Fewer than 3 vertices are marked
  • The vertices don’t form a simple polygon (self-intersections, etc.)
  • The polygon is invalid for any reason Polygon::new would reject