saber-vector
math/vector lib for javascript!
npm install saber-vector
API
angleToRad
角度转弧度
angleToRad90 // 1.5707963267948966
radToAngle
弧度转角度
radToAngleMath.PI / 2 // 90
magnitude2d
求 2d 向量的模
magnitude2d3, 4 // 5
magnitude3d
求 3d 向量的模
magnitude3d1, 2, 2 // 3
Vec2
2d 坐标类型
构造函数:
new Vec2x, y
方法:
注意:以下方法均为纯函数
- props 获取所有坐标属性
- equals 判断两个向量是否相等
- add 向量和
- sub 向量差
- mul 向量乘
- div 向量除
- scale 向量缩放
- neg 向量取反
- mag 返回向量的模
- angleWith 与另一个向量夹角
- dot 向量数量积
- cross 向量叉积
- unitized 单位化
Vec3
3d 坐标类型
构造函数:
new Vec3x, y, z
方法:...同 Vec2
Line2D
二维有向线段
构造函数:
new Line2Dstart_vec2, end_vec2
方法:
注意:以下方法均为纯函数
- projection 在另一个二维有向线段上的投影
- toVec 转为 Vec2 类型
Line3D
三维有向线段
构造函数:
new Line3Dstart_vec3, end_vec3
方法:...同 Line2D
Examples
// v.neg().neg()console.log end2_3d .neg .neg .isEqualsend2_3d // true // get angle in two vectors?console.logradToAngleline1_3d.toVec.angleWithline2_3d.toVec // 45 // get props of vector?console.logline1_3d.toVec.props // get line's projection on line2?console.log line1_3d .projectionline2_3d .toVec .props // same to 2d console.log'2d: ', radToAngleline1_2d.toVec.angleWithline2_2d.toVecconsole.log line1_2d .projectionline2_2d .toVec .propsconsole.log end2_2d .neg .neg .isEqualsend2_2d // true
Author
saber2pr