js 把二维数组改成四象限图用的格式

07-11 1483阅读

function quadrantSort(arr) {

js 把二维数组改成四象限图用的格式
(图片来源网络,侵删)

    let quadrant1 = [];

    let quadrant2 = [];

    let quadrant3 = [];

    let quadrant4 = [];

    arr.forEach(point => {

        if (point[0] > 0 && point[1] > 0) {

            quadrant1.push(point);

        } else if (point[0] 0) {

            quadrant2.push(point);

        } else if (point[0]

            quadrant3.push(point);

        } else if (point[0] > 0 && point[1]

            quadrant4.push(point);

        }

    });

    return {

        quadrant1,

        quadrant2,

        quadrant3,

        quadrant4

    };

}

const points = [[-1, 1], [1, 2], [-1, -1], [1, -1]];

const sortedPoints = quadrantSort(points);

console.log('Quadrant 1:', sortedPoints.quadrant1);

console.log('Quadrant 2:', sortedPoints.quadrant2);

console.log('Quadrant 3:', sortedPoints.quadrant3);

console.log('Quadrant 4:', sortedPoints.quadrant4);

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]