2021-03-30 22:27:12 +02:00
|
|
|
//! 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);
|
2021-04-08 07:13:22 +02:00
|
|
|
context.draw_multipolygon(union);
|
2021-03-30 22:27:12 +02:00
|
|
|
|
|
|
|
// intersection
|
2021-04-10 13:33:56 +02:00
|
|
|
context.save();
|
2021-03-30 22:27:12 +02:00
|
|
|
context.translate(0.0, 250.);
|
|
|
|
let intersection = &polygon_a.intersection(&polygon_b, 10.0);
|
2021-04-08 07:13:22 +02:00
|
|
|
context.draw_multipolygon(intersection);
|
2021-03-30 22:27:12 +02:00
|
|
|
context.restore();
|
|
|
|
|
|
|
|
// difference A
|
|
|
|
context.translate(250.0, 0.);
|
|
|
|
let difference = &polygon_a.difference(&polygon_b, 10.0);
|
2021-04-08 07:13:22 +02:00
|
|
|
context.draw_multipolygon(difference);
|
2021-03-30 22:27:12 +02:00
|
|
|
|
|
|
|
// difference B
|
|
|
|
context.translate(0., 250.);
|
|
|
|
let difference = &polygon_b.difference(&polygon_a, 10.0);
|
2021-04-08 07:13:22 +02:00
|
|
|
context.draw_multipolygon(difference);
|
2021-03-30 22:27:12 +02:00
|
|
|
|
|
|
|
context.render();
|
|
|
|
}
|