summaryrefslogtreecommitdiff
path: root/examples/graphics/mandelbrot.zc
diff options
context:
space:
mode:
Diffstat (limited to 'examples/graphics/mandelbrot.zc')
-rw-r--r--examples/graphics/mandelbrot.zc74
1 files changed, 37 insertions, 37 deletions
diff --git a/examples/graphics/mandelbrot.zc b/examples/graphics/mandelbrot.zc
index 04f61b9..3a0662e 100644
--- a/examples/graphics/mandelbrot.zc
+++ b/examples/graphics/mandelbrot.zc
@@ -5,35 +5,37 @@ struct Complex {
im: float,
}
-fn complex_make(re: float, im: float) -> Complex {
- return Complex { re: re, im: im };
-}
+impl Complex {
+ fn new(re: float, im: float) -> Complex {
+ return Complex { re: re, im: im };
+ }
-fn complex_add(a: Complex, b: Complex) -> Complex {
- return Complex { re: a.re + b.re, im: a.im + b.im };
-}
+ fn add(self, b: Complex) -> Complex {
+ return Complex { re: self.re + b.re, im: self.im + b.im };
+ }
-fn complex_mul(a: Complex, b: Complex) -> Complex {
- return Complex {
- re: a.re * b.re - a.im * b.im,
- im: a.re * b.im + a.im * b.re
- };
-}
+ fn mul(self, b: Complex) -> Complex {
+ return Complex {
+ re: self.re * b.re - self.im * b.im,
+ im: self.re * b.im + self.im * b.re
+ };
+ }
-fn complex_abs2(z: Complex) -> float {
- return z.re * z.re + z.im * z.im;
-}
+ fn abs2(self) -> float {
+ return self.re * self.re + self.im * self.im;
+ }
-fn complex_print(z: Complex) {
- println "{z.re}{z.im}i";
+ fn print(self) {
+ println "{self.re}{self.im}i";
+ }
}
fn pick_char(iter: int, max_iter: int, edge_chars: char[], edge_count: int) -> char {
if (iter >= max_iter) { return ' '; }
if (iter <= 0) { return edge_chars[0]; }
- let t: float = ((float)iter) / ((float)max_iter);
- let idx: int = (int)(t * ((float)(edge_count - 1)));
+ let t = (float)iter / (float)max_iter;
+ let idx = (int)(t * (float)(edge_count - 1));
if (idx < 0) { idx = 0; }
if (idx >= edge_count) { idx = edge_count - 1; }
@@ -42,32 +44,30 @@ fn pick_char(iter: int, max_iter: int, edge_chars: char[], edge_count: int) -> c
}
fn main() {
- let width: int = 120;
- let height: int = 40;
- let max_iter: int = 200;
+ let width = 120;
+ let height = 40;
+ let max_iter = 200;
- let edge_chars: char[12] = [ '#', '@', '%', '8', '&', '*', '+', '=', '-', ':', '.', ',' ];
- let edge_count: int = 12;
+ let edge_chars: char[] = [ '#', '@', '%', '8', '&', '*', '+', '=', '-', ':', '.', ',' ];
+ let edge_count = 12;
- let min_re: float = -2.2;
- let max_re: float = 1.0;
- let min_im: float = -1.2;
- let max_im: float = 1.2;
+ let min_re = -2.2;
+ let max_re = 1.0;
+ let min_im = -1.2;
+ let max_im = 1.2;
for y in 0..height {
- let im: float =
- max_im - (max_im - min_im) * ( ((float)y) / ((float)(height - 1)) );
+ let im = max_im - (max_im - min_im) * ((float)y / (float)(height - 1));
for x in 0..width {
- let re: float =
- min_re + (max_re - min_re) * ( ((float)x) / ((float)(width - 1)) );
+ let re = min_re + (max_re - min_re) * ((float)x / (float)(width - 1));
- let c: Complex = complex_make(re, im);
- let z: Complex = complex_make(0.0, 0.0);
+ let c = Complex::new(re, im);
+ let z = Complex::new(0.0, 0.0);
- let iter: int = 0;
- while (iter < max_iter and complex_abs2(z) <= 4.0) {
- z = complex_add(complex_mul(z, z), c);
+ let iter = 0;
+ while (iter < max_iter and z.abs2() <= 4.0) {
+ z = z.mul(z).add(c);
iter += 1;
}