Comments (6)
No it doesn't: normalisation changes a vector's length, but preserves its angle, and so as long as you make sure to use the correct origin, atan2 will do the right thing. Is there an actual bug you found with input values that do the wrong thing? If so, can you add those to the issue?
Because this should work perfectly fine, and the following test code works exactly as it should, finding the correct angle a
at every iteration:
cos = Math.cos; sin = Math.sin; tau = Math.PI *2;
for (var a = 0; a<Math.PI*2; a+=0.1) {
v1 = {x: cos(a), y: sin(a) };
v2 = {x: cos(2*a), y: sin(2*a) };
r = utils.angle({x:0,y:0}, v1, v2);
// difference between v1 and v2 should be a
console.log(Math.abs(a - (r+tau)%tau) < 0.00001);
}
This gives me true
for every iteration. 63 times on a+=0.1
, 629 times on a+=0.01
.
(That's not to say the normalisation should be kept: if the code does the same without normalisation then there is no point in wasting time on the computation, but that's a different issue)
from bezierjs.
Your code does not show the problem because you are using unit vectors, so it does not really matter if you normalise or not.
Try the same code using lengths > 1. (the angle is the same no matter vectors length).
cos = Math.cos; sin = Math.sin; tau = Math.PI *2;
var l1 = 2, l2 = 1;
for (var a = 0; a<Math.PI*2; a+=0.1) {
v1 = {x: l1*cos(a), y: l1*sin(a) };
v2 = {x: l2*cos(2*a), y: l2*sin(2*a) };
r = utils.angle({x:0,y:0}, v1, v2);
// difference between v1 and v2 should be a
console.log(Math.abs(a - (r+tau)%tau) < 0.00001);
}
I suggest to remove the normalisation because there is no need for using unit vectors.
from bezierjs.
updated the issue title, will be removing them later today and spinning a new release.
from bezierjs.
Perfect.
However, I want to remark this is a bug, not a noop.
If you use lengths > 1 your code will throw only 1 TRUE and FALSE for the rest. This is because cross product is equal to l1 l2 sin(angle) but dot product is equal to cos(angle). Of course if l1 and l2 are equal to 1 (as in your code) the result will be ok, but not for any others l1 and l2 values.
Function should be modified as follows:
angle: function(o,v1,v2) {
var dx1 = v1.x - o.x,
dy1 = v1.y - o.y,
dx2 = v2.x - o.x,
dy2 = v2.y - o.y,
//cross = dx1*dy2 - dy1*dx2,
cross, // do not compute yet. This is the bug I said
m1 = sqrt(dx1*dx1+dy1*dy1),
m2 = sqrt(dx2*dx2+dy2*dy2),
dot;
// Now you normalize (or not).
//Now it does not change the result, so now is noop, but only because we move cross product calculation below this line
dx1/=m1; dy1/=m1; dx2/=m2; dy2/=m2;
// Now compute dot and cross
dot = dx1*dx2 + dy1*dy2;
cross = dx1*dy2 - dy1*dx2, // this is bug fix if you want to keep the normalization
return atan2(cross, dot);
},
If for any reason you prefer using unit vectors as atan2 input you should normalize them before calculating cross and dot products, not in between.
from bezierjs.
noted, thanks for the explanation.
from bezierjs.
normalization removed and v2.2.2 pushed/published
from bezierjs.
Related Issues (20)
- _lut caching doesn't work: steps is off by one HOT 5
- Release v6.1.2 broken HOT 3
- Why get(t = 0.5) point is not the middle point of the curve? HOT 5
- Find optmal D1 - Bezier.cubicFromPoints HOT 1
- PolyBezier is not exported correct for ES Module. HOT 3
- Rational Bezier Curve Support HOT 1
- Example usage of `dist/bezier.js` HOT 4
- How to make a "strict" version of bezier.project(point)? HOT 2
- Should bezier.lineIntersects(line) work for line-line intersections? HOT 3
- intersects bug Bezier to Bezier HOT 3
- Suspicious line in `utils.compute()` HOT 2
- The intersection of the two bezier is empty HOT 10
- The intersection of two identical curves HOT 2
- Intersect line method does not always return existing intersections HOT 6
- Ability to get and estimate the centerline of a list of 3D points HOT 3
- PR for extending API to interpolate values along curve and find point on curve at distance?
- Artifact when offsetting an SVG path
- Split curves do not follow old curve. HOT 2
- Missing LICENSE file HOT 1
- How to Find Y-Coordinate for a Given X-Coordinate on a Bezier Curve HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bezierjs.