Repository navigation
Expand file tree
/
Copy pathpointableobject.cpp
More file actions
178 lines (141 loc) · 4.3 KB
/
Copy pathpointableobject.cpp
File metadata and controls
178 lines (141 loc) · 4.3 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "drawable_abstract.h"
PointableObject::PointableObject(Rectangle rect)
:DrawableObject(),
first(nullptr),
second(nullptr),
rotatedTopPoint(Point()),
rotatedBottomPoint(Point()),
angle(0.0),
rectangle(rect)
{
}
void PointableObject::updateAngle()
{
Point p1 = first->getCenter();
Point p2 = second->getCenter();
if(std::abs(p1.x - p2.x) < std::numeric_limits<double>::epsilon())
{
if (p1.y > p2.y)
{
this->angle = 0.0;
return;
}else
{
this->angle = 180.0;
return;
}
}else if(std::abs(p1.y - p2.y) < std::numeric_limits<double>::epsilon())
{
if (p1.x > p2.x)
{
this->angle = 90.0;
return;
}else{
this->angle = 270.0;
return;
}
}else
{
double hypo = std::hypot(
p1.x - p2.x,
p1.y - p2.y
);
// if (this->type == ROD)
// defaultLength = hypo;
// qInfo() << "Default length of spring: ";
// qInfo() << defaultLength;
// if (p1.y > p2.y)
// this->angle = astd::sin((p1.x - p2.x) / hypo) / PI * 180;
// else
// this->angle = 90 + astd::cos((p1.x - p2.x) / hypo) / PI * 180;
if (p1.y > p2.y)
{
if (p1.x > p2.x)
{
this->angle = 90 - std::acos((p1.x - p2.x) / hypo) / PI * 180;
}
else
{
this->angle = 270 + std::acos(-(p1.x - p2.x) / hypo) / PI * 180;
}
}
else
{
if (p1.x > p2.x)
{
this->angle = 90 + std::asin(-(p1.y - p2.y) / hypo) / PI * 180;
}
else
{
this->angle = 270 + std::asin((p1.y - p2.y) / hypo) / PI * 180;
}
}
}
}
QVector<Point*> PointableObject::rotate(QVector<Point *> points)
{
Point center = this->rectangle.getCenter();
QMatrix4x4 transformationMatrix;
QVector3D radiusVector = QVector3D(center.x, center.y, 0.0);
transformationMatrix.rotate(this->angle, 0.0, 0.0, 1.0);
for (int i = 0; i < points.length(); i++){
QVector3D point = QVector3D(points[i]->x, points[i]->y, 0.0);
point -= radiusVector;
point = point * transformationMatrix;
point += radiusVector;
points[i]->x = point.x();
points[i]->y = point.y();
}
rotatedTopPoint = *points[0];
rotatedBottomPoint = *points[points.size() - 1];
return points;
}
void PointableObject::updateLength()
{
if (this->first == nullptr || this->second == nullptr)
return;
Point firstContactPoint = this->first->getContactPoint(this->second);
Point secondContactPoint = this->second->getContactPoint(this->first);
double hypotenuse = std::sqrt(
std::pow(firstContactPoint.x - secondContactPoint.x, 2.0) +
std::pow(firstContactPoint.y - secondContactPoint.y, 2.0)
);
if (hypotenuse < std::numeric_limits<double>::epsilon())
return;
this->rectangle.height = hypotenuse;
}
void PointableObject::update(bool isMovedByUser)
{
if (this->first == nullptr || this->second == nullptr)
return;
Point contactPoint = first->getContactPoint(second);
this->updateAngle();
if (this->getType() == SPRING)
this->updateLength();
else if (isMovedByUser)
this->updateLength();
this->draw();
this->moveTo(
Point(
contactPoint.x - (this->rotatedTopPoint.x - this->rectangle.getCenter().x),
contactPoint.y - (this->rotatedTopPoint.y - this->rectangle.getCenter().y)
)
);
}
void PointableObject::setFirstConnectable(ConnectableObject *f)
{
this->first = f;
}
void PointableObject::setSecondConnectable(ConnectableObject *s)
{
this->second = s;
//if (this->getType() == ROD)
//{
if (this->getFirstConnectable()->getType() == MATERIAL_POINT && this->getSecondConnectable()->getType() == STATIONARY_POINT)
{
ConnectableObject *tmp = this->first;
this->first = this->second;
this->second = tmp;
}
//}
}