Blog

Drawing a Sinus in QT

by Thomas Spycher on October 31, 2010

I had to create an application which draws an Sinus in a Window. I needed to create this app in C++ and QT as Framework. So i decided to create my own specialized QWidget class.

sinus.h

#ifndef SINUS_H
#define SINUS_H
 
#include <QWidget>
#include <QPainter>
#include <QPen>
#include <iostream>
#include <cmath>
 
#define PI 3.14159265
 
class Sinus : public QWidget
{
    Q_OBJECT
public:
    explicit Sinus(QWidget *parent = 0);
signals:
 
protected:
    void paintEvent( QPaintEvent* );
public slots:
 
};
 
#endif // SINUS_H

sinus.cpp

#include "sinus.h"
using namespace std;
 
Sinus::Sinus(QWidget *parent) :
    QWidget(parent) { }
 
void Sinus::paintEvent ( QPaintEvent* pe){
    QPainter aPainter(this);
    int windowHeight = this->rect().height();
    int windowWidth = this->rect().width();
 
    int xg0 = 0;
    int yg0 = windowHeight/2;
    aPainter.translate (xg0,yg0);
 
    float x;
    float y;
    float angle;
    float radius = 100;
    float sinus;
    int steps = windowWidth;
    int labelAllxSteps = steps / 7;
 
    aPainter.drawLine(0,0,windowWidth,0);
 
    for(int i = 0;i < steps;i++) {
        angle = 360 / (float)steps * (float)i;                // Calculate the angle
        sinus = sin (angle*PI/180);
        y = sinus * radius;
        x = (float)i;
        //cout << "i: "<< i << " Angle: " << angle << " x: "<< x << " y: "<< y << "\n";
        aPainter.drawPoint((int)x,(int)y);
 
        // Draw the center vertical line
        if((int)y == 0 && x > 0) {
            aPainter.drawLine(x,0-(windowHeight/2),x,0+(windowHeight/2));
        }
 
        // Draw Label's
        if((i % labelAllxSteps) == 0 && x > 0 && x < steps) {
            QString str;
            str.sprintf("Sin: %f Angel: %f", y, angle);
            aPainter.drawText((int)x,(int)y,str);
        }
    }
}

main.cpp

#include <QtGui/QApplication>
#include "sinus.h";
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Sinus sinus;
    sinus.setGeometry (300, 300, 200, 200);
    sinus.show();
    return a.exec();
}

2 Comments for Drawing a Sinus in QT



blödi says:

und wofür ist das?

    Thomas Spycher says:

    War ein Teil von Programmier-Hausaufgaben der KTSI…



Wanna say something?