javascript - Rotating 2D Vector by unknown angle such that its direction vector is [1,0] -
i trying rotate vector [x,y]
around origin such when rotation completed lies on x axis. in order this, i'm first computing angle between [x,y]
, [1,0]
, applying simple 2d rotation matrix it. i'm using numericjs work vectors.
math.anglebetween = function(a, b) { var x = numeric.dot(a, b) / (numeric.norm2(a) * numeric.norm2(b)); if(math.abs(x) <= 1) { return math.acos(x); } else { throw "bad input anglebetween"; } }; math.aligntox = function(v) { var theta = -math.anglebetween([1,0], v); var r = [[math.cos(theta), -math.sin(theta)], [math.sin(theta), math.cos(theta)]]; return numeric.dot(r, v); };
(note: math
namespace object within project. math
ye olde math object.)
this code works sometimes, there occasions no matter how many times run math.aligntox
vector never gets close aligning x axis. i'm testing checking if y
coordinate less 1e-10
.
i've tried using math.atan2
implicit z
coordinate of 0, results have been same. errors not being thrown. example results:
math.aligntox([152.44444444444434, -55.1111111111111]) // result: [124.62691466033475, -103.65652585400568] // expected: [?, 0] math.aligntox([372, 40]) // result: [374.14435716712336, -2.0605739337042905e-13] // expected: [?, 0] // value has abs(y coordinate) < 1e-10, considered aligned
what doing wrong?
if you're rotating other vector, you'll need use r
matrix. if need rotate vector, result [math.sqrt(x*x+y*y),0]
.
Comments
Post a Comment