大图中搜索小图的算法

// 定义 bigRect 的尺寸
function range(min, max, step = 1) {
    const arr = [];
    for (let i = min; i <= max; i += step) {
        arr.push(i);
    }
    return arr;
}


const bigRect = {
    width: 5, // 示例宽度
    height: 5,  // 示例高度
    data:range(1,25)
};

const smaRect = {
    width: 3, // 示例宽度
    height: 3,  // 示例高度
    data:range(1,9)
}; // 可根据需要扩展


const view={
    getValueByPosArr(posArr,rect){
        return rect.data[posArr[0]+posArr[1]*rect.width]
    },
    //通过花费得到的收益
    initItem(item){
        const rect1=this.rect1
        const rect2=this.rect2
        const posArr1=this.posArr1
        const posArr2=this.posArr2

        while(item.price>0){
            const pos1=[posArr1[item.id][0]+posArr2[item.curFee][0],posArr1[item.id][1]+posArr2[item.curFee][1]]
            const pos2=posArr2[item.curFee]
            item.curFee++
            item.price--
            if(this.getValueByPosArr(pos1,rect1)===this.getValueByPosArr(pos2,rect2)){
                item.val++
            }else {
                break
            }
        }

    },
    //大图中查找小图,获取最大的相似区域
    getMaxList(rect1,rect2){
        this.rect1=rect1
        this.rect2=rect2
        this.posArr1=[]
        this.posArr2=[]
        this.taskList=[]
        const posArr1=this.posArr1
        const posArr2=this.posArr2


// 初始化任务列表
        const taskList=this.taskList
        let id = 0;

        const len2=rect2.width*rect2.height
// 遍历 bigRect 的每个像素点,生成任务
        const h1=bigRect.height-smaRect.height+1
        const w1=bigRect.width-smaRect.width+1
        for (let y = 0; y < h1; y++) {
            for (let x = 0; x < w1; x++) {
                posArr1.push([x,y])
                taskList.push({
                    id: id,          // 唯一任务 ID
                    price: len2,     // 价格
                    curFee:0,   //花费
                    val:0,    //价值
                });
                id++
            }
        }
        for (let y = 0; y < smaRect.height; y++) {
            for (let x = 0; x < smaRect.width; x++) {
                posArr2.push([x,y])
            }
        }

        //一直找到val最大的,
        let maxVal=0
        let maxList=[]
        let isRuning=true
        while (isRuning){
            isRuning=false
            taskList.forEach( (item)=>{
                //过滤无效任务,不执行了
                if (item.price>0&&maxVal < item.price + item.val) {
                    isRuning=true
                    this.initItem(item)
                    if (item.val > maxVal) {
                        maxVal = item.val
                        maxList.splice(0,maxList.length)
                        maxList.push(item)
                    }else if (item.val === maxVal) {
                        if(maxList.indexOf(item)===-1){
                            maxList.push(item)
                            maxVal = item.val
                        }
                    }

                }
            })
        }
        if(maxList.length){
            maxList.forEach((item)=>{
                item.x=posArr1[item.id][0]
                item.y=posArr1[item.id][1]
                item.pipei=parseInt((item.val*100/item.curFee))
            })
        }
        return maxList


    }
}
const list=view.getMaxList(bigRect,smaRect)
console.log(list)

  

posted @ 2025-05-01 22:11  无工时代  阅读(17)  评论(0)    收藏  举报