Crates.io | polygonical |
lib.rs | polygonical |
version | 0.5.0 |
source | src |
created_at | 2023-01-24 08:56:11.571186 |
updated_at | 2023-02-06 21:55:22.731439 |
description | 2d polygon geometry and operations |
homepage | |
repository | https://github.com/emilyselwood/polygonical |
max_upload_size | |
id | 766523 |
size | 39,715 |
A library for interacting with polygons on a 2d plane.
Rotate a polygon:
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:
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);
Things we want to implement but haven't yet.
Things this library won't do.
In that order
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.