InitializedDrawRules

Struct InitializedDrawRules 

Source
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: DrawRules

The rewriting rules and assignment of characters to draw actions.

§start: String

The initial string.

Implementations§

Source§

impl InitializedDrawRules

Source

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));
Source

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 stack 0
    • ]: pop location/angle pair off of stack 0
  • 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));
Source

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 stack 0
    • ]: pop location/angle pair off of stack 0
  • 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::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));
Source

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);
Source

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.

Source

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

Source§

fn almost_eq(&self, other: &Self, epsilon: f64) -> bool

Returns true if the distance between objects is less than epsilon.
Source§

impl Clone for InitializedDrawRules

Source§

fn clone(&self) -> InitializedDrawRules

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for InitializedDrawRules

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for InitializedDrawRules

Source§

fn eq(&self, other: &InitializedDrawRules) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryFrom<JSONDrawRules> for InitializedDrawRules

Source§

type Error = LSystemError

The type returned in the event of a conversion error.
Source§

fn try_from(json_input: JSONDrawRules) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl StructuralPartialEq for InitializedDrawRules

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.