CPlot - C++ function plotter

29 Kasım 2008

1.
/**
2.
* CPlot
3.
* Simple function plotter in C++ that allows you to plot a one-variable function on a 2D graph
4.
* simply by passing its equation (in x variable) via argv. It was written using
5.
*
6.
* --> CImg library - For image manipulation
7.
* http://cimg.sourceforge.net
8.
* --> ExprParser library - For math expressions parsing and solving
9.
* http://blacklight.gotdns.org/cgi-bin/nopaste.pl?mode=view&id=1219044900
10.
*
11.
* To compile it after installing these two libraries:
12.
* g++ -o cplot cplot.cpp -lm -lpthread -lX11 -lexprparser
13.
*
14.
* To use it:
15.
* ./cplot "function_to_plot"
16.
*
17.
* Example:
18.
* ./cplot "e^(0.1*x)"
19.
*
20.
* copyleft 2008 by BlackLight
21.
* Released under GNU GPL licence v.3
22.
*/
23.

24.
#include
25.
#include
26.
#include
27.
#include
28.

29.
using namespace std;
30.
using namespace cimg_library;
31.

32.
#define W 400
33.
#define H 400
34.

35.
typedef unsigned char u8;
36.

37.
int main (int argc, char **argv) {
38.
if (!argv[1]) return 1;
39.

40.
CImg img(W,H);
41.
img.fill(0);
42.
u8 white[] = { 255,255,255 };
43.

44.
img.draw_line (W/2,0,W/2,H,white);
45.
img.draw_line (0,H/2,W,H/2,white);
46.
map m;
47.

48.
for (int x=0; x 49.
try {
50.
vector v;
51.
v.push_back((float) x-(W/2));
52.
ExprParser e(argv[1],v);
53.
int y = (H/2) - (int) e.solve();
54.
//img.draw_point(x,y,white);
55.
m.insert(make_pair(x,y));
56.
}
57.

58.
catch (MathException e) {}
59.
}
60.

61.
for (int i=-W/2; i<(W)-1; i++)
62.
img.draw_line (i, m[i], i+1, m[i+1], white);
63.

64.
img.display();
65.
}




http://www.evilsocket.net/?action=nopaste&do=view&key=8497596

0 yorum: