|
14 | 14 | * @namespace fabric.util |
15 | 15 | */ |
16 | 16 | 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 | | - |
29 | 17 | /** |
30 | 18 | * Calculate the cos of an angle, avoiding returning floats for known results |
31 | 19 | * @static |
|
169 | 157 | }, |
170 | 158 |
|
171 | 159 | /** |
172 | | - * Calculates angle between 2 vectors using dot product |
| 160 | + * Calculates angle between 2 vectors using atan2 |
173 | 161 | * @static |
174 | 162 | * @memberOf fabric.util |
175 | 163 | * @param {Point} a |
176 | 164 | * @param {Point} b |
177 | 165 | * @returns the angle in radian between the vectors |
178 | 166 | */ |
179 | 167 | 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); |
183 | 172 | }, |
184 | 173 |
|
185 | 174 | /** |
|
203 | 192 | getBisector: function (A, B, C) { |
204 | 193 | var AB = fabric.util.createVector(A, B), AC = fabric.util.createVector(A, C); |
205 | 194 | 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; |
209 | 196 | return { |
210 | 197 | vector: fabric.util.getHatVector(fabric.util.rotateVector(AB, phi)), |
211 | 198 | angle: alpha |
|
0 commit comments