图形-回行扫描函数

//回行扫描函数 left、top、right、bottom
function scanRound(x,y,w,h,isOpenFunc,callfunc) {
    const map={}
    function isOpen(x,y) {

        if(map[x+','+y]){
            return 0
        }
        if(x<0||x>=w||y<0||y>=h){
            return 0;
        }
        return isOpenFunc(x,y)
    }
    function callback(x,y) {
        map[x+','+y]=true;
        return callfunc(x,y)
    }
    if(isOpen(x,y)){
        callback(x,y)
    }else{
        return;
    }
    const dreMap={
        left:{x:-1,y:0},
        right:{x:1,y:0},
        bottom:{x:0,y:1},
        top:{x:0,y:-1},
    }
    const pos={
        x,y
    }
    let dre='init';
    while (dre!=='end'){
        if(dre==='init'){
            if(isOpen(pos.x+dreMap.left.x,pos.y+dreMap.left.y)){
                dre='left';
            }else if(isOpen(pos.x+dreMap.top.x,pos.y+dreMap.top.y)){
                dre='top';
            }else if(isOpen(pos.x+dreMap.right.x,pos.y+dreMap.right.y)){
                dre='right';
            }else if(isOpen(pos.x+dreMap.bottom.x,pos.y+dreMap.bottom.y)){
                dre='bottom';
            }else{
                dre='end';
            }
        }
        if(dre!=='end'){
            const npos={
               x:pos.x+dreMap[dre].x,y:pos.y+dreMap[dre].y
            }
            if(isOpen(npos.x,npos.y)){
                pos.x=npos.x;
                pos.y=npos.y;
                callback(pos.x,pos.y)
            }else{
                dre='init';
            }
        }

    }
}


scanRound(0,0,100,100,function (x,y) {
    return 1;
},function (x,y) {
    console.log(x,y);
})

 

posted @ 2019-06-26 11:18  巅峰蜗牛  阅读(249)  评论(0编辑  收藏  举报