#include "library.h" struct complex { double real, imag; }; struct point { double x, y; }; const double win_width = 500, win_height = 500; const double range = 5; const double x_scale = 0.5 * win_width / range; const double y_scale = 0.5 * win_height / range; complex point_to_complex(point p) { complex c; c.real = (p.x - win_width/2) / x_scale; c.imag = - (p.y - win_height/2) / y_scale; return c; } point complex_to_point(complex c) { point p; p.x = c.real * x_scale + win_width/2; p.y = (- c.imag * y_scale) + win_height/2; return p; } void print(complex c) { cout << c.real << " + " << c.imag << "i\n"; } complex add(complex a, complex b) { complex c; c.real = a.real + b.real; c.imag = a.imag + b.imag; return c; } complex sub(complex a, complex b) { complex c; c.real = a.real - b.real; c.imag = a.imag - b.imag; return c; } complex mul(complex a, complex b) { complex c; c.real = a.real * b.real - a.imag * b.imag; c.imag = a.real * b.imag + a.imag * b.real; return c; } complex div(complex a, complex b) { complex c; double bottom = b.real * b.real + b.imag * b.imag; if (bottom==0.0) cout << "Error, division by zero!\n"; else { c.real = (a.real * b.real + a.imag * b.imag) / bottom; c.imag = (b.real * a.imag - a.real * b.imag) / bottom; } return c; } double magnitude(complex a) { return sqrt(a.real*a.real + a.imag*a.imag); } void draw_axes() { set_pen_width(1); set_pen_color(color::grey); complex c; point p; for (c.real = -range; c.real <= range; c.real += 1) { c.imag = - range; p = complex_to_point(c); move_to(p.x, p.y); c.imag = range; p = complex_to_point(c); draw_to(p.x, p.y); } for (c.imag = -range; c.imag <= range; c.imag += 1) { c.real = - range; p = complex_to_point(c); move_to(p.x, p.y); c.real = range; p = complex_to_point(c); draw_to(p.x, p.y); } set_pen_color(color::black); move_to(0.0, win_height/2); draw_to(win_width, win_height/2); move_to(win_width/2, 0.0); draw_to(win_width/2, win_height); } void main() { make_window(win_width, win_height); draw_axes(); while (true) { wait_for_mouse_click(); point p; p.x = get_click_x(); p.y = get_click_y(); complex c = point_to_complex(p); print(c); point q = complex_to_point(c); set_pen_width(4); draw_point(q.x, q.y); } } // v = 0; c = point; v = v * v + c