pub struct InitializedDrawRules {
pub draw_rules: DrawRules,
pub start: String,
}Expand description
Gives the specification for an L-System, including the intitial string.
Fields§
§draw_rules: DrawRulesThe rewriting rules and assignment of characters to draw actions.
start: StringThe initial string.
Implementations§
Source§impl InitializedDrawRules
impl InitializedDrawRules
Sourcepub fn new(
rewrite_rules: HashMap<char, String>,
assignment: HashMap<char, DrawAction>,
start: String,
) -> Self
pub fn new( rewrite_rules: HashMap<char, String>, assignment: HashMap<char, DrawAction>, start: String, ) -> Self
Creates InitializedDrawRules with the given rewrite rules and rule assignments given as
HashMaps, and the specified start string.
§Example
use std::collections::HashMap;
use l_system_fractals::rules::{
DrawRules,
InitializedDrawRules,
DrawAction,
RewriteRules,
RuleAssignment
};
use l_system_fractals::num_validity::AlmostEq;
let rwr_hm = HashMap::from([
('=', "=.=".to_string()),
('.', "...".to_string())
]);
let assignment_hm = HashMap::from([
('=', DrawAction::DrawForward(1.0)),
('.', DrawAction::MoveForward(1.0))
]);
let start = String::from("=");
let dr1 = InitializedDrawRules::new(rwr_hm.clone(), assignment_hm.clone(), start.clone());
let dr2 = InitializedDrawRules {
draw_rules: DrawRules {
rewrite_rules: RewriteRules(rwr_hm),
assignment: RuleAssignment(assignment_hm)
},
start
};
assert!(dr1.almost_eq(&dr2, 0.001));Sourcepub fn new_simple(
replacement: HashMap<char, String>,
angle_numerator: isize,
angle_denominator: usize,
start: String,
) -> Result<Self, LSystemError>
pub fn new_simple( replacement: HashMap<char, String>, angle_numerator: isize, angle_denominator: usize, start: String, ) -> Result<Self, LSystemError>
Creates a new InitializedDrawRules with the specified replacement rules, angle, and start
string.
The resulting InitializedDrawRules will have the following properties:
-
The following commands are defined:
+: rotate clockwise by angle-: rotate counterclockwise by angle[: push current location/angle pair onto stack0]: pop location/angle pair off of stack0
-
The angle is specified as
PI * angle_numerator / angle_denominator. -
Every other character represents drawing forward a distance of 1.0.
§Example
use std::collections::HashMap;
use std::f64::consts::PI;
use l_system_fractals::rules::{
DrawAction,
DrawRules,
InitializedDrawRules,
RuleAssignment,
RewriteRules
};
use l_system_fractals::num_validity::AlmostEq;
// Koch snowflake
// from Benoit B. Mandelbrot (1983), _The Fractal Geometry of Nautre (Updated and
// Agumented)_, New York: W.H. Freeman and Company, p. 42
let rwr_hm = HashMap::from([('A', "A+A--A+A".into())]);
let angle_numerator: isize = 1;
let angle_denominator: usize = 3;
let start = String::from("+A--A--A");
let dr1 = InitializedDrawRules::new_simple(
rwr_hm.clone(),
angle_numerator,
angle_denominator,
start.clone()
).unwrap();
let rwr = RewriteRules(rwr_hm);
let assignment = RuleAssignment(
HashMap::from([
('A', DrawAction::DrawForward(1.0)),
('+', DrawAction::RotateCW(PI/3.0)),
('-', DrawAction::RotateCW(-PI/3.0)),
('[', DrawAction::Push(0)),
(']', DrawAction::Pop(0))
])
);
let dr2 = InitializedDrawRules {
draw_rules: DrawRules {
rewrite_rules: rwr,
assignment
},
start
};
assert!(dr1.almost_eq(&dr2, 0.001));Sourcepub fn new_advanced(
replacement: HashMap<char, String>,
draw_step_sizes: HashMap<char, f64>,
move_step_sizes: HashMap<char, f64>,
angle_numerator: isize,
angle_denominator: usize,
start: String,
) -> Result<Self, LSystemError>
pub fn new_advanced( replacement: HashMap<char, String>, draw_step_sizes: HashMap<char, f64>, move_step_sizes: HashMap<char, f64>, angle_numerator: isize, angle_denominator: usize, start: String, ) -> Result<Self, LSystemError>
Creates a new InitializedDrawRules with the specified replacement rules, draw and move distances, angle, and start string.
The resulting InitializedDrawRules will have the following properties:
-
The following commands are defined:
+: rotate clockwise by angle-: rotate counterclockwise by angle[: push current location/angle pair onto stack0]: pop location/angle pair off of stack0
-
The angle is specified as
PI * angle_numerator / angle_denominator. -
Other characters are mapped to drawing or moving forward based on the fields
draw_step_sizesandmove_step_sizes. -
Any other character is mapped to
Null.
§Example
use std::collections::HashMap;
use std::f64::consts::PI;
use l_system_fractals::rules::{
DrawAction,
DrawRules,
InitializedDrawRules,
RuleAssignment,
RewriteRules
};
use l_system_fractals::num_validity::AlmostEq;
// Islands
// from Benoit B. Mandelbrot (1983), _The Fractal Geometry of Nautre (Updated and
// Agumented)_, New York: W.H. Freeman and Company, p. 121
let replacement = HashMap::from([
('A', "A+BA-AA-A-AA+B+AA-BA+AA+A+AA-B-AAA".into()),
('B', "BBBBBB".into())
]);
let draw_step_sizes = HashMap::from([('A', 1.0)]);
let move_step_sizes = HashMap::from([('B', 1.0)]);
let angle_numerator: isize = 1;
let angle_denominator: usize = 2;
let start = "A-A-A-A-".to_string();
let dr1 = InitializedDrawRules::new_advanced(
replacement.clone(),
draw_step_sizes,
move_step_sizes,
angle_numerator,
angle_denominator,
start.clone()
).unwrap();
let rwr = RewriteRules(replacement);
let assignment = RuleAssignment(
HashMap::from([
('A', DrawAction::DrawForward(1.0)),
('B', DrawAction::MoveForward(1.0)),
('+', DrawAction::RotateCW(PI/2.0)),
('-', DrawAction::RotateCW(-PI/2.0)),
('[', DrawAction::Push(0)),
(']', DrawAction::Pop(0))
])
);
let dr2 = InitializedDrawRules {
draw_rules: DrawRules {
rewrite_rules: rwr,
assignment
},
start
};
assert!(dr1.almost_eq(&dr2, 0.001));Sourcepub fn get_iteration_paths(
&self,
iterations: usize,
start: Point,
start_angle: f64,
) -> Result<HashMap<usize, Path>, LSystemError>
pub fn get_iteration_paths( &self, iterations: usize, start: Point, start_angle: f64, ) -> Result<HashMap<usize, Path>, LSystemError>
Produces paths for the specified number of iterations, returning the Paths as
HashMaps.
§Example
use std::collections::HashMap;
use std::f64::consts::PI;
use l_system_fractals::rules::InitializedDrawRules;
use l_system_fractals::paths::{Point, Path};
use l_system_fractals::num_validity::AlmostEq;
// Koch curve
// from Benoit B. Mandelbrot (1983), _The Fractal Geometry of Nautre (Updated and
// Agumented)_, New York: W.H. Freeman and Company, p. 42
let dr = InitializedDrawRules::new_simple(
HashMap::from([('A', "A+A--A+A".into())]),
1,
3,
"A".into()
).unwrap();
let ipaths = dr.get_iteration_paths(2, Point::new(1.0, 1.0), 0.0).unwrap();
let p0: Path = vec![Point::new(1.0, 1.0), Point::new(2.0, 1.0)].into();
assert!(ipaths.get(&0).unwrap().almost_eq(&p0, 0.001));
let p1: Path = vec![
Point::new(1.0, 1.0),
Point::new(2.0, 1.0),
Point::new(2.5, 1.0 + 3.0_f64.sqrt() / 2.0),
Point::new(3.0, 1.0),
Point::new(4.0, 1.0)
].into();
assert!(ipaths.get(&1).unwrap().almost_eq(&p1, 0.001));
assert_eq!(ipaths.get(&2).unwrap().0.len(), 17);Sourcepub fn make_iteration_svg(
&self,
iteration: usize,
params: &PlotParameters,
) -> Result<String, LSystemError>
pub fn make_iteration_svg( &self, iteration: usize, params: &PlotParameters, ) -> Result<String, LSystemError>
Creates an SVG of the result of the specified iteration.
The resulting SVG (returned as a String that can be saved into a file) will be resized
so that its width and height are at most max_width and max_height, respectively, and
will be styled with the given fill, stroke and stroke_width.
Sourcepub fn make_combined_svg(
&self,
params: &PlotParameters,
row_len: usize,
iterations: Vec<usize>,
) -> Result<String, LSystemError>
pub fn make_combined_svg( &self, params: &PlotParameters, row_len: usize, iterations: Vec<usize>, ) -> Result<String, LSystemError>
Creates an SVG showing all specified iterations.
The resulting paths will be showing in a grid, where every row has length row_len.
The resulting SVG (returned as a String that can be saved into a file) will be resized
so that its width and height are at most max_width and max_height, respectively, and
will be styled with the given fill, stroke and stroke_width.
Trait Implementations§
Source§impl AlmostEq for InitializedDrawRules
impl AlmostEq for InitializedDrawRules
Source§impl Clone for InitializedDrawRules
impl Clone for InitializedDrawRules
Source§fn clone(&self) -> InitializedDrawRules
fn clone(&self) -> InitializedDrawRules
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more