-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotter.cpp
91 lines (73 loc) · 2.1 KB
/
plotter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "plotter.h"
#include <math.h>
#include <QPainter>
#include <QBrush>
#include <QPen>
#include <QColor>
#include <QMouseEvent>
Plotter::Plotter(QWidget *parent) : QWidget(parent){
freq = 1;
ampl = 1.0;
velocidade = 0.0;
teta = 0.0;
startTimer(100);
setMouseTracking(true); // habilita o rastreio do mouse
}
void Plotter::timerEvent(QTimerEvent *e, std::vector<int>& data){
teta += velocidade;
repaint();
}
void Plotter::setVelocidade(int velocidade){
this->velocidade = velocidade/100.0;
}
void Plotter::paintEvent(QPaintEvent *e){
QPainter painter(this);
QBrush brush;
QPen pen;
int x1, y1, x2, y2;
// habilita o anti-aliasing
painter.setRenderHint(QPainter::Antialiasing);
// define as props do preenchimento
brush.setColor(QColor(255,255,0));
brush.setStyle(Qt::SolidPattern);
// define as props da caneta
pen.setColor(QColor(255,0,0));
pen.setWidth(2);
// comunica as mudancas ao objeto painter
painter.setBrush(brush);
painter.setPen(pen);
// desenha um retangulo
painter.drawRect(0,0,width(), height());
// atualiza as propriedades da caneta
pen.setColor(QColor(0,0,0));
pen.setWidth(2);
// atualiza a caneta e desenha o eixo
// horizontal
painter.setPen(pen);
painter.drawLine(0, height()/2, width(), height()/2);
// atualiza as propriedades da caneta
pen.setColor(QColor(0,0,255));
pen.setWidth(1);
// atualiza a caneta e desenha o eixo
// horizontal
painter.setPen(pen);
// primeiro ponto
x1 = 0;
y1 = height()/2-height()/2*ampl*sin(2*M_PI*freq*x1+teta);
// desenha na tela
for(int i=1; i<width(); i++){
x2 = i;
y2 = height()/2-height()/2*ampl*sin(2*M_PI*(freq*x2)/width()+teta);
painter.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
}
void Plotter::setAmplitude(int ampl){
this->ampl = ampl/100.0;
repaint();
}
void Plotter::setFrequencia(int freq){
this->freq = freq/10.0;
repaint();
}