fractal_gen/fractal.rs
1use std::io::Write;
2use crate::image::{Img, pixel::Pixel};
3mod circle;
4use circle::{circle, multiple_circles};
5mod line;
6mod koch_curve;
7use koch_curve::{koch_curve};
8mod sierpinski_triangle;
9use sierpinski_triangle::{triangle, trianglev2, sierpinski_triangle, multiple_triangles};
10mod mandelbrot;
11use mandelbrot::mandelbrot;
12mod tree;
13use tree::tree;
14mod barnsley_fern;
15use barnsley_fern::barnsley_fern;
16mod julia;
17use julia::{julia, julia_colored};
18
19
20/// The structure of the Fractal.
21pub struct Fractal {
22 /// The fractal's image data.
23 pub image: Img
24}
25
26
27impl Fractal {
28
29
30 /// Create a Fractal by giving the pixels as a 2D Vector.
31 ///
32 /// # Example
33 ///
34 /// ```
35 /// let mut image = Fractal::new(pixels);
36 /// ```
37 pub fn new(pixels: Vec<Vec<Pixel>>) -> Fractal {
38 let image = Img::new(pixels);
39 Fractal {
40 image: image
41 }
42 }
43
44
45
46 /// Write the image as a bmp file in the given path.
47 ///
48 /// # Example
49 ///
50 /// ```
51 /// let image = image::Img::new(pixels);
52 /// image.write_image("./myimage.bmp");
53 /// ```
54 pub fn write_image(&self, path: &str){
55 if !path.contains(".bmp") {
56 panic!("I am not a bmp image!");
57 }
58 let width = self.image.pixels[0].len();
59 for row in self.image.pixels.iter(){
60 if row.len() != width {
61 panic!("The pixel array does not have equal row sizes!")
62 }
63 }
64 let mut data = std::fs::File::create(path).unwrap();
65 let img = Img::new(self.image.pixels.clone());
66 let bdata = img.get_binary_data();
67 data.write(&bdata).unwrap();
68 }
69
70
71 /// Generate a circle by giving the center coordinates, the radius, and the color.
72 pub fn circle(&mut self, xc: usize, yc: usize, radius: u32, color: Pixel){
73 circle(xc, yc, radius, &mut self.image, color);
74 }
75
76
77 /// Generate repeating circle patterns where each circle's radius is decremented by number.
78 ///
79 /// # Example
80 ///
81 /// ```
82 /// image.multiple_circles(825, 825, 125, 6, Pixel::new(0, 150, 150));
83 /// ```
84 pub fn multiple_circles(&mut self, xc: usize, yc: usize, radius: u32, number: u32, color: Pixel){
85 multiple_circles(xc, yc, radius, number, &mut self.image, color);
86 }
87
88
89 /// Generate a Koch Curve by giving the start and end coordinates, the color, and the amount of recursion.
90 /// Keep in mind that the time to run increases significantly as amount increases.
91 ///
92 /// # Example
93 ///
94 /// ```
95 /// image.koch_curve(675, 75, 925, 325, 5, Pixel::new(0, 250, 0));
96 /// ```
97 pub fn koch_curve(&mut self, p1x: i32, p1y: i32, p2x: i32, p2y: i32, amount: u32, color: Pixel){
98 koch_curve(p1x, p1y, p2x, p2y, amount as i32, &mut self.image.pixels, color);
99 }
100
101
102 /// Generate a triange by giving its coordinates, height and color.
103 pub fn triangle(&mut self, x: u32, y: u32, h: u32, color: Pixel){
104 triangle(x, y, h, &mut self.image.pixels, color);
105 }
106
107
108 /// Generate a triange by giving its coordinates, height and color.
109 pub fn trianglev2(&mut self, x: u32, y: u32, h: u32, color: Pixel){
110 trianglev2(x, y, h, &mut self.image.pixels, color);
111 }
112
113
114 /// Generate a Sierpinksi Triange by giving its coordinates, height, and color.
115 pub fn sierpinski_triangle(&mut self, x: u32, y: u32, h: u32, color: Pixel){
116 trianglev2(x, y, h * 2, &mut self.image.pixels, color);
117 sierpinski_triangle(x as f64, y as f64, h as f64, &mut self.image.pixels, color);
118 }
119
120
121 /// Generate repeating triangles where each triangles height is decremented by number.
122 ///
123 /// # Example
124 ///
125 /// ```
126 /// image.multiple_triangles(175, 800, 220, 6, Pixel::new(250, 0, 250));
127 /// ```
128 pub fn multiple_triangles(&mut self, x: u32, y: u32, h: u32, number: u32, color: Pixel){
129 multiple_triangles(x, y, h, number, &mut self.image.pixels, color);
130 }
131
132
133 /// Generate a Mandelbrot Set with the given color.
134 ///
135 /// # Example
136 ///
137 /// ```
138 /// image.mandelbrot(Pixel::new(250, 0, 0));
139 /// ```
140 pub fn mandelbrot(&mut self, color: Pixel){
141 mandelbrot(&mut self.image.pixels, color);
142 }
143
144
145 /// Rotate the fractal by 90 degrees.
146 pub fn rotate(&mut self){
147 self.image.rotate();
148 }
149
150
151 /// Generate a tree by giving its coordinates, height, angle, growth, and color.
152 ///
153 /// # Example
154 ///
155 /// ```
156 /// image.tree(100, 800, 100, PI / 4.0, 2, Pixel::new(0, 0, 255));
157 /// ```
158 pub fn tree(&mut self, x: u32, y: u32, h: u32, angle: f64, growth: u32, color: Pixel){
159 tree(x, y, h, angle, growth, &mut self.image.pixels, color);
160 }
161
162 /// Generate a fern by selecting its coordinates, number of iterations, and color.
163 /// The more iterations, the better the shape becomes a fern. A good value is about 5000000.
164 ///
165 /// # Example
166 ///
167 /// ```
168 /// image.barnsley_fern(500, 100, 5000000, Pixel::new(255, 0, 0));
169 /// ```
170 pub fn barnsley_fern(&mut self, x: i32, y: u32, iterations: u32, color: Pixel){
171 barnsley_fern(x as i32 - self.image.pixels.len() as i32, y, iterations, &mut self.image.pixels, color);
172 }
173
174
175 /// Generate a Julia Set.
176 ///
177 /// # Example
178 ///
179 /// ```
180 /// image.julia();
181 /// ```
182 pub fn julia(&mut self){
183 julia(&mut self.image.pixels);
184 }
185
186
187 /// Generate a Julia Set with a mixture of colors given in the pixel parameter.
188 ///
189 /// # Example
190 ///
191 /// ```
192 /// image.julia_colored(Pixel::new(250, 150, 100));
193 /// ```
194 pub fn julia_colored(&mut self, color: Pixel){
195 julia_colored(&mut self.image.pixels, color);
196 }
197}