var a = {x:0,y:100};
var b = {x:10,y:20};
//内積
var dot = a.x * b.x + a.y * b.y;
//絶対値
var absA = Math.sqrt(a.x*a.x + a.y*a.y);
var absB = Math.sqrt(b.x*b.x + b.y*b.y);
//dot = |a||b|cosθという公式より
var cosTheta = dot / (absA*absB);
//すでにベクトルがノーマライズされてたら dotのみでいける
//cosθの逆関数
var theta = Math.acos(cosTheta);
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme