pub enum JSONDrawRules {
Simple {
replacement: HashMap<char, String>,
angle_numerator: isize,
angle_denominator: usize,
start: String,
},
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,
},
Full {
replacement: HashMap<char, String>,
assignment: HashMap<char, JSONDrawAction>,
start: String,
},
}Expand description
Represents user input from JSON of the L-System rules, for conversion into DrawRules.
Variants§
Simple
Most basic specification syntax for rules.
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::parse_files::{JSONDrawAction, JSONDrawRules};
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 json_string = r#"{
"type": "Simple",
"replacement": {"A": "A+A--A+A"},
"angle_numerator": 1,
"angle_denominator": 3,
"start": "+A--A--A"
}"#;
let json_dr: JSONDrawRules = serde_json::from_str(json_string).unwrap();
let dr1: InitializedDrawRules = json_dr.try_into().unwrap();
let rwr = RewriteRules(HashMap::from([('A', "A+A--A+A".into())]));
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: "+A--A--A".into()
};
assert!(dr1.almost_eq(&dr2, 0.001));Fields
Advanced
A more advanced specification syntax for rules that provides more flexibility.
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_sizes and move_step_sizes.
Any other character is mapped to Null.
§Example
use std::collections::HashMap;
use std::f64::consts::PI;
use l_system_fractals::parse_files::{JSONDrawAction, JSONDrawRules};
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 json_string = r#"{
"type": "Advanced",
"replacement": { "A": "A+BA-AA-A-AA+B+AA-BA+AA+A+AA-B-AAA", "B": "BBBBBB" },
"draw_step_sizes": { "A": 1.0 },
"move_step_sizes": { "B": 1.0 },
"angle_numerator": 1,
"angle_denominator": 2,
"start": "A-A-A-A-"
}"#;
let json_dr: JSONDrawRules = serde_json::from_str(json_string).unwrap();
let dr1: InitializedDrawRules = json_dr.try_into().unwrap();
let rwr = RewriteRules(HashMap::from([
('A', "A+BA-AA-A-AA+B+AA-BA+AA+A+AA-B-AAA".into()),
('B', "BBBBBB".into())
]));
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: "A-A-A-A-".into()
};
assert!(dr1.almost_eq(&dr2, 0.001));Fields
draw_step_sizes: HashMap<char, f64>Used to define which characters will represent drawing forward (and how much).
Full
A specification syntax that allows for complete flexibility.
§Example
use std::collections::HashMap;
use std::f64::consts::PI;
use l_system_fractals::parse_files::{JSONDrawAction, JSONDrawRules};
use l_system_fractals::rules::{
DrawAction,
DrawRules,
InitializedDrawRules,
RuleAssignment,
RewriteRules
};
use l_system_fractals::num_validity::AlmostEq;
let json_string = r#"{
"type": "Full",
"replacement": {
"A": "A[A{B<AX",
"B": "BBBB",
"X": "}+A{>+A<]+A["
},
"assignment": {
"A": { "type": "DrawForward", "dist": 1.0 },
"B": { "type": "MoveForward", "dist": 1.0 },
"X": { "type": "Null" },
"[": { "type": "Push", "stack": 0 },
"{": { "type": "Push", "stack": 1 },
"<": { "type": "Push", "stack": 2 },
"]": { "type": "Pop", "stack": 0 },
"}": { "type": "Pop", "stack": 1 },
">": { "type": "Pop", "stack": 2 },
"+": { "type": "RotateCW", "numerator": 2, "denominator": 9 }
},
"start": "+A+A+A+A+A+A+A+A+A"
}"#;
let json_dr: JSONDrawRules = serde_json::from_str(json_string).unwrap();
let dr1: InitializedDrawRules = json_dr.try_into().unwrap();
let rwr = RewriteRules(HashMap::from([
('A', "A[A{B<AX".into()),
('B', "BBBB".into()),
('X', "}+A{>+A<]+A[".into())
]));
let assignment = RuleAssignment(
HashMap::from([
('A', DrawAction::DrawForward(1.0)),
('B', DrawAction::MoveForward(1.0)),
('X', DrawAction::Null),
('+', DrawAction::RotateCW(2.0 * PI/9.0)),
('[', DrawAction::Push(0)),
('{', DrawAction::Push(1)),
('<', DrawAction::Push(2)),
(']', DrawAction::Pop(0)),
('}', DrawAction::Pop(1)),
('>', DrawAction::Pop(2)),
])
);
let dr2 = InitializedDrawRules {
draw_rules: DrawRules {
rewrite_rules: rwr,
assignment
},
start: "+A+A+A+A+A+A+A+A+A".into()
};
assert!(dr1.almost_eq(&dr2, 0.001));Trait Implementations§
Source§impl AlmostEq for JSONDrawRules
impl AlmostEq for JSONDrawRules
Source§impl Clone for JSONDrawRules
impl Clone for JSONDrawRules
Source§fn clone(&self) -> JSONDrawRules
fn clone(&self) -> JSONDrawRules
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more