I created an application with Qt framework, and I used QPainter
to plot some graphs.
I can plot the exp function, and I want to plot the numerical solution of y'=y
differential equation (Euler method), which is exp(x)
, and it seems that they are drawn correctly, but when I try to resize the window, the exp function remains there, but the approximation disappears for some reason.
What can be the problem?
(I know the code is quite ugly because now I have put everything in the header)
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QPointF>
#include <math.h>
#include <QWidget>
#include <QPainter>
#include <QVector>
#include <cmath>
#include <QImage>
#include <Qt>
#include <QPen>
class Plot : public QWidget
{
public:
Plot() {}
void init()
{
QPainter painter(this);
QPen my_pen;
my_pen.setStyle(Qt::SolidLine);
my_pen.setWidth(5);
my_pen.setBrush(Qt::blue);
my_pen.setColor(Qt::blue);
my_pen.setCapStyle(Qt::RoundCap);
my_pen.setJoinStyle(Qt::RoundJoin);
painter.setPen(my_pen);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(width()/2.0f,height()/2.0f);
painter.scale(1/10.0f,-1/10.0f);
//DRAWING THE COORDINATE SYSTEM
painter.drawLine(QPointF(-m_width/2.0f,0.0f),QPointF(m_width/2.0f,0.0f));
painter.drawLine(QPointF(0.0f,-m_height/2.0f),QPointF(0.0f,m_height/2.0f));
//SPLITTING Y AND X ASIS TO UNITS
for(int i = -20/2.0f; i <= 20/2.0f; ++i)
{
painter.drawLine(QPointF(i*m_unit,-10),QPointF(i*m_unit,10));
}
for(int i = -20/2.0f; i <= 20/2.0f; ++i)
{
painter.drawLine(QPointF(-10,i*m_unit),QPointF(10,i*m_unit));
}
//DRAWING EXP(X) FUNCTION
for(int i = -m_number_of_points/2; i < m_number_of_points/2; ++i)
{
m_point_array[i+m_number_of_points/2] = QPointF(i,m_unit*exp(i/m_unit));
}
for(int i = 0; i < m_number_of_points; ++i)
{
painter.drawPoint(m_point_array[i]);
}
//DRAWING THE NUMERICAL APPROXIMATION OF THE SOLUTION OF y'=y DIFF. EQ, WHICH IS exp(x)
for(int i = 0; i < m_number_of_basepoints; ++i)
{
m_approx_point_array[i] = QPointF(m_unit*((i+1)*m_step+m_first_x_coord),m_unit*(m_initial_condition + m_step*derived_func(m_initial_condition)));
m_initial_condition = m_initial_condition + m_step*derived_func(m_initial_condition);
}
my_pen.setColor(Qt::red);
painter.setPen(my_pen);
painter.drawLine(QPointF(m_unit*m_first_x_coord,m_unit*m_xxx),QPointF(m_approx_point_array[0]));
for(int i = 0; i < m_number_of_basepoints-1; ++i)
{
painter.drawLine(m_approx_point_array[i],m_approx_point_array[i+1]);
}
}
void paintEvent(QPaintEvent*)
{
init();
}
float derived_func(float param)
{
return param;
}
//INITIALIZING THE VALUES FOR COORDINATE SYSTEM
float m_ratio_x = 1/10.0f;
float m_ratio_y = -1/10.0f;
float m_width = width() * (1.0f/m_ratio_x);
float m_height = height() * (1.0f/m_ratio_y);
float m_unit = 300.0f;
//INITIALIZING THE VALUES FOR DRAWING exp(x)
int m_number_of_points = m_width;
QPointF* m_point_array = new QPointF[m_number_of_points];
//INITIALIZING THE VALUES FOR APPROXIMATION
int m_number_of_basepoints = 1000;
QPointF* m_approx_point_array = new QPointF[m_number_of_basepoints];
float m_first_x_coord = -5.0f;
float m_initial_condition = 1/exp(5);
float m_last_x_coord = 2.0f;
float m_step = (m_last_x_coord - m_first_x_coord)/m_number_of_basepoints;
float m_xxx = m_initial_condition;
};
#endif
Can't know for sure but I suppose that most likely it is caused by m_initial_condition
variable being modified on each calculation. As a good way to diagnose such problems I would propose to add const
to variables which should not actually be changed.