#include "library.h" const double pi = acos(-1.0); double power(const double y, const int p) { if (p==0) return 1; else { const double prev = power(y, p-1); return y*prev; } } double factorial(const int n) { if (n==0) return 1; else { const double prev = factorial(n-1); return n*prev; } } int multiplier(const int n) { if (n%2==0) return 0; else if (n%4==1) return 1; else return -1; } double term(const double x, const int n) { return multiplier(n) * power(x, n) / factorial(n); } double sum_of_terms(const int first, const int last, const double x) { const double first_term = term(x, first); if (first==last) return first_term; const double other_terms = sum_of_terms(first+1, last, x); return first_term + other_terms; } double sine(const double angle, const int terms) { const double x = angle*pi/180; return sum_of_terms(0, terms, x); } const int winw=720, winh=500; void setup() { make_window(winw, winh); set_pen_color(color::green); move_to(0, 1*winh/4); draw_to(winw, 1*winh/4); move_to(0, 2*winh/4); draw_to(winw, 2*winh/4); move_to(0, 3*winh/4); draw_to(winw, 3*winh/4); move_to(1*winw/4, 0); draw_to(1*winw/4, winh); move_to(2*winw/4, 0); draw_to(2*winw/4, winh); move_to(3*winw/4, 0); draw_to(3*winw/4, winh); } void plot_graph(const double min_x, const double max_x, const int num_terms) { if (max_x-min_x <= 1) { const double min_sine = sine(min_x, num_terms); const double max_sine = sine(max_x, num_terms); move_to(min_x, winh/2-min_sine*winh/4); draw_to(max_x, winh/2-max_sine*winh/4); } else { const double mid_x = (min_x+max_x)/2; plot_graph(min_x, mid_x, num_terms); plot_graph(mid_x, max_x, num_terms); } } void plot_many_graphs(const int first_test, const int last_test) { if (first_test<=last_test) { const double redness = (first_test-1.0)/(last_test-1.0); set_pen_color(redness, 0, 1-redness); plot_graph(0, winw, first_test); plot_many_graphs(first_test+1, last_test); } } void main() { setup(); plot_many_graphs(1, 49); }