philsupertramp/game-math
Loading...
Searching...
No Matches
plot/TestPlot.cpp

This is an example on how to use the Plot, ScatterPlot, FunctionPlot and SurfacePlot classes.

#include "../Test.h"
class PlotTestCase : public Test
{
bool TestPlot() {
Matrix<double> Y = X.Apply([](const double& in) { return in * in; });
LagrangeBase base(X, Y);
Matrix<double> xi = linspace(-3.5, 3.5, 5).Transpose();
auto res = base.Evaluate(xi);
Plot plot("Comparison of approximation with true value");
plot.AddData(HorizontalConcat(X, Y), "support values");
plot.AddData(HorizontalConcat(xi, res), "approximation");
plot();
return true;
}
bool TestScatterPlot() {
Matrix<double> Y = X.Apply([](const double& in) { return in * in; });
ScatterPlot plot2("Test Scatter Plot: 2 Support Values");
plot2.AddData(X, Y, "support values");
plot2.pointSize(5);
plot2.xAxis("X");
plot2.yAxis("Y");
plot2();
return true;
}
bool TestFunctionPlot() {
FunctionPlot plot3([](const double& in) { return in * in; }, "Function Plot: x^2");
plot3.AddData(-5, 5, 0.1, "support values");
plot3.xAxis("X");
plot3.yAxis("Y");
plot3();
return true;
}
bool TestSurfacePlot() {
SurfacePlot plot("Surface Plot Test");
auto dat = ones(100, 3);
plot.AddData(dat, "some data", DataTypes::LINE, nullptr, 3);
plot();
return true;
}
bool TestImagePlot() {
ImagePlot plot("Image Plot: Test");
auto in = zeros(10, 10);
for(size_t i = 0; i < 10; ++i) {
for(size_t j = 0; j < 10; ++j) {
if(i <= 5 && j <= 5) { in(i, j) = 0.5; }
if(i > 5 && j > 5) { in(i, j) = 1.0; }
}
}
plot.AddData(in, "some data");
plot();
return true;
}
public:
virtual void run() {
TestPlot();
TestScatterPlot();
TestFunctionPlot();
TestImagePlot();
TestSurfacePlot();
TestImagePlot();
}
};
int main() { PlotTestCase().run(); }
Definition: Plot.h:368
void AddData(const double &start, const double &end, const double &stepSize, const char *name)
Definition: Plot.h:389
Definition: Plot.h:434
void AddData(const Matrix< double > &mat, const std::string &name)
Definition: Plot.h:451
Definition: SupportValues.h:133
virtual Matrix< double > Evaluate(const Matrix< double > &in) const
Definition: SupportValues.h:173
Definition: Matrix.h:42
constexpr Matrix< T > Transpose() const
Definition: Matrix.h:256
Matrix< T > Apply(const std::function< T(T)> &fun) const
Definition: Matrix.h:375
Definition: Plot.h:104
void AddData(const Matrix< double > &x, const Matrix< double > &y, const std::string &name, DataTypes dataType=DataTypes::NONE, const char *character=nullptr)
Definition: Plot.h:182
void yAxis(const std::string &name)
Definition: Plot.h:155
void xAxis(const std::string &name)
Definition: Plot.h:145
void pointSize(const int &size)
Definition: Plot.h:165
Definition: Plot.h:350
Definition: Plot.h:401
Matrix< T > HorizontalConcat(const Matrix< T > &lhs, const Matrix< T > &rhs)
Definition: matrix_utils.h:81
Matrix< double > ones(size_t rows, size_t columns=1, size_t elements=1)
Matrix< double > zeros(size_t rows, size_t columns, size_t elements=1)
Matrix< double > linspace(double start, double end, unsigned long num_elements)