Skip to content

Commit 7c31e27

Browse files
authored
Merge pull request #1158 from IKhonakhbeeva/rotation
Fix sensor rotation in xml file
2 parents 63afd0a + a824f77 commit 7c31e27

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

qrutils/graphicsUtils/rotater.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,41 +122,43 @@ void Rotater::calcResizeItem(QGraphicsSceneMouseEvent *event)
122122
const qreal mouseX = event->scenePos().x() - masterCenter.x();
123123
const qreal mouseY = event->scenePos().y() - masterCenter.y();
124124

125-
// Master rotation is signed angle between initial and mouse vector.
126-
// Calculating it from theese vectors product and cosine theorem
127-
const qreal vectorProduct = zeroRotationVector.x() * mouseY
128-
- zeroRotationVector.y() * mouseX;
129125
const qreal mouseVectorLength = sqrt(mouseX * mouseX + mouseY * mouseY);
130126
if (mouseVectorLength < EPS) {
131127
return;
132128
}
133129

130+
// Master rotation is signed angle between initial and mouse vector.
131+
// Calculating it from theese vectors product and cosine theorem
132+
const qreal vectorProductLength = zeroRotationVector.x() * mouseY
133+
- zeroRotationVector.y() * mouseX;
134+
const qreal sin = vectorProductLength / (mouseVectorLength * mLength);
135+
134136
const qreal translationX = mouseX - zeroRotationVector.x();
135137
const qreal translationY = mouseY - zeroRotationVector.y();
136-
const qreal translation = translationX * translationX + translationY * translationY;
137-
138-
const qreal sin = vectorProduct / (mouseVectorLength * mLength);
139-
const bool cosIsNegative = mouseVectorLength * mouseVectorLength + mLength * mLength < translation;
138+
const bool cosIsNegative = mouseVectorLength * mouseVectorLength + mLength * mLength
139+
< translationX * translationX + translationY * translationY;
140140

141141
const qreal angleInWrongQuarter = asin(sin);
142-
const qreal littleAngle = cosIsNegative ? M_PI - angleInWrongQuarter : angleInWrongQuarter;
142+
const qreal angleInRightQuarter = cosIsNegative ? M_PI - angleInWrongQuarter : angleInWrongQuarter;
143+
const qreal littleAngle = angleInRightQuarter * 180 / M_PI;
143144

144145
const qreal masterAngleCompensation = mMaster->parentItem()
145146
? mMaster->parentItem()->rotation()
146147
: 0.0;
147148

148-
const qreal deltaAngle = fmod(littleAngle - mMaster->rotation() * M_PI / 180, 2 * M_PI);
149-
const qreal addAngle = deltaAngle > M_PI ? -2 * M_PI : deltaAngle < -M_PI ? 2 * M_PI : 0;
150-
const qreal angle = mMaster->rotation() * M_PI / 180 + deltaAngle + addAngle;
149+
const qreal deltaAngle = fmod(littleAngle - mMaster->rotation()- masterAngleCompensation, 360);
150+
const qreal addAngle = deltaAngle > 180 ? -360 : deltaAngle < -180 ? 360 : 0;
151+
const qreal angle = mMaster->rotation() + deltaAngle + addAngle;
151152

152153
if (event->modifiers() & Qt::ShiftModifier) {
153-
qreal roundedAngle = (angle - fmod(angle, M_PI_4));
154-
if (qAbs(roundedAngle - angle) > qAbs(roundedAngle + M_PI_4 - angle)) {
155-
roundedAngle += M_PI_4;
154+
qreal roundedAngle = (angle - fmod(angle, 45));
155+
qreal nextRoundedAngle = roundedAngle + (angle > 0 ? 45 : -45);
156+
if (qAbs(roundedAngle - angle) > qAbs(nextRoundedAngle - angle)) {
157+
roundedAngle = nextRoundedAngle;
156158
}
157-
mMaster->setRotation(roundedAngle * 180 / M_PI - masterAngleCompensation);
159+
mMaster->setRotation(roundedAngle);
158160
} else {
159-
mMaster->setRotation(angle * 180 / M_PI - masterAngleCompensation);
161+
mMaster->setRotation(angle);
160162
}
161163
}
162164

0 commit comments

Comments
 (0)