This repository has been archived on 2023-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
MINDLE/Modules/Graphics/graphics.hpp

99 lines
2.4 KiB
C++

#include <iostream>
#include <SFML/Graphics.hpp>
#include <eigen3/Eigen/Dense>
using namespace sf;
using namespace std;
using namespace Eigen;
#define SIZE_X 1024
#define SIZE_Y 1024
template <typename Ta, typename Tb>
void sfmlDisplay(const string window_name, const MatrixBase<Ta> &x, const MatrixBase<Ta> &y, const MatrixBase<Tb> &theta)
{
/* Let's create a new window ! */
RenderWindow window(VideoMode(SIZE_X, SIZE_Y), window_name);
/* Just a variable to store this length, 'cause we need it in various computations below */
const long int nbPoints = x.cols();
/* One shape for each points of the coordinates, and for each point of our computed regression */
CircleShape *shape = new CircleShape[2 * nbPoints];
if(!shape)
{
cout << "Error during allocation of the shapes array for display result." << endl;
exit(EXIT_FAILURE);
}
const Vector2<unsigned int> window_size = window.getSize();
const long double ratio_x = window_size.x / (long double)nbPoints;
const long double ratio_y = window_size.y / std::abs(y.maxCoeff() - y.minCoeff());
/* Dots for generated coordinates */
int i(0);
for(; i < nbPoints; i++)
{
/* The dot will be red, and with a radius of 20 ! */
shape[i].setRadius(2);
shape[i].setFillColor(Color(255, 0, 0));
shape[i].setPosition(x(0, i) * ratio_x, window_size.y - y(0, i) * ratio_y);
}
/* Dots for our computed regression */
for(long double temp_y(0.0); i < 2 * nbPoints; i++, temp_y = 0.0)
{
/* The dot will be blue, and with a radius of 15 ! */
shape[i].setRadius(1.5);
shape[i].setFillColor(Color(0, 0, 255));
/* Condition if we're doing a simple linear regression (y = a * x), or not */
if(theta.cols() > 1)
{
for(int j(0); j < theta.cols(); j++)
{
temp_y += pow(x(0, i - nbPoints), (long double)j) * theta(0, j);
}
}
else
{
temp_y = x(0, i - nbPoints) * theta(0, 0);
}
shape[i].setPosition(x(0, i - nbPoints) * ratio_x, window_size.y - temp_y * ratio_y);
}
/* Set a low frame rate limit in order to deal with CPU-usage while in the loop below */
window.setFramerateLimit(25);
/* Infinite loop waiting for closing action */
while(window.isOpen())
{
Event event;
while(window.pollEvent(event))
{
if(event.type == Event::Closed)
{
window.close();
}
}
window.clear(Color(255, 255, 255, 255));
for(i = 0; i < 2 * nbPoints; i++)
{
window.draw(shape[i]);
}
window.display();
}
delete[] shape;
}