QT C++ Graphics Programming Video Tutorial|Damped Harmonic Oscillator
Rate this article
5.00 out of 5
In This Video Tutorial we will show you how to Develop a Damped Harmonic Video simulation program in QT C++..

What is Damped Harmonic Oscillator

Damped harmonic oscillator is an Oscillator system where the amplitude of the oscillation decreases as time pass by. Most of the oscillatory system which we see in real life say simple pendulum,  LCR Circuit , flywheel rotation swing, all have this property. The cause of damping might be several aspects depending upon the physical system under investigation which includes air resistance, inductive or capacitive forces , friction etc.
 
Watch the video from the link below..




Main Source Code

Multithread.h

#ifndef MULTITHREAD_H
#define MULTITHREAD_H

#include <QMainWindow>
 #include <QThread>
#include <QObject>
#include <QMutex>
#include <QWaitCondition>
//#include<mainwindow.h>

class MultiThread : public QObject
{
    Q_OBJECT
public:

     MultiThread(QObject *parent = 0);
     void requestThread();
       void abort();
    void resume();


private:

    bool _abort;
    bool _working;
    QMutex mutex;
    QWaitCondition pauseContd;
     //MainWindow *mainwindow;


signals:
    void setChange();
    void multithreadRequested();

    void valueChanged(const QString &value);

    void finished();
    void resumed();

public slots:

    void doMultiThraed();

};
#endif // MULTITHREAD_H

Multithread.cpp

#include "Multithread.h"
#include <QTimer>
#include <QEventLoop>
#include "mainwindow.h"
#include <QThread>
#include <QDebug>

MultiThread::MultiThread(QObject *parent) :
    QObject(parent)
{
    _working =false;
    _abort = false;

}

void MultiThread::requestThread()
{
    mutex.lock();
    _working = true;
    _abort = false;
    qDebug()<<thread()->currentThreadId();
    mutex.unlock();

    emit multithreadRequested();
}

void MultiThread::abort()
{
    mutex.lock();
    if (_working) {
        _abort = true;
        qDebug()<<thread()->currentThreadId();
    }
    mutex.unlock();
}
void MultiThread::resume()
{
mutex.lock();
if(!_working)
{
_abort = true;
qDebug()<<thread()->currentThreadId();
}
mutex.unlock();
pauseContd.wakeAll();
emit resume();


}

void MultiThread::doMultiThraed()
{

    qDebug()<<thread()->currentThreadId();
    long int iCounter=0;
    int jCounter=0;
    while(true)
    {
     iCounter++;
     jCounter++;
 emit setChange();
     //mycode
     if(iCounter==9)
         iCounter=0;
        mutex.lock();
        bool abort = _abort;
        mutex.unlock();

        if (abort) {
            qDebug()<<thread()->currentThreadId();
            break;
        }

        QEventLoop loop;
        QTimer::singleShot(500, &loop, SLOT(quit()));
        loop.exec();
        emit valueChanged(QString::number(iCounter));

    }
    mutex.lock();
    _working = false;
    mutex.unlock();

    qDebug()<<thread()->currentThreadId();

    emit finished();

}

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "Multithread.h"
#include <QThread>
namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
        int m_width;
        int m_Height;
        int m_NoOfRows;
        int m_NoOfCols;
        int m_XOffset; //Offset from which drawing start
        int m_YOffset;
        int m_ZOffset;
        qreal x;
        qreal y;
        qreal amplitude;
        qreal Time;
        qreal timePeriod;
        qreal x1;
        qreal y1;
        int jcounter;
        int flagncount;

    MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    void changeEvent(QEvent *e);
    void paintEvent(QPaintEvent *);
       void Initialize();

private:
    Ui::MainWindow *ui;
    QThread *thread;
        MultiThread *multithread;

private slots:
        void on_actionPAUSE_activated();
        void on_actionSTART_activated();
        void setValue();
    void on_pushButton_2_clicked();
    void on_pushButton_clicked();
};

#endif // MAINWINDOW_H

MainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QtGui>
#include <QPointF>
#include <QPalette>
#include<math.h>

int YAXIS_Y1 = 150;
int YAXIS_Y2 = 700;
int YAXIS_X1 = 50;
int YAXIS_X2 = 50;
//XAXIS
int XAXIS_Y1 = 425;
int XAXIS_Y2 = 425;
int XAXIS_X1 = 50;
int XAXIS_X2 = 750;

int XAXISX1=0;
int XAXISY1=0;
int WIDTH=0;
int HEIGHT=0;
int STARTANGLE=0;
int SPANANGLE=0;
int flag=0;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    Initialize();
    thread=new QThread;
        multithread=new MultiThread;
        //mainwindow=new MainWindow;

        multithread->moveToThread(thread);

        connect(multithread, SIGNAL(valueChanged(QString)), ui->label, SLOT(setText(QString)));
        connect(multithread, SIGNAL(multithreadRequested()), thread, SLOT(start()));
       // connect(multithread, SIGNAL(multithreadRequested()), thread, SLOT(changeTwentyfive()));
        connect(thread, SIGNAL(started()), multithread, SLOT(doMultiThraed()));
        connect(multithread,SIGNAL(setChange()),SLOT(setValue()));

        connect(multithread, SIGNAL(finished()), thread, SLOT(quit()), Qt::DirectConnection);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow ::Initialize()
{
         x=50.0;
         y=425.0;
         amplitude=0.0;
         Time=0.0;
         timePeriod=0;
          x1=x;
          y1=y;
          jcounter=0;
          flagncount=0;
          ui->textEdit_amplitude->setText("60");
}

void MainWindow::paintEvent(QPaintEvent *pEvent)
{

    QWidget::paintEvent(pEvent);
        QPainter painter(this);

        painter.setRenderHint(QPainter::Antialiasing);
        painter.setPen(Qt::red);

        painter.drawText(35,185,"A");
        painter.drawText(35,200,"/");
        painter.drawText(36,203,"|");
        painter.drawText(37,203,"|");
        painter.drawText(38,200," ");
        painter.drawText(35,220,"Y");
        painter.drawText(35,427,"O");

        painter.drawText(650,448,"-> TIME - X axis");


        painter.drawLine(YAXIS_X1,YAXIS_Y1,YAXIS_X2,YAXIS_Y2);
        painter.drawLine(XAXIS_X1,XAXIS_Y1,XAXIS_X2,XAXIS_Y2);


        //HERE WE PASTE OUR CODE
        QPainterPath path;
            path.moveTo(x,y);

            qreal t=0;

            for (int j=0;j<jcounter;j++)
            {
             t=amplitude;
                for(int a=1;a<amplitude;a++)
                 {
                  x1+=timePeriod/t;
                  t--;
                  y1-=1;
                  path.lineTo(x1,y1);
                  path.moveTo(x1,y1);
                 }


                t=1;
                 for(int b=amplitude;b>0;b--)
                 {

                    x1+=timePeriod/t;
                    t++;
                    y1+=1;
                    path.lineTo(x1,y1);
                    path.moveTo(x1,y1);
                 }
             //negative direction
                 t=amplitude;
                 for(int a=1;a<amplitude;a++)
                  {
                   x1+=timePeriod/t;
                   t--;
                   y1+=1;
                   path.lineTo(x1,y1);
                   path.moveTo(x1,y1);
                  }

                 t=1;

                  for(int b=amplitude;b>0;b--)
                  {
                       x1+=timePeriod/t;
                       t++;
                       y1-=1;
                       path.lineTo(x1,y1);
                       path.moveTo(x1,y1);
                  }


        //HERE WE APPLY OUR EQUATION FOR DAMPED HARMONIC SINEWAVE
        //nextamplitude=amplitude*e raised-(bt/2m)
        //m mass of body
        //t-time
        //b-damping constant
                    amplitude-=8;

        //          qreal amp=amplitude*qExp(1/(1*timePeriod)/2*10);
        //          amplitude=abs(amp);
                    flagncount++;


            }

        painter.drawPath(path);

        x=50.0;
        y=425.0;
        amplitude=0.0;
        Time=0.0;
        timePeriod=0;
        x1=x;
        y1=y;
        jcounter=0;
        flagncount=0;

}

void MainWindow :: setValue()
{
    QString var_Amplitude=ui->textEdit_amplitude->toPlainText();
        amplitude=var_Amplitude.toInt();

        //msgBox.setText(var_Amplitude);
        // msgBox.show();

        QString var_Timeperiod=ui->comboBox_timeprd->currentText();
        Time=var_Timeperiod.toInt();
        if(Time==10)
        {
            timePeriod=10/2;
        }else if(Time==20)
        {
            timePeriod=20/2;
        }else if(Time==30)
        {
            timePeriod=30/2;
        }

        QString s=ui->label->text();
        //msgBox.setText(s);
        //msgBox.show();
        int lbl=s.toInt();
    if(lbl==1)
        {

        jcounter=1;
         repaint();
    }else if(lbl==2)
    {


        jcounter=2;
         repaint();


    }else if(lbl==3)
    {

        jcounter=3;
         repaint();

    }else if(lbl==4)
    {


        jcounter=4;
         repaint();

    }else if(lbl==5)
    {


        jcounter=5;
         repaint();

    }else if(lbl==6)
    {

        jcounter=6;
         repaint();

    }else if(lbl==7)
    {


        jcounter=7;
         repaint();

    }else if(lbl==8)
    {
        jcounter=8;
         repaint();

    }else if(lbl==9)
    {
        jcounter=9;
         repaint();

    }


}


void MainWindow::on_pushButton_clicked()
{
        multithread->abort();
       thread->wait();
       multithread->requestThread();
}

void MainWindow::on_pushButton_2_clicked()
{
    multithread->abort();
        thread->wait();
}

void MainWindow::on_actionSTART_activated()
{
    //CODE FOR START
    multithread->abort();
    thread->wait();
    multithread->requestThread();
}

void MainWindow::on_actionPAUSE_activated()
{
    //CODE FOR PAUSING THE THREAD
         multithread->abort();
         thread->wait();
}

main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}




Download

Author :
vimal vs
Joined Date :17-Jun-2014
Articles
Posted
1
Messages
Posted
0

   
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments

TekTipsDownload
GateExam
Academic Projects
TekTipsExperts



 
Site optimized for IE7, 1280 X 768 and above. Copyright © 2010 - 2018 KTS InfoTech
Site Developed Using KTS WebCloud