Skip to content

Commit 366ac10

Browse files
committed
angleBetween, tests
1 parent 3ba2094 commit 366ac10

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

src/elements/element.line.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import defaults from '../core/core.defaults';
44
import Element from '../core/core.element';
5-
import {_steppedLineTo, _bezierCurveTo} from '../helpers/helpers.canvas';
6-
import {_angleDiff, _angleDiffPD, _normalizeAngle, _pointInLine, _steppedInterpolation, _bezierInterpolation} from '../helpers/helpers.math';
5+
import {_bezierCurveTo, _steppedLineTo} from '../helpers/helpers.canvas';
6+
import {_angleBetween, _angleDiff, _bezierInterpolation, _normalizeAngle, _pointInLine, _steppedInterpolation} from '../helpers/helpers.math';
77

88
const defaultColor = defaults.global.defaultColor;
99

@@ -167,15 +167,10 @@ function useFastPath(vm, options) {
167167
return !vm.tension && !vm.steppedLine && !vm.fill && (!vm.borderDash || !vm.borderDash.length) && !options.bounds;
168168
}
169169

170-
function angleBetween(a, s, e) {
171-
return a === s || a === e ||
172-
(_angleDiffPD(a, s) > _angleDiffPD(a, e) && _angleDiffPD(s, a) < _angleDiffPD(e, a));
173-
}
174-
175170
function propertyFn(property) {
176171
if (property === 'angle') {
177172
return {
178-
between: angleBetween,
173+
between: _angleBetween,
179174
compare: _angleDiff,
180175
normalize: _normalizeAngle,
181176
};

src/helpers/helpers.math.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export function _bezierInterpolation(p1, p2, t) {
181181
}
182182

183183
/**
184-
* Shortest distance between agnles, in either direction.
184+
* Shortest distance between angles, in either direction.
185185
* @private
186186
*/
187187
export function _angleDiff(a, b) {
@@ -197,9 +197,15 @@ export function _normalizeAngle(a) {
197197
}
198198

199199
/**
200-
* Angle difference in positive direction.
201200
* @private
202201
*/
203-
export function _angleDiffPD(a, b) {
204-
return _normalizeAngle(_normalizeAngle(b) - _normalizeAngle(a));
202+
export function _angleBetween(angle, start, end) {
203+
const a = _normalizeAngle(angle);
204+
const s = _normalizeAngle(start);
205+
const e = _normalizeAngle(end);
206+
const angleToStart = _normalizeAngle(s - a);
207+
const angleToEnd = _normalizeAngle(e - a);
208+
const startToAngle = _normalizeAngle(a - s);
209+
const endToAngle = _normalizeAngle(a - e);
210+
return a === s || a === e || (angleToStart > angleToEnd && startToAngle < endToAngle);
205211
}

test/specs/helpers.math.tests.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,36 @@ describe('Chart.helpers.math', function() {
106106
expect(math.isNumber(undefined)).toBe(false);
107107
expect(math.isNumber('cbc')).toBe(false);
108108
});
109+
110+
it('should compute shortest distance between angles', function() {
111+
expect(math._angleDiff(1, 2)).toEqual(-1);
112+
expect(math._angleDiff(2, 1)).toEqual(1);
113+
expect(math._angleDiff(0, 3.15)).toBeCloseTo(3.13, 2);
114+
expect(math._angleDiff(0, 3.13)).toEqual(-3.13);
115+
expect(math._angleDiff(6.2, 0)).toBeCloseTo(-0.08, 2);
116+
expect(math._angleDiff(6.3, 0)).toBeCloseTo(0.02, 2);
117+
expect(math._angleDiff(4 * Math.PI, -4 * Math.PI)).toBeCloseTo(0, 4);
118+
expect(math._angleDiff(4 * Math.PI, -3 * Math.PI)).toBeCloseTo(-3.14, 2);
119+
expect(math._angleDiff(6.28, 3.1)).toBeCloseTo(-3.1, 2);
120+
expect(math._angleDiff(6.28, 3.2)).toBeCloseTo(3.08, 2);
121+
});
122+
123+
it('should normalize angles correctly', function() {
124+
expect(math._normalizeAngle(-Math.PI)).toEqual(Math.PI);
125+
expect(math._normalizeAngle(Math.PI)).toEqual(Math.PI);
126+
expect(math._normalizeAngle(2)).toEqual(2);
127+
expect(math._normalizeAngle(5 * Math.PI)).toEqual(Math.PI);
128+
expect(math._normalizeAngle(-50 * Math.PI)).toBeCloseTo(6.28, 2);
129+
});
130+
131+
it('should determine if angle is between boundaries', function() {
132+
expect(math._angleBetween(2, 1, 3)).toBeTrue();
133+
expect(math._angleBetween(2, 3, 1)).toBeFalse();
134+
expect(math._angleBetween(-3.14, 2, 4)).toBeTrue();
135+
expect(math._angleBetween(-3.14, 4, 2)).toBeFalse();
136+
expect(math._angleBetween(0, -1, 1)).toBeTrue();
137+
expect(math._angleBetween(-1, 0, 1)).toBeFalse();
138+
expect(math._angleBetween(-15 * Math.PI, 3.1, 3.2)).toBeTrue();
139+
expect(math._angleBetween(15 * Math.PI, -3.2, -3.1)).toBeTrue();
140+
});
109141
});

0 commit comments

Comments
 (0)