#include "library.h" double f(double x) { return cos(x*x*x/1000000.0)/(x+1.0)*10000.0+250.0; } double g(double x) { return sin(x*x/10000.0)/(x+1.0)*10000.0+250.0; } // plot the graph of y=func(x) with x ranging from xmin to xmax. // if the range of x (xmax - xmin) is very small, we could just // draw a straight line and nobody would know the difference. // Otherwise divide and conquer: find the middle x value then // draw the two halves, one where x ranges from the minimum to // the middle, and one where x ranges from the middle to the // maximum. void plot(double func(double x), double xmin, double xmax, double fineness) { if (xmax-xmin < fineness) { const double ymin = func(xmin), ymax = func(xmax); move_to(xmin, ymin); draw_to(xmax, ymax); } else { const double xmid = (xmin+xmax)/2; plot(func, xmid, xmax, fineness); plot(func, xmin, xmid, fineness); } } void main() { make_window(500, 500); set_pen_width(2); set_pen_color(color::red); plot(f, 0.0, 500.0, 0.01); set_pen_color(color::blue); plot(g, 0.0, 500.0, 0.01); }