账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    求平面内满足条件的点(坐标点)?
    27
    0

    问题描述

    参数是【个数,距离】求出在浏览器这个二位平面内满足的点位坐标。

    个数:在浏览器这个二位平面内 坐标点的数量。

    距离:每一个坐标点位之间的间距必须小于这个距离。

    相关代码

    /**
         * 个数:生成几个。
         * 距离:间距多少(px)
         */
        function starList() {
            var list = [], //生成之后的list
                arg = [].slice.call(arguments), //参数
                number = arg[0] || 10, //个数
                distance = arg[1] || 100, //距离
                domBody = document && document.body || {
                    clientWidth: 1920,
                    clientHeight: 1080
                },
                maxW = domBody.clientWidth,
                maxH = domBody.clientHeight;
                if(typeof number!=="number"){
                     throw("第一个参数出错,应该是number类型");
                    
                }
                if(typeof distance!=="number"){
                    throw("第二个参数出错,应该是number类型");
                
               }
               if(number*distance>Math.min(maxW,maxH)){
                    throw("参数一与参数二的积必须小于视图的宽与度的最小值");
                }
            return {
                init: function (callback) {
                    callback = callback || function () {};
                    if(typeof callback!=="function"){
                        throw("参数出错,应该是function类型");
                        
                    }
                    this.pushList(); //存入数据
                    callback(list);
                },
                /**
                 * 存入数组
                 */
                pushList: function () {
                    //判断生成的点位数个数是否足够
                    if (list.length === number) {
                        return;
                    }
                    var point = this._random(maxW, maxH, 0);
                    if (this.judgeDistances(point)) {
                        list.push(point);
                        this.pushList();
                    } else {
                        //如果不满足条件再次执行
                        this.pushList();
                    }
                },
                /**
                 * 随机方法
                 */
                _random: function (maxw, maxh, min) {
                    return {
                        x: parseInt(Math.random() * (maxw - min + 1) + min, 10),
                        y: parseInt(Math.random() * (maxh - min + 1) + min, 10)
                    };
                },
                /**
                 * 判断距离
                 */
                judgeDistances: function (point) {
                    var l = list.length;
                    while (l--) {
                        //如果新生成的坐标距离不满足distance 则返回false 
                        if (Math.abs(list[l].x - point.x) < distance || Math.abs(list[l].y - point.y) < distance) {
                            return false;
                        }
                    }
                    return true;
                }
            }
        };
        var creatStar = starList(20,26);//如果26改为50或者45,则会爆栈。
         creatStar.init(function(list){
           console.log(list);
    
         });

    //结果出现爆栈了,请问大佬有没有好的解决方案。

    怎么解决这种问题呢?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • ︶自难忘 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部