Skip to content

Commit a5a5f06

Browse files
author
Luiz Zappa
committed
fix(calcAngleBetweenVectors): use atan2 instead of dot product
1 parent 7f01868 commit a5a5f06

File tree

1 file changed

+6
-19
lines changed

1 file changed

+6
-19
lines changed

src/util/misc.js

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,6 @@
1414
* @namespace fabric.util
1515
*/
1616
fabric.util = {
17-
/**
18-
* Calculate the arccosine of an angle, avoiding values outside [-1, 1]
19-
* @static
20-
* @memberOf fabric.util
21-
* @param {Number} value the value of the cosine
22-
* @return {Number}
23-
*/
24-
acos: function(value) {
25-
var adjustedValue= Math.max(-1, Math.min(value, 1));
26-
return Math.acos(adjustedValue);
27-
},
28-
2917
/**
3018
* Calculate the cos of an angle, avoiding returning floats for known results
3119
* @static
@@ -169,17 +157,18 @@
169157
},
170158

171159
/**
172-
* Calculates angle between 2 vectors using dot product
160+
* Calculates angle between 2 vectors using atan2
173161
* @static
174162
* @memberOf fabric.util
175163
* @param {Point} a
176164
* @param {Point} b
177165
* @returns the angle in radian between the vectors
178166
*/
179167
calcAngleBetweenVectors: function (a, b) {
180-
return fabric.util.acos(
181-
(a.x * b.x + a.y * b.y) / (Math.sqrt(a.x * a.x + a.y * a.y) * Math.sqrt(b.x * b.x + b.y * b.y))
182-
);
168+
var dot = a.x * b.x + a.y * b.y,
169+
det = a.x * b.y - a.y * b.x;
170+
171+
return Math.atan2(det, dot);
183172
},
184173

185174
/**
@@ -203,9 +192,7 @@
203192
getBisector: function (A, B, C) {
204193
var AB = fabric.util.createVector(A, B), AC = fabric.util.createVector(A, C);
205194
var alpha = fabric.util.calcAngleBetweenVectors(AB, AC);
206-
// check if alpha is relative to AB->BC
207-
var ro = fabric.util.calcAngleBetweenVectors(fabric.util.rotateVector(AB, alpha), AC);
208-
var phi = alpha * (Math.abs(ro) <= 1e-7 ? 1 : -1) / 2;
195+
var phi = alpha / 2;
209196
return {
210197
vector: fabric.util.getHatVector(fabric.util.rotateVector(AB, phi)),
211198
angle: alpha

0 commit comments

Comments
 (0)