2019-01-13 01:53:16 +08:00
|
|
|
/** \example jkqtplotter_simpletest_functionplot.cpp
|
2019-01-20 23:15:10 +08:00
|
|
|
* Shows how to plot Mathematical Functions as Line Graphs with JKQTPlotter (as evaluated C/C++ functions)
|
2019-01-13 01:53:16 +08:00
|
|
|
*
|
2019-01-20 23:15:10 +08:00
|
|
|
* \ref JKQTPlotterFunctionPlots
|
2019-01-13 01:53:16 +08:00
|
|
|
*/
|
|
|
|
|
2018-12-24 03:27:24 +08:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QVector>
|
|
|
|
#include <QMap>
|
|
|
|
#include "jkqtplotter/jkqtplotter.h"
|
|
|
|
#include "jkqtplotter/jkqtpgraphsevaluatedfunction.h"
|
|
|
|
|
2018-12-24 19:29:33 +08:00
|
|
|
double sinc(double x) {
|
2018-12-24 03:27:24 +08:00
|
|
|
return 10.0*sin(x)/x;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SincSqr {
|
|
|
|
public:
|
|
|
|
inline SincSqr(double amplitude): a(amplitude) {}
|
2018-12-24 19:29:33 +08:00
|
|
|
inline double operator()(double x) {
|
2018-12-24 03:27:24 +08:00
|
|
|
return a*sin(x)*sin(x)/x/x;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
double a;
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
QApplication app(argc, argv);
|
|
|
|
|
|
|
|
// 1. create a window that conatins a line-edit to edit a function
|
2019-01-20 23:15:10 +08:00
|
|
|
// and a JKQTPlotter to display the function, combine everything in a layout
|
2018-12-24 03:27:24 +08:00
|
|
|
QWidget mainWin;
|
2019-01-20 23:15:10 +08:00
|
|
|
JKQTPlotter* plot=new JKQTPlotter(&mainWin);
|
2018-12-24 03:27:24 +08:00
|
|
|
QVBoxLayout* layout=new QVBoxLayout;
|
|
|
|
mainWin.setLayout(layout);
|
|
|
|
layout->addWidget(plot);
|
|
|
|
|
2019-01-20 17:49:29 +08:00
|
|
|
// 2. now we add a JKQTPXFunctionLineGraph object, which will draw a simple function
|
2018-12-24 03:27:24 +08:00
|
|
|
// the function is defined as C++ inline function
|
2019-01-20 17:49:29 +08:00
|
|
|
JKQTPXFunctionLineGraph* func1=new JKQTPXFunctionLineGraph(plot);
|
2018-12-24 19:29:33 +08:00
|
|
|
func1->set_plotFunction([](double x) { return 0.2*x*x-0.015*x*x*x; });
|
2018-12-24 03:27:24 +08:00
|
|
|
func1->set_title("C++-inline function $0.2x^2-0.015x^3$");
|
|
|
|
plot->addGraph(func1);
|
|
|
|
|
2019-01-20 17:49:29 +08:00
|
|
|
// 3. now we add a JKQTPXFunctionLineGraph object, which will draw a simple function
|
2018-12-24 03:27:24 +08:00
|
|
|
// the function is again defined as C++ inline function, but now uses internal
|
|
|
|
// parameters (handed over to the function as a pointer to QVector<double>
|
2019-01-20 17:49:29 +08:00
|
|
|
JKQTPXFunctionLineGraph* func2=new JKQTPXFunctionLineGraph(plot);
|
2018-12-24 03:27:24 +08:00
|
|
|
func2->set_plotFunction([](double x, void* params) {
|
|
|
|
QVector<double>* p=static_cast<QVector<double>*>(params);
|
|
|
|
return p->at(0)*sin(2.0*M_PI*x*p->at(1));
|
|
|
|
});
|
|
|
|
// here we set the parameters p0, p1
|
|
|
|
func2->set_paramsV(5, 0.2);
|
|
|
|
func2->set_title("C++-inline function with int. params $p_0\\cdot\\sin(x*2.0*\\pi\\cdot p_1)$");
|
|
|
|
plot->addGraph(func2);
|
|
|
|
|
2019-01-20 17:49:29 +08:00
|
|
|
// 4. now we add a JKQTPXFunctionLineGraph object, which will draw a simple function
|
2018-12-24 03:27:24 +08:00
|
|
|
// the function is again defined as C++ inline function, but now uses external
|
|
|
|
// parameters, which may have any type (here QMap<QString,double)
|
2019-01-20 17:49:29 +08:00
|
|
|
JKQTPXFunctionLineGraph* func3=new JKQTPXFunctionLineGraph(plot);
|
2018-12-24 03:27:24 +08:00
|
|
|
func3->set_plotFunction([](double x, void* params) {
|
|
|
|
QMap<QString,double>* p=static_cast<QMap<QString,double>*>(params);
|
|
|
|
return p->value("amplitude")*sin(2.0*M_PI*x*p->value("frequency"));
|
|
|
|
});
|
|
|
|
// here we set the parameters p0, p1
|
|
|
|
QMap<QString,double> params3;
|
|
|
|
params3["amplitude"]=-3;
|
|
|
|
params3["frequency"]=0.3;
|
|
|
|
func3->set_params(¶ms3);
|
|
|
|
func3->set_title("C++-inline function with ext. params $p_0\\cdot\\sin(x*2.0*\\pi\\cdot p_1)$");
|
|
|
|
plot->addGraph(func3);
|
|
|
|
|
|
|
|
// 5. of course the function may also be any C+ funtor object:
|
2019-01-20 17:49:29 +08:00
|
|
|
JKQTPXFunctionLineGraph* func4=new JKQTPXFunctionLineGraph(plot);
|
2018-12-24 03:27:24 +08:00
|
|
|
func4->set_plotFunction(SincSqr(-8));
|
|
|
|
func4->set_title("C++ functor $-8*\\sin^2(x)/x^2$");
|
|
|
|
plot->addGraph(func4);
|
|
|
|
|
|
|
|
|
2019-01-20 17:49:29 +08:00
|
|
|
// 6. now we use a JKQTPXFunctionLineGraph to draw a static C function
|
|
|
|
JKQTPXFunctionLineGraph* func5=new JKQTPXFunctionLineGraph(plot);
|
2018-12-24 03:27:24 +08:00
|
|
|
func5->set_plotFunction(&sinc);
|
|
|
|
func5->set_title("static C function $10*\\sin(x)/x$");
|
|
|
|
plot->addGraph(func5);
|
|
|
|
|
2019-01-20 17:49:29 +08:00
|
|
|
// 7. finally JKQTPXFunctionLineGraph defines a small set of common functions
|
|
|
|
JKQTPXFunctionLineGraph* func6=new JKQTPXFunctionLineGraph(plot);
|
|
|
|
func6->setSpecialFunction(JKQTPXFunctionLineGraph::Line);
|
2018-12-24 22:07:14 +08:00
|
|
|
// here we set offset p0=-1 and slope p1=1.5 of the line p0+p1*x
|
2018-12-24 03:27:24 +08:00
|
|
|
func6->set_paramsV(-1,1.5);
|
2018-12-24 22:07:14 +08:00
|
|
|
func6->set_title("special function: linear p_0=-1, p_1=1.5");
|
2018-12-24 03:27:24 +08:00
|
|
|
plot->addGraph(func6);
|
|
|
|
|
2019-01-20 17:49:29 +08:00
|
|
|
// 7. finally JKQTPXFunctionLineGraph defines a small set of common functions
|
|
|
|
JKQTPXFunctionLineGraph* func7=new JKQTPXFunctionLineGraph(plot);
|
|
|
|
func7->setSpecialFunction(JKQTPXFunctionLineGraph::Line);
|
2018-12-24 22:07:14 +08:00
|
|
|
// here we set offset p0=1 and slope p1=-1.5 of the line p0+p1*x by adding these into a column
|
|
|
|
// in the internal datastore and then set that column as parameterColumn for the function graph
|
|
|
|
QVector<double> params;
|
|
|
|
params << /*p0=*/1 << /*p1=*/-1.5;
|
|
|
|
size_t paramCol=plot->getDatastore()->addCopiedColumn(params);
|
|
|
|
func7->set_parameterColumn(paramCol);
|
|
|
|
func7->set_title("special function: linear p_0=1, p_1=-1.5");
|
|
|
|
plot->addGraph(func7);
|
|
|
|
|
2018-12-24 03:27:24 +08:00
|
|
|
|
|
|
|
// 8. set some axis properties (we use LaTeX for nice equation rendering)
|
2018-12-28 05:52:00 +08:00
|
|
|
plot->get_xAxis()->set_axisLabel(QObject::tr("x-axis"));
|
|
|
|
plot->get_yAxis()->set_axisLabel(QObject::tr("y-axis"));
|
2019-01-20 17:49:29 +08:00
|
|
|
plot->get_plotter()->set_keyPosition(JKQTPKeyOutsideBottomLeft);
|
2018-12-24 03:27:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
// 4. scale the plot so the graph is contained
|
|
|
|
plot->setXY(-10,10,-10,10);
|
|
|
|
|
|
|
|
// show window and make it a decent size
|
|
|
|
mainWin.show();
|
|
|
|
mainWin.resize(800,800);
|
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
}
|