Find the points in the plane that meet the conditions (coordinate points)?

problem description

The

parameter is [number, distance] to find out the point coordinates satisfied in the two-bit plane of the browser.

number: the number of coordinate points in the two-bit plane of the browser.

distance: the distance between each coordinate point must be less than this distance.

related codes

/**
     * :
     * :(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);//265045
     creatStar.init(function(list){
       console.log(list);

     });

/ / as a result, there is a stack explosion. I would like to ask if the boss has a good solution.

how to solve this problem?

Nov.19,2021

  1. there is a limit to the stack calls of browsers. Take a look at this: https://zhuanlan.zhihu.com/p/. rel= Chrome is about 10000
  2. Why is the
  3. program calling stack greater than 10000?

suppose: like me, the display screen of the subject is 1980 * 1080,
that is, clientWidth = 1980; clientHeight = 1080; distance = 50;
requirement: generate 20 points (passed in by the subject), so that the distance between each point (whether x or y) is greater than 50;

know that 50 * 20 = 1000 equals about 1080;

then the problem can be reduced to:

there are three random points on the band of a 155cm, so that the distance between every three points must be greater than or equal to 50cm;

50 * 3 = 150 equals about 155

suppose there are two points whose distance is greater than 55cm and less than 100cm, and the left and right boundaries of the distance between the two points are less than 50cm (for example, there is a point from the left 40cm, there is a point from the right 40cm, and there is only 75cm in the middle), then there is no third point to satisfy that the pairwise distance is greater than 50;

so: three points, consider only one axis, there is a high probability, get less than three points,
then, 20 points to meet both the x-axis and y-axis, there is a very high probability, can not get the last few points and fall into an infinite cycle, so Boom!

Menu