1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*

#[derive(Debug, Default, Clone)]
pub struct Offsets {
    pub offset_area: f64,
    pub perimeter: f64,
    pub contour: Vec<(f64, f64)>,
}

#[derive(Default, Debug, Clone)]
pub struct Polygon {
    pub edges: Vec<Edge>,
    pub vertices: HashMap<usize, Vertex>,
    pub offset_margin: f64,
}

*/

use polygon_offsetting::draw_svg::draw_svg_offset;
use polygon_offsetting::Polygon;
use polygon_offsetting::Offset;

fn example_offsetting() -> Result <(), Box<dyn std::error::Error>> {
	// The initial polygon must be closed
	let points: Vec<(f64, f64)> = vec![
		(0., 0.),
		(100., 0.),
		(40., 20.),
		(100., 20.),
		(100., 60.),
		(95., 78.),
		(55., 40.),
		(0., 60.),
		(0.0, 0.0)
	];

	// The size of our margin offset, if this value is egal to 0 no offsetting will be computed
	let offset_size: f64 = 12.25;
	// Tolerance is the arc segments precision in polygon offsetting (rounded corner)
	let tolerance: f64 = 0.1;

	let mut polygon = Polygon::new(&points, offset_size).map_err(|e| { e })?;
	let offset: Offset = polygon.offsetting(tolerance).map_err(|e| { e })?;

	println!("Initial contour length: {:?}", points.len());
	println!("Offset contour length: {:?}", offset.contour.len());
	println!("offset area: {:?}", offset.area);
	println!("offset perimeter: {:?}", offset.perimeter);
    
    draw_svg_offset(
	    &points,
	    &mut offset.clone(),
	    "/examples/svg/".to_string(),
        "example_offsetting".to_string(),
	).map_err(|e| { 
	    print!("Error on creating offset svg: {:?}", e);
	    e 
	})?;

    Ok(())
}

fn main() {
	match example_offsetting() {
		Ok(_offset_polygon) => { println!("Offset Polygon computed"); },
		Err(e) => { println!("Error Offsetting: {:?}", e); }
	}
}