https://github.com/emilyselwood/polygonical
Polygon geometry functions in rust
https://github.com/emilyselwood/polygonical
2d geometry polygons rust
Last synced: 10 months ago
JSON representation
Polygon geometry functions in rust
- Host: GitHub
- URL: https://github.com/emilyselwood/polygonical
- Owner: emilyselwood
- License: mit
- Created: 2023-01-23T18:31:02.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-07T07:29:14.000Z (over 3 years ago)
- Last Synced: 2025-07-28T02:20:06.917Z (10 months ago)
- Topics: 2d, geometry, polygons, rust
- Language: Rust
- Homepage:
- Size: 66.4 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Polygonical
A library for interacting with polygons on a 2d plane.
## Examples
Rotate a polygon:
```rust
use polygonical::polygon::Polygon;
use polygonical::point::Point;
let poly = Polygon::new(vec![
Point::new(0.0, 1.0),
Point::new(1.0, 1.0),
Point::new(1.0, 0.0),
]);
// get the area of the polygon
let area = poly.area();
// rotate the polygon around its own center by 90 degrees
let rotated = poly.rotate_around_center(90.0_f64.to_radians());
println!("area: {} rotated: {}", area, rotated);
```
Create an approximation of a circle:
```rust
use polygonical::polygon::Polygon;
use polygonical::point::Point;
let num_points = 16;
let radius = 2.0;
let center = Point::new(10.0, 20.0);
// Note: we use an integer number of degrees here because rust won't let you iterate over floats like this.
let points = (0..360).step_by(360/num_points)
.map(|a| Point::new(radius, 0.0).rotate((a as f64).to_radians()).translate(¢er))
.collect();
let circle = Polygon::new(points);
let approx_area = circle.area();
let area = std::f64::consts::PI * radius * radius;
println!("area: {} aprox_area: {} difference: {}", area, approx_area, area - approx_area);
```
## Features
* Points
* Polygons
* Bounding boxes
* Translations of points
* Polygons contain points
* Polygon is_self_intersecting
* Polygon area
* Translations of polygons
* Rotations of points
* Rotations of polygons
* Overlap detection
* Contains detection
* Polygon unions
## Wanted Features
Things we want to implement but haven't yet.
* Scale of points
* Scale of polygons
* Polygon subtraction
## Unwanted Features
Things this library won't do.
* 3d Geometry
* Output to things like svg (that is for another library)
* Coordinate system transforms, epsg codes, pixel space to world space etc.
## Design goals
* Correct
* Safe
* Fast
In that order
## Limitations / Warnings
Polygons must be specified with the points going clockwise around them. Several algorithms only work when points are defined
clockwise. There is no detection of this, it will probably cause weird results, it has not been tested.