polygon-art/examples/clipping.rs

58 lines
1.4 KiB
Rust
Raw Normal View History

//! Shows how to do clipping.
use geo::polygon;
use geo_clipper::Clipper;
use geo_types::Polygon;
use polygon_art::Context;
fn main() {
// two polygons
let polygon_a: Polygon<f64> = polygon![
( x : 40., y : 34. ),
( x : 200., y : 66. ),
( x : 106., y : 80. ),
( x : 120., y : 175. ),
];
let polygon_b: Polygon<f64> = polygon![
( x: 133., y : 120.),
( x: 80., y : 146.),
( x: 26., y : 106.),
( x: 40., y : 90.),
( x: 0., y : 53.),
( x: 80., y : 66.),
( x: 146., y : 0.),
];
let context = Context::create();
// input polygons
context.translate(2.0, 100.);
context.draw_polygon(&polygon_a);
context.draw_polygon(&polygon_b);
// union
context.translate(250.0, 0.);
let union = &polygon_a.union(&polygon_b, 10.0);
context.draw_multipolygon(union);
// intersection
context.save();
context.translate(0.0, 250.);
let intersection = &polygon_a.intersection(&polygon_b, 10.0);
context.draw_multipolygon(intersection);
context.restore();
// difference A
context.translate(250.0, 0.);
let difference = &polygon_a.difference(&polygon_b, 10.0);
context.draw_multipolygon(difference);
// difference B
context.translate(0., 250.);
let difference = &polygon_b.difference(&polygon_a, 10.0);
context.draw_multipolygon(difference);
context.render();
}