-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlot.cpp
More file actions
83 lines (59 loc) · 1.55 KB
/
Plot.cpp
File metadata and controls
83 lines (59 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Plot.cpp
#include "Plot.h"
#include <stdio.h>
Plot* Plot::pInstance = NULL;
Plot* Plot::CreateInstance()
{
if (!pInstance) pInstance = new Plot();
return pInstance;
}
Plot::Plot()
{
this->xmax = 3;
this->ymax = 3;
this->xmin = -this->xmax;
this->ymin = -this->ymax;
gnuplot = popen("gnuplot -geometry 600x600 - > /dev/null 2>&1","w");
setArea();
write("unset key");
write("set format ''");
write("f(x) = -50");
write("plot f(x)");
}
Plot::~Plot()
{
write("set terminal x11 close 0");
write("exit");
pclose(gnuplot);
}
double Plot::getXMin() { return this->xmin; }
double Plot::getXMax() { return this->xmax; }
double Plot::getYMin() { return this->ymin; }
double Plot::getYMax() { return this->ymax; }
void Plot::setXMin(double xmin) { this->xmin = xmin; this->setArea(); }
void Plot::setXMax(double xmax) { this->xmax = xmax; this->setArea(); }
void Plot::setYMin(double ymin) { this->ymin = ymin; this->setArea(); }
void Plot::setYMax(double ymax) { this->ymax = ymax; this->setArea(); }
double Plot::getArea() { return (xmax-xmin)*(ymax-ymin); }
void Plot::setArea()
{
char command[128];
sprintf(command, "set xrange[%f:%f]", this->xmin, this->xmax);
write(command);
sprintf(command, "set yrange[%f:%f]", this->ymin, this->ymax);
write(command);
write("set size square");
write("set size ratio -1");
write("set ticslevel 0");
write("set xtics autofreq 1");
write("set ytics autofreq 1");
}
void Plot::write( const char* command )
{
fprintf(gnuplot, "%s\n", command);
fflush(gnuplot);
}
void Plot::refresh()
{
write("replot");
}