-
Notifications
You must be signed in to change notification settings - Fork 0
/
PendulumScene.cpp
132 lines (111 loc) · 3.61 KB
/
PendulumScene.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "PendulumGraphicsItem.h"
#include "PendulumScene.h"
#include "ToQtCoordinates.h"
#include <iostream>
#include <QGraphicsSceneMouseEvent>
#define XPOS 0.0
#define YPOS 0.0
#define WIDTH 450
#define HEIGHT 650
PendulumScene::PendulumScene(QObject *parent)
:
QGraphicsScene(XPOS, YPOS, WIDTH, HEIGHT, parent)
{
addRect(XPOS,YPOS,WIDTH,HEIGHT,QPen(Qt::black));
toQtCoordinates = new ToQtCoordinates(
ToQtCoordinates::Type::MidPoint,WIDTH,HEIGHT);
int count =1;
for(int i = 1; i <= numOfPendulums; i++){
vectorOfOmegas.push_back(2*PI/(60.0/(maxNumOfCyclesPerMinute-i)));
vectorOfLengths.push_back(initialLength+2*radius*(i-1));
vectorOfPendulums.push_back(
std::make_shared<PendulumGraphicsItem>(
toQtCoordinates->convertToQtCoordinates(
QPointF(
-vectorOfLengths.back()*sin(initialTetha),
altura - vectorOfLengths.back()*cos(initialTetha)
)
),
radius
)
);
addItem(vectorOfPendulums.back().get());
count++;
}
}
void PendulumScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
std::cout << "Scene position: " << mouseEvent->scenePos().x() << " , " << mouseEvent->scenePos().y() << std::endl;
std::cout << "Scene: " << this->width() << " , " << this->height() << std::endl;
}
void PendulumScene::setPendulumSceneToTimeZero()
{
currentTime = 0;
vectorOfThetas.clear();
setPendulumnPositionsAtTimet(currentTime);
update();
}
void PendulumScene::onUpdate()
{
vectorOfThetas.clear();
setPendulumnPositionsAtTimet(currentTime);
currentTime += timeIncrement;
update();
}
void PendulumScene::setPendulumColors(int index)
{
for(int i = 0; i < vectorOfPendulums.size(); i++){
switch (index) {
case 0:
vectorOfPendulums.at(i)->setColor(ColorOfBall::Yellow);
break;
case 1:
if(i%2 == 0){
vectorOfPendulums.at(i)->setColor(ColorOfBall::Red);
}
else{
vectorOfPendulums.at(i)->setColor(ColorOfBall::Blue);
}
break;
case 2:
if(i%3 == 0){
vectorOfPendulums.at(i)->setColor(ColorOfBall::Red);
}
else if(i%3 == 1){
vectorOfPendulums.at(i)->setColor(ColorOfBall::Blue);
}
else{
vectorOfPendulums.at(i)->setColor(ColorOfBall::Green);
}
break;
case 3:
break;
}
}
update();
}
void PendulumScene::setInitialTetha(double initialTetha)
{
this->initialTetha = initialTetha*PI/180;
vectorOfThetas.clear();
setPendulumnPositionsAtTimet(currentTime);
update();
}
void PendulumScene::setPendulumnPositionsAtTimet(double currentTime)
{
for(int i = 0; i < numOfPendulums; i++){
vectorOfThetas.push_back(initialTetha*cos(vectorOfOmegas.at(i)*currentTime + PI));
vectorOfPendulums.at(i)->setCenterOfBall(
toQtCoordinates->convertToQtCoordinates(
QPointF(
vectorOfLengths.at(i)*sin(vectorOfThetas.at(i)),
altura - vectorOfLengths.at(i)*cos(vectorOfThetas.at(i))
)
)
);
}
}
void PendulumScene::setTimeIncrement(double timeIncrement)
{
this->timeIncrement = timeIncrement;
}