Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a wrapAngle utility function. #836

Merged
merged 1 commit into from
Jul 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/mapInteractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1156,10 +1156,8 @@ var mapInteractor = function (args) {
}
if (m_state.zoomrotateAllowRotation) {
var theta = m_state.initialRotation + deltaTheta;
/* Compute the delta in the range of [-PI, PI). This is involed to work
* around modulo returning a signed value. */
deltaTheta = ((theta - m_this.map().rotation()) % (Math.PI * 2) +
Math.PI * 3) % (Math.PI * 2) - Math.PI;
/* Compute the delta in the range of [-PI, PI). */
deltaTheta = util.wrapAngle(theta - m_this.map().rotation());
/* If we reverse direction, don't rotate until some threshold is
* exceeded. This helps prevent rotation bouncing while panning. */
if (deltaTheta && (deltaTheta * (m_state.lastRotationDelta || 0) >= 0 ||
Expand Down
17 changes: 17 additions & 0 deletions src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,23 @@ var util = module.exports = {
return {min: min, max: max};
},

/**
* Given a value in radians, return a value wrapped to the range [-PI, PI).
*
* @param {number} value A value in radians.
* @returns {number} The wrapped value.
*/
wrapAngle: function (value) {
/* Module will only ensure that this is between [-2 PI, 2 PI). */
value = value % (Math.PI * 2);
if (value < -Math.PI) {
value += Math.PI * 2;
} else if (value >= Math.PI) {
value -= Math.PI * 2;
}
return value;
},

/**
* Escape any character in a string that has a code point >= 127.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/cases/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,16 @@ describe('geo.util', function () {
expect(util.getMinMaxValues(values, 200, 300, true)).toEqual({min: 200, max: 300});
expect(util.getMinMaxValues(values, 100, 400, true)).toEqual({min: 181, max: 303});
});

it('wrapAngle', function () {
expect(util.wrapAngle(0)).toBe(0);
expect(util.wrapAngle(2)).toBe(2);
expect(util.wrapAngle(4)).toBe(4 - Math.PI * 2);
expect(util.wrapAngle(7)).toBe(7 - Math.PI * 2);
expect(util.wrapAngle(17)).toBe(17 - Math.PI * 6);
expect(util.wrapAngle(-2)).toBe(-2);
expect(util.wrapAngle(-4)).toBe(-4 + Math.PI * 2);
expect(util.wrapAngle(-7)).toBe(-7 + Math.PI * 2);
expect(util.wrapAngle(-17)).toBe(-17 + Math.PI * 6);
});
});