js 一些补充学习的笔记

随机打乱数组

function random_array(array_data)  {
    array_data.sort(function(lhs, rhs) { // 随机决定他们的大小
        if(Math.random() <= 0.5) {
            return -1;
        }
        else {
            return 1;
        }
    })
}

array_data = [101, 201, 301, 501, 701];
random_array(array_data);
console.log(array_data);

弧度转角度

function rad2deg(r) {
    var degree = r * 180 / Math.PI;
    return degree;
}

角度转弧度

function rad2deg(r) {
    var degree = r * 180 / Math.PI;
    return degree;
}

Math.PI == 180度弧度值

value = rad2deg(Math.PI / 4)//180度/4
console.log(value);//45
// 反三角函数,给你一个 正玄值,--> 返回一个弧度
value = Math.sin(deg2rad(90));//90度转弧度 弧度 转正弦值
value = Math.asin(value);//正弦值再返转为弧度
console.log(rad2deg(value));//90 弧度再转回角度

value = Math.cos(deg2rad(90));//90度转弧度 弧度 转余弦值
value = Math.acos(value);//余弦值再返转为弧度
console.log(rad2deg(value));//90 弧度再转回角度

value = Math.tan(deg2rad(88));
value = Math.atan(value);
console.log(rad2deg(value));

Math.atan2(y, x), 返回一个坐标(y, x)对应的角度;(-PI, PI];
这个值是指这个x,y 基于0,0的弧度
默认角度向右为0度 一般摇杆是默认向上为0度 所以要-90度偏转

var r = Math.atan2(0, 1);
value = rad2deg(r);
console.log(value);//0

// atan2: (-PI, PI]
var r = Math.atan2(1, 1);
value = rad2deg(r);
console.log(value);//45

r = Math.atan2(-1, -1);
value = rad2deg(r);
console.log(value);//-135
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容