-
-
Save rust-play/af7dbc91f6c63264746735e7892f48f0 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[derive(Debug, Clone)] | |
| struct Rect { | |
| name: String, | |
| x: f64, | |
| y: f64, | |
| width: f64, | |
| height: f64, | |
| } | |
| fn subdivide_paper(rect: Rect, level: u32, max_level: u32) { | |
| println!( | |
| "{}: Position ({:.1}, {:.1}) | Size: {:.1}mm x {:.1}mm", | |
| rect.name, rect.x, rect.y, rect.width, rect.height | |
| ); | |
| if level >= max_level { | |
| return; | |
| } | |
| let next_level = level + 1; | |
| // If width > height, cut vertically. Otherwise, cut horizontally. | |
| if rect.width > rect.height { | |
| let new_width = rect.width / 2.0; | |
| let left = Rect { | |
| name: format!("A{}", next_level), | |
| x: rect.x, | |
| y: rect.y, | |
| width: new_width, | |
| height: rect.height, | |
| }; | |
| let _right = Rect { | |
| name: format!("A{}", next_level), | |
| x: rect.x + new_width, | |
| y: rect.y, | |
| width: new_width, | |
| height: rect.height, | |
| }; | |
| // Recursively process one side to show the nested fractal path | |
| subdivide_paper(left, next_level, max_level); | |
| // (Optional) process right side if mapping the full grid | |
| } else { | |
| let new_height = rect.height / 2.0; | |
| let top = Rect { | |
| name: format!("A{}", next_level), | |
| x: rect.x, | |
| y: rect.y, | |
| width: rect.width, | |
| height: new_height, | |
| }; | |
| let _bottom = Rect { | |
| name: format!("A{}", next_level), | |
| x: rect.x, | |
| y: rect.y + new_height, | |
| width: rect.width, | |
| height: new_height, | |
| }; | |
| subdivide_paper(top, next_level, max_level); | |
| } | |
| } | |
| fn main() { | |
| // A0 Base dimensions in millimeters | |
| let a0 = Rect { | |
| name: "A0".to_string(), | |
| x: 0.0, | |
| y: 0.0, | |
| width: 841.0, | |
| height: 1189.0, | |
| }; | |
| println!("--- ISO A-Series Fractal Tree ---"); | |
| subdivide_paper(a0, 0, 8); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment