diff options
Diffstat (limited to 'src/mbgl/map/transform.cpp')
-rw-r--r-- | src/mbgl/map/transform.cpp | 29 |
1 files changed, 12 insertions, 17 deletions
diff --git a/src/mbgl/map/transform.cpp b/src/mbgl/map/transform.cpp index b932c8cb38..3c451d00eb 100644 --- a/src/mbgl/map/transform.cpp +++ b/src/mbgl/map/transform.cpp @@ -310,29 +310,24 @@ void Transform::rotateBy(const PrecisionPoint& first, const PrecisionPoint& seco return; } - double center_x = static_cast<double>(state.width) / 2.0, center_y = static_cast<double>(state.height) / 2.0; + PrecisionPoint center(state.width, state.height); + center /= 2; - const double begin_center_x = first.x - center_x; - const double begin_center_y = first.y - center_y; - - const double beginning_center_dist = - std::sqrt(begin_center_x * begin_center_x + begin_center_y * begin_center_y); + const PrecisionPoint offset = first - center; + const double distance = std::sqrt(std::pow(2, offset.x) + std::pow(2, offset.y)); // If the first click was too close to the center, move the center of rotation by 200 pixels // in the direction of the click. - if (beginning_center_dist < 200) { - const double offset_x = -200, offset_y = 0; - const double rotate_angle = std::atan2(begin_center_y, begin_center_x); - const double rotate_angle_sin = std::sin(rotate_angle); - const double rotate_angle_cos = std::cos(rotate_angle); - center_x = first.x + rotate_angle_cos * offset_x - rotate_angle_sin * offset_y; - center_y = first.y + rotate_angle_sin * offset_x + rotate_angle_cos * offset_y; + if (distance < 200) { + const double heightOffset = -200; + const double rotateAngle = std::atan2(offset.y, offset.x); + center.x = first.x + std::cos(rotateAngle) * heightOffset; + center.y = first.y + std::sin(rotateAngle) * heightOffset; } - const double first_x = first.x - center_x, first_y = first.y - center_y; - const double second_x = second.x - center_x, second_y = second.y - center_y; - - const double ang = state.angle + util::angle_between(first_x, first_y, second_x, second_y); + const PrecisionPoint newFirst = first - center; + const PrecisionPoint newSecond = second - center; + const double ang = state.angle + util::angle_between(newFirst.x, newFirst.y, newSecond.x, newSecond.y); _setAngle(ang, duration); } |