diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..10efcb2 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug", + "program": "${workspaceFolder}/", + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/examples/Cargo.toml b/examples/Cargo.toml index d7fc6ee..0ef696c 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -96,4 +96,32 @@ path = "./textbox_without_macro.rs" [[example]] name = "diamond_with_macro" -path = "./diamond_with_macro.rs" \ No newline at end of file +path = "./diamond_with_macro.rs" + +[[example]] +name = "diamond_without_macro" +path = "./diamond_without_macro.rs" + +[[example]] +name = "optical_effect_with_macro" +path = "./optical_effect_with_macro.rs" + +[[example]] +name = "optical_effect_without_macro" +path = "./optical_effect_without_macro.rs" + +[[example]] +name = "basque_cross_with_macro" +path = "./basque_cross_with_macro.rs" + +[[example]] +name = "basque_cross_without_macro" +path = "./basque_cross_without_macro.rs" + +[[example]] +name = "line_with_macro" +path = "./line_with_macro.rs" + +[[example]] +name = "line_without_macro" +path = "./line_without_macro.rs" diff --git a/examples/basque_cross_with_macro.rs b/examples/basque_cross_with_macro.rs new file mode 100644 index 0000000..95718ba --- /dev/null +++ b/examples/basque_cross_with_macro.rs @@ -0,0 +1,50 @@ +use std::{f32::consts::PI, fs}; + +use dessin::{nalgebra::Rotation2, prelude::*}; +use dessin_svg::ToSVG; + +fn main() { + let basque_cross: Shape = dessin2!([ + // creates a little circle which serves as references for mouvements + Circle!(radius = 0.01, fill = rgb(255, 0, 0),), + for n in 0..=4 { + dessin2!([ + // creates large half red circle + ThickArc!( + // it starts at an angle of 90° + start_angle = PI / 2_f32, + outer_radius = 20., + inner_radius = 0., + span_angle = PI, + fill = rgb(255, 0, 0), + translate = [0., 20.], + // it rotates of 90° each time + rotate = Rotation2::new(PI * (n as f32) / 2_f32) + ), + // add a small red circle to the second part of the half large circle + Circle!( + radius = 10., + fill = rgb(255, 0, 0), + translate = [0., 30.], + // it rotates of 90° each time + rotate = Rotation2::new(PI * (n as f32) / 2_f32) + ), + //add a small white half circle to the first part of the half large circle + ThickArc!( + // it starts at an angle of 90° + start_angle = PI / 2_f32, + outer_radius = 10., + inner_radius = 0., + span_angle = PI, + fill = rgb(255, 255, 255), + translate = [0., 10.], + // it rotates of 90° each time + rotate = Rotation2::new(PI * (n as f32) / 2_f32) + ) + ]) + } + ]); + + // prints in svg version + fs::write("./out/basque_cross.svg", basque_cross.to_svg().unwrap()).unwrap(); +} diff --git a/examples/basque_cross_without_macro.rs b/examples/basque_cross_without_macro.rs new file mode 100644 index 0000000..237f251 --- /dev/null +++ b/examples/basque_cross_without_macro.rs @@ -0,0 +1,84 @@ +use std::{f32::consts::PI, fs}; + +use dessin::{nalgebra::Rotation2, prelude::*}; +use dessin_svg::ToSVG; + +fn main() { + let circle_point = Circle::default().with_radius(0.01); + + let large_half_circle = ThickArc::default(); + let small_red_circle = Circle::default(); + let little_half_circle = ThickArc::default(); + + // creates a group + let mut group = Group::default(); + + group.shapes = vec![]; + let circle_point = Shape::from(circle_point); + group.shapes.push(circle_point); + for n in 0..=4 { + // creates a large half red circle + let mut large_half_circle = Style::new(large_half_circle.clone()); + + large_half_circle.start_angle(PI / 2_f32); + + large_half_circle.outer_radius(20.); + + large_half_circle.inner_radius(0.); + + large_half_circle.span_angle(PI); + + large_half_circle.fill(rgb(255, 0, 0)); + + large_half_circle.translate([0., 20.]); + + large_half_circle.rotate(Rotation2::new(PI * (n as f32) / 2_f32)); + + // add a small red circle to the second part of the half large circle + let mut small_red_circle = Style::new(small_red_circle.clone()); + + small_red_circle.radius(10.); + + small_red_circle.fill(rgb(255, 0, 0)); + + small_red_circle.translate([0., 30.]); + + small_red_circle.rotate(Rotation2::new(PI * (n as f32) / 2_f32)); + + // creates a little half white circle + let mut little_half_circle = Style::new(little_half_circle.clone()); + + little_half_circle.start_angle(PI / 2_f32); + + little_half_circle.outer_radius(10.); + + little_half_circle.inner_radius(0.); + + little_half_circle.span_angle(PI); + + little_half_circle.fill(rgb(255, 255, 255)); + + little_half_circle.translate([0., 10.]); + + little_half_circle.rotate(Rotation2::new(PI * (n as f32) / 2_f32)); + + + + let large_half_circle = Shape::from(large_half_circle); + let small_red_circle = Shape::from(small_red_circle); + let little_half_circle = Shape::from(little_half_circle); + + // add each figures in the group + + group.shapes.push(large_half_circle); + group.shapes.push(small_red_circle); + group.shapes.push(little_half_circle); + } + + // prints in svg version with Shape::from(...) -> Shape::Group(group) because of the group + fs::write( + "./out/basque_cross.svg", + Shape::Group(group).to_svg().unwrap(), + ) + .unwrap(); +} diff --git a/examples/diamond_with_macro.rs b/examples/diamond_with_macro.rs index fbf3565..82cdad5 100644 --- a/examples/diamond_with_macro.rs +++ b/examples/diamond_with_macro.rs @@ -33,6 +33,7 @@ fn main() { // chooses a rotation of -10 radians in the trigonometric direction rotate={Rotation2::new(-10_f32.to_radians())} + // moves of 15 following the x axis and 5 following the y axis translate={[15.,5.]} ), diff --git a/examples/diamond_without_macro.rs b/examples/diamond_without_macro.rs new file mode 100644 index 0000000..ade51ce --- /dev/null +++ b/examples/diamond_without_macro.rs @@ -0,0 +1,52 @@ +use std::fs; + +use dessin::{prelude::*, shapes}; +use dessin_svg::ToSVG; + +use dessin::nalgebra::Rotation2; + +fn main() { + // here we use the circle as a point to have a reference to use when moving the diamond + let circle = Circle::default().with_radius(0.1); + + // creates a diamond + let diamond = Diamond::default(); + + let mut diamond = Style::new(diamond); + + // chooses a width of 4 for following the x axis + diamond.width(4.); + + // chooses a size of 5 between the origin and the diamond top apex following the y axis + diamond.height_top(5.); + + // chooses a size of 3 between the origin and the diamond bottom apex following the y axis + diamond.height_bottom(3.); + + // paints the inside of the diamond in diamond color + diamond.fill(Fill::Color(rgb(185, 242, 255))); + + // creates a black margin of 0.1 (0.05 outside and 0.05 inside the diamond) + diamond.stroke(Stroke::Full { + color: rgb(0, 0, 0), + width: 0.1, + }); + + // chooses a rotation of -10 radians in the trigonometric direction + diamond.rotate(Rotation2::new(-10_f32.to_radians())); + + // moves of 15 following the x axis and 5 following the y axis + diamond.translate([15., 5.]); + + // transforms circle and diamond into Shape + let circle = Shape::from(circle); + let diamond = Shape::from(diamond); + + // creates a group with diamond and circle + let mut group = Group::default(); + + group.shapes = vec![diamond, circle]; + + // prints in svg version with Shape::from(...) -> Shape::Group(group) because of the group + fs::write("./out/diamond.svg", Shape::Group(group).to_svg().unwrap()).unwrap(); +} \ No newline at end of file diff --git a/examples/line_with_macro.rs b/examples/line_with_macro.rs new file mode 100644 index 0000000..3d4c21e --- /dev/null +++ b/examples/line_with_macro.rs @@ -0,0 +1,28 @@ +use std::fs; + +use dessin::{nalgebra::Point2, prelude::*}; +use dessin_svg::ToSVG; + +fn main() { + let line: Shape = dessin2!([ + // creates a little circle as reference for a movement + Circle!(radius = 0.1), + // creates a line + Line!( + // chooses the starting point of the line + from = Point2::new(1., 0.), + // chooses the ending point of the line + to = Point2::new(12., 5.2), + // not needed here + fill = rgb(255, 100, 100), + stroke = Stroke::Full { + color: rgb(255, 100, 100), + width: 0.05 + }, + translate = [5., 1.] + ) + ]); + + // prints in svg version + fs::write("./out/line.svg", line.to_svg().unwrap()).unwrap(); +} diff --git a/examples/line_without_macro.rs b/examples/line_without_macro.rs new file mode 100644 index 0000000..36c8e72 --- /dev/null +++ b/examples/line_without_macro.rs @@ -0,0 +1,33 @@ +use std::fs; + +use dessin::{nalgebra::Point2, prelude::*}; +use dessin_svg::ToSVG; + +fn main() { + let circle_point = Circle::default().with_radius(0.1); + let line = Line::default(); + + let mut line = Style::new(line); + + line.from(Point2::new(1., 0.)); + + line.to(Point2::new(12., 5.2)); + + line.fill(rgb(255, 100, 100)); + + line.stroke(Stroke::Full { + color: rgb(255, 100, 100), + width: 0.05, + }); + + line.translate([5., 1.]); + + let circle_point = Shape::from(circle_point); + let line = Shape::from(line); + + let mut group = Group::default(); + group.shapes = vec![circle_point, line]; + + // prints in svg version + fs::write("./out/line.svg", Shape::Group(group).to_svg().unwrap()).unwrap(); +} diff --git a/examples/optical_effect_with_macro.rs b/examples/optical_effect_with_macro.rs new file mode 100644 index 0000000..d1e58db --- /dev/null +++ b/examples/optical_effect_with_macro.rs @@ -0,0 +1,73 @@ +use std::{f32::consts::PI, fs}; + +use dessin::{nalgebra::Rotation2, prelude::*}; +use dessin_svg::{SVGOptions, ToSVG}; + +fn main() { + + let optical_effect: Shape = dessin2!([ + + for n in 0..11 { + + dessin2!([ + + ThickArc!( + outer_radius = 10., + + inner_radius= 0., + + span_angle=PI/10_f32, + + fill=rgb(0,0,0), + + // chooses a rotation of (n*PI)/5 radians in the trigonometric direction + rotate = Rotation2::new(PI*(n as f32)/5_f32) + ) + ]) + }, + + Circle!( + // chooses a radius of 10 + radius = 1., + + fill=rgb(255,255,255) + ), + + Rectangle!( + + width=15., + + height=15., + + stroke=Stroke::Full { color: rgb(0, 0, 0), width: 1. } + ), + ]); + + let fond = optical_effect.local_bounding_box(); + + // dbg!(fond.width()); // if we want to know the fond.width size + + //Here we want to create a grey font behind all + let truc = dessin2!([ + + Rectangle!( + width = fond.width(), + + height = fond.height(), + + fill = rgb(150,150,150) + ), + + // Add optical_effect before the new Rectangle + {optical_effect} + ]); + + // prints in svg version + fs::write("./out/optical_effect.svg", truc.to_svg_with_options(SVGOptions{ + viewport:dessin_svg::ViewPort::ManualCentered { width: 14., height: 14. }}).unwrap()).unwrap(); +} + //.to_svg_with_options(SVGOptions{viewport:dessin_svg::ViewPort::ManualCentered permits to choose how we will see the svg + +// Note : +// (1) This solution is not be optimal because we can merge these two renctangles into one. +// (2) This code micht not return what we expect if you use "microsoft edge" but there is no same case with others like "google chrome" or "firefox" \ No newline at end of file diff --git a/examples/optical_effect_without_macro.rs b/examples/optical_effect_without_macro.rs new file mode 100644 index 0000000..74cf0d8 --- /dev/null +++ b/examples/optical_effect_without_macro.rs @@ -0,0 +1,86 @@ +use std::{f32::consts::PI, fs}; + +use dessin::{nalgebra::Rotation2, prelude::*}; +use dessin_svg::{SVGOptions, ToSVG}; + +fn main() { + + let rectangle1 = Rectangle::default(); + + // creates a grey font + let mut rectangle1 = Style::new(rectangle1); + + rectangle1.width(15.); + + rectangle1.height(15.); + + rectangle1.fill(Fill::Color(rgb(150, 150, 150))); + + let rectangle1 = Shape::from(rectangle1); + + // creates a group + let mut group = Group::default(); + + group.shapes = vec![]; + + // add rectangle1 in the group at first to let it be the font + group.shapes.push(Shape::from (rectangle1)); + + // creates the optical effect + let optical_effect = ThickArc::default(); + + for n in 0..11 { + + let mut optical_effect = Style::new(optical_effect.clone()); + + optical_effect.outer_radius(10.); + + optical_effect.inner_radius(0.); + + optical_effect.span_angle(PI/10_f32); + + // paints the inside of the thick arc in black + optical_effect.fill(Fill::Color(rgb(0, 0, 0))); + + // chooses a rotation of (n*PI)/5 radians in the trigonometric direction + optical_effect.rotate(Rotation2::new(PI*(n as f32)/5_f32)); + + // add the nth optical effect in the group + group.shapes.push(Shape::from (optical_effect)); + } + + let rectangle2 = Rectangle::default(); + + // creates a rectangle for the border + let mut rectangle2 = Style::new(rectangle2); + + rectangle2.width(15.); + + rectangle2.height(15.); + + rectangle2.stroke(Stroke::Full { color: rgb(0, 0, 0), width: 1. }); + + // creates a white circle in the middle + let circle = Circle::default().with_radius(1.); + + let mut circle = Style::new(circle); + + circle.fill(rgb(255,255,255)); + + // transforms rectangle2 and circle into Shape + let rectangle2 = Shape::from(rectangle2); + let circle = Shape::from(circle); + + group.shapes.push(Shape::from (rectangle2)); + group.shapes.push(Shape::from (circle)); + + // prints in svg version with Shape::from(...) -> Shape::Group(group) because of the group + fs::write("./out/optical_effect.svg", Shape::Group(group).to_svg_with_options(SVGOptions{ + viewport:dessin_svg::ViewPort::ManualCentered { width: 14., height: 14. }}).unwrap()).unwrap(); +} + //.to_svg_with_options(SVGOptions{viewport:dessin_svg::ViewPort::ManualCentered permits to choose how we will see the svg + + +// Note : +// (1) This solution is not be optimal because we can merge these two renctangles into one. +// (2) This code micht not return what we expect if you use "microsoft edge" but there is no same case with others like "google chrome" or "firefox" \ No newline at end of file diff --git a/examples/out/diamond.svg b/examples/out/diamond.svg index b32e9ec..f33dcda 100644 --- a/examples/out/diamond.svg +++ b/examples/out/diamond.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file