运行代码:
#include"std_lib_facilities.h"
#include"GUI/Simple_window.h"
#include"GUI/Graph.h"
#include"GUI/Point.h"
//------------------------------------------------------------------------
double one(double) { return 1; }
double slope(double x) { return x / 2; }
double square(double x) { return x * x; }
double sloping_cos(double x) { return cos(x) + slope(x); }
int main()
try{
const int xmax = 600;
const int ymax = 400;
const int x_orig = xmax / 2;
const int y_orig = ymax / 2;
const Point orig(x_orig, y_orig);
const int r_min = -10;
const int r_max = 11;
const int n_points = 3000;
const int x_scale = 30;
const int y_scale = 30;
Simple_window win(Point(100, 100), xmax, ymax, "Function graphing");
Function s(one, r_min, r_max, orig, n_points, x_scale, y_scale);
Function s2(slope, r_min, r_max, orig, n_points, x_scale, y_scale);
Function s3(square, r_min, r_max, orig, n_points, x_scale, y_scale);
Function s4(cos, r_min, r_max, orig, n_points, x_scale, y_scale);
Function s5(sloping_cos, r_min, r_max, orig,n_points, x_scale, y_scale);
s.set_color(Color::black);
s2.set_color(Color::black);
s3.set_color(Color::black);
s4.set_color(Color::dark_green);
s5.set_color(Color::dark_green);
win.attach(s);
win.attach(s2);
win.attach(s3);
win.attach(s4);
win.attach(s5);
Function f1(log, 0.000001, r_max, orig, n_points, x_scale, y_scale);
Function f2(sin, r_min, r_max, orig, n_points, x_scale, y_scale);
Function f3(cos, r_min, r_max, orig, n_points, x_scale, y_scale);
Function f4(exp, r_min, r_max, orig, n_points, x_scale, y_scale);
f1.set_color(Color::dark_blue);
f2.set_color(Color::dark_blue);
f3.set_color(Color::dark_blue);
f4.set_color(Color::dark_blue);
win.attach(f1);
win.attach(f2);
win.attach(f3);
win.attach(f4);
Text ts(Point(100, y_orig - 40), "one");
Text ts2(Point(100, y_orig + y_orig / 2 - 20), "x/2");
Text ts3(Point(x_orig - 110, 20), "x*x");
ts.set_color(Color::black);
ts2.set_color(Color::black);
ts3.set_color(Color::black);
win.attach(ts);
win.attach(ts2);
win.attach(ts3);
const int xlength = xmax - 40;
const int ylength = ymax - 40;
Axis x(Axis::x, Point(20, y_orig), xlength, xlength / x_scale, "one notch==1");
Axis y(Axis::y, Point(x_orig, ylength + 20), ylength, ylength / y_scale, "one notch==1");
x.label.move(-160, 0);
x.set_color(Color::red);
y.set_color(Color::red);
win.attach(x);
win.attach(y);
win.set_label("Function graphing:label functions");
win.wait_for_button();
}
catch (exception& e) {
cerr << "error:" << e.what() << '\n';
keep_window_open();
return 1;
}
catch (...) {
cerr << "Oops:unknown exception!\n";
keep_window_open();
return 2;
}
//-----------------------------------------------------------------------
运行结果: