#include "library.h" #include struct Complex { double re, im; Complex(double r, double i) { re = r; im = i; } Complex add(Complex b) { return Complex(re + b.re, im + b.im); } Complex sub(Complex b) { return Complex(re - b.re, im - b.im); } Complex mul(Complex b) { return Complex(re * b.re - im * b.im, re * b.im + im * b.re); } Complex div(Complex b) { return Complex((re * b.re + im * b.im) / (b.re * b.re + b.im * b.im), (im * b.re - re * b.im) / (b.re * b.re + b.im * b.im)); } double magnitude() { return sqrt(re * re + im * im); } void print() { cout << setprecision(15) << re; if (im >= 0) cout << "+"; cout << setprecision(15) << im << "i\n"; } }; struct explorer { int screenw, screenh; double minre, maxre, minim, maxim; explorer(int screensize, double minr, double maxr, double mini, double maxi) { screenw = screenh = screensize; minre = minr; maxre = maxr; minim = mini; maxim = maxi; make_window(screenw, screenh); set_pen_width(2); } double screenx_to_real(double x) { return x / screenw * (maxre - minre) + minre; } double screeny_to_imag(double y) { return y / screenh * (maxim - minim) + minim; } double real_to_screenx(double r) { return (r - minre) / (maxre - minre) * screenw; } double imag_to_screeny(double i) { return (i - minim) / (maxim - minim) * screenh; } void clear() { fill_rectangle(0, 0, screenw, screenh, color::white); } void move_to(Complex c) { ::move_to(real_to_screenx(c.re), imag_to_screeny(c.im)); } void draw_to(Complex c) { ::draw_to(real_to_screenx(c.re), imag_to_screeny(c.im)); } void mark(Complex c) { double x = real_to_screenx(c.re), y = imag_to_screeny(c.im); ::move_to(x-5, y-5); ::draw_to(x+5, y+5); ::move_to(x-5, y+5); ::draw_to(x+5, y-5); } Complex complex_from_x_y(double x, double y) { return Complex(screenx_to_real(x), screeny_to_imag(y)); } }; void main() { explorer E(600, -2, 2, -2, 2); // explorer E(600, -0.167, -0.067, -0.913, -0.827); // explorer E(600, -0.1445, -0.143, -0.8885, -0.8872); // explorer E(600, -0.14337, -0.14333, -0.887967, -0.887928); // explorer E(600, -0.143352733, -0.143352267, -0.88795955, -0.88796015); // explorer E(600, -0.1433525194, -0.1433525124, -0.887959812, -0.887959820); for (int x = 0; x < E.screenw; x += 1) for (int y = 0; y < E.screenh; y += 1) { bool gotbig = false; Complex c = E.complex_from_x_y(x, y); Complex z = c; for (int i = 0; i < 10000; i += 1) { if (z.magnitude() > 5) { gotbig = true; break; } z = z.mul(z).add(c); } if (gotbig) set_pixel_color(x, y, color::red); else set_pixel_color(x, y, color::yellow); } wait_for_mouse_click(); E.complex_from_x_y(get_click_x(), get_click_y()).print(); wait_for_mouse_click(); E.complex_from_x_y(get_click_x(), get_click_y()).print(); }