2013-05-20 19:07:24 -05:00
|
|
|
use std::cast::transmute;
|
|
|
|
use std::from_str::FromStr;
|
|
|
|
use std::libc::{STDOUT_FILENO, c_int, fdopen, fputc};
|
2013-05-24 21:35:29 -05:00
|
|
|
use std::os;
|
2012-12-10 19:32:48 -06:00
|
|
|
|
2013-04-18 23:44:50 -05:00
|
|
|
static ITER: uint = 50;
|
|
|
|
static LIMIT: f64 = 2.0;
|
2012-02-03 19:46:17 -06:00
|
|
|
|
2013-04-18 23:44:50 -05:00
|
|
|
#[fixed_stack_segment]
|
|
|
|
fn main() {
|
|
|
|
unsafe {
|
2013-08-03 18:59:24 -05:00
|
|
|
let w: i32 = FromStr::from_str(os::args()[1]).unwrap();
|
2013-06-04 23:43:41 -05:00
|
|
|
let h = w;
|
2013-04-18 23:44:50 -05:00
|
|
|
let mut byte_acc: i8 = 0;
|
|
|
|
let mut bit_num: i32 = 0;
|
|
|
|
|
2013-07-22 11:04:51 -05:00
|
|
|
printfln!("P4\n%d %d", w as int, h as int);
|
2013-04-18 23:44:50 -05:00
|
|
|
|
|
|
|
let mode = "w";
|
|
|
|
let stdout = fdopen(STDOUT_FILENO as c_int, transmute(&mode[0]));
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for y in range(0i32, h) {
|
2013-04-18 23:44:50 -05:00
|
|
|
let y = y as f64;
|
2013-08-03 11:45:23 -05:00
|
|
|
for x in range(0i32, w) {
|
2013-06-06 20:54:14 -05:00
|
|
|
let mut Zr = 0f64;
|
|
|
|
let mut Zi = 0f64;
|
|
|
|
let mut Tr = 0f64;
|
|
|
|
let mut Ti = 0f64;
|
2013-04-18 23:44:50 -05:00
|
|
|
let Cr = 2.0 * (x as f64) / (w as f64) - 1.5;
|
|
|
|
let Ci = 2.0 * (y as f64) / (h as f64) - 1.0;
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for _ in range(0i32, ITER as i32) {
|
2013-04-18 23:44:50 -05:00
|
|
|
if Tr + Ti > LIMIT * LIMIT {
|
|
|
|
break;
|
2013-02-17 23:48:06 -06:00
|
|
|
}
|
2013-04-18 23:44:50 -05:00
|
|
|
|
|
|
|
Zi = 2.0*Zr*Zi + Ci;
|
|
|
|
Zr = Tr - Ti + Cr;
|
|
|
|
Tr = Zr * Zr;
|
|
|
|
Ti = Zi * Zi;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte_acc <<= 1;
|
|
|
|
if Tr + Ti <= LIMIT * LIMIT {
|
|
|
|
byte_acc |= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bit_num += 1;
|
|
|
|
|
|
|
|
if bit_num == 8 {
|
|
|
|
fputc(byte_acc as c_int, stdout);
|
|
|
|
byte_acc = 0;
|
|
|
|
bit_num = 0;
|
|
|
|
} else if x == w - 1 {
|
|
|
|
byte_acc <<= 8 - w%8;
|
|
|
|
fputc(byte_acc as c_int, stdout);
|
|
|
|
byte_acc = 0;
|
|
|
|
bit_num = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-02-03 21:50:32 -06:00
|
|
|
}
|
|
|
|
}
|
2012-02-03 19:46:17 -06:00
|
|
|
}
|