example_offsetting/
example_offsetting.rs

1/*
2
3#[derive(Debug, Default, Clone)]
4pub struct Offsets {
5    pub offset_area: f64,
6    pub perimeter: f64,
7    pub contour: Vec<(f64, f64)>,
8}
9
10#[derive(Default, Debug, Clone)]
11pub struct Polygon {
12    pub edges: Vec<Edge>,
13    pub vertices: HashMap<usize, Vertex>,
14    pub offset_margin: f64,
15}
16
17*/
18
19use polygon_offsetting::draw_svg::draw_svg_offset;
20use polygon_offsetting::Polygon;
21use polygon_offsetting::Offset;
22
23fn example_offsetting() -> Result <(), Box<dyn std::error::Error>> {
24	// The initial polygon must be closed
25	let points: Vec<(f64, f64)> = vec![
26		(0., 0.),
27		(100., 0.),
28		(40., 20.),
29		(100., 20.),
30		(100., 60.),
31		(95., 78.),
32		(55., 40.),
33		(0., 60.),
34		(0.0, 0.0)
35	];
36
37	// The size of our margin offset, if this value is egal to 0 no offsetting will be computed
38	let offset_size: f64 = 12.25;
39	// Tolerance is the arc segments precision in polygon offsetting (rounded corner)
40	let tolerance: f64 = 0.1;
41
42	let mut polygon = Polygon::new(&points, offset_size).map_err(|e| { e })?;
43	let offset: Offset = polygon.offsetting(tolerance).map_err(|e| { e })?;
44
45	println!("Initial contour length: {:?}", points.len());
46	println!("Offset contour length: {:?}", offset.contour.len());
47	println!("Offset area: {:?}", offset.area);
48	println!("Offset perimeter: {:?}", offset.perimeter);
49    
50    draw_svg_offset(
51	    &points,
52	    &offset,
53	    "/examples/svg/",
54        "example_offsetting",
55	).map_err(|e| { 
56	    print!("Error creating offset svg: {:?}", e);
57	    e 
58	})?;
59
60    Ok(())
61}
62
63fn main() {
64	match example_offsetting() {
65		Ok(_offset_polygon) => { println!("Offset Polygon computed"); },
66		Err(e) => { println!("Error Offsetting: {:?}", e); }
67	}
68}
69