绘制函数图

运行代码:

#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;
}
//-----------------------------------------------------------------------

运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值