js实现向量偏移角度

2017-09-07 07:52:32来源:CSDN作者:yangshuolll人点击

分享

需求是这样的,给出一个向量(x,y) 然后逆时针旋转45度,然后得到对应的x, y值


var xx = 0;var yy = 0;var tha = Math.PI/4;var params = [xx,yy,tha];function changeAngle(param){	if(param[0] !== 0 && param[1] !== 0){		var x = param[0];		var y = param[1];		var tha1 = param[2];		var value = Math.sqrt(x*x + y*y);		var cos1 = x / value;		var sin1 = y / value;		var cos2 = Math.cos(tha1);		var sin2 = Math.sin(tha1);		var cos3 = cos1*cos2 - sin1*sin2;		var sin3 = sin1*cos2 + cos1*sin2;		param[0] = (value * cos3).toFixed(2);		param[1] = (value * sin3).toFixed(2);	}}changeAngle(params);console.log(params);


微信扫一扫

第七城市微信公众平台