Skip to content

Commit

Permalink
Co-authored-by: francois-deadalus <[email protected]
Browse files Browse the repository at this point in the history
…thub.com>
  • Loading branch information
MathNuba committed Feb 23, 2024
1 parent 5f955d5 commit 65e7816
Show file tree
Hide file tree
Showing 11 changed files with 453 additions and 2 deletions.
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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}/<executable file>",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
30 changes: 29 additions & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,32 @@ path = "./textbox_without_macro.rs"

[[example]]
name = "diamond_with_macro"
path = "./diamond_with_macro.rs"
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"
50 changes: 50 additions & 0 deletions examples/basque_cross_with_macro.rs
Original file line number Diff line number Diff line change
@@ -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();
}
84 changes: 84 additions & 0 deletions examples/basque_cross_without_macro.rs
Original file line number Diff line number Diff line change
@@ -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();
}
1 change: 1 addition & 0 deletions examples/diamond_with_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.]}
),

Expand Down
52 changes: 52 additions & 0 deletions examples/diamond_without_macro.rs
Original file line number Diff line number Diff line change
@@ -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();
}
28 changes: 28 additions & 0 deletions examples/line_with_macro.rs
Original file line number Diff line number Diff line change
@@ -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();
}
33 changes: 33 additions & 0 deletions examples/line_without_macro.rs
Original file line number Diff line number Diff line change
@@ -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();
}
73 changes: 73 additions & 0 deletions examples/optical_effect_with_macro.rs
Original file line number Diff line number Diff line change
@@ -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"
Loading

0 comments on commit 65e7816

Please sign in to comment.