与位图和矩形相交/碰撞

时间:2014-11-30 23:11:36

标签: android intersect rect

当坐标cx / cy与矩形/ s相交时,我一直在尝试编写代码,矩形会改变颜色。这一直在推动我。这是我的矩形代码。

for(int k = 0; k<=15; k++){
        k = k * 55;
        for(int i=0;i<=9;i++){
             i = i*55;  
             bounds.set(left+i,top+k,right+i,bottom+k);
             paint.setColor(Color.WHITE);
             canvas.drawRect(bounds, paint);
             if (cx == left || cx == right || cy==top|| cy == bottom){
                 paint.setColor(Color.DKGRAY);
                 canvas.drawRect(bounds, paint);
             }
             i=i/55;
            }    
        k = k/55;
    }   

2 个答案:

答案 0 :(得分:0)

您是否试图给矩形充气,并在交叉点发生变化时颜色?

请注意,leftrighttopbottom的值实际上从未改变,因此如果检查未在第一次迭代中触发循环,它永远不会。

另外,请注意,在它相交的情况下,它会将颜色设置为DKGRAY,但在下一个循环中再将其直接设置回WHITE。这是你想要的吗?

我认为你的意思是做这样的事情。在这里,我们将与实际变化值进行比较。

for(int k = 0; k<=15; k++){
    k = k * 55;
    for(int i=0;i<=9;i++){
         i = i*55;  
         int boundsLeft = left + i;
         int boundsTop = top + k;
         int boundsRight = right + i;
         int boundsBottom = bottom + k;
         bounds.set(boundsLeft, boundsTop, boundsRight, boundsBottom);
         paint.setColor(Color.WHITE);
         canvas.drawRect(bounds, paint);
         if (cx == boundsLeft|| cx == boundsRight || cy==boundsTop || cy == boundsBottom ){
             paint.setColor(Color.DKGRAY);
             canvas.drawRect(bounds, paint);
         }
         i=i/55;
        }    
    k = k/55;
}   

答案 1 :(得分:0)

             for(int i=0;i<=9;i++){
             i = i*55;  
             int boundsLeft = left + i;
             int boundsTop = top + k;
             int boundsRight = right + i;
             int boundsBottom = bottom + k;
             bounds.set(boundsLeft, boundsTop, boundsRight, boundsBottom);
             if(boxes[k][i]==0){
                 paint.setColor(Color.GREEN);
                 canvas.drawRect(bounds, paint);
             }
             else{
                 paint.setColor(Color.DKGRAY);
                 canvas.drawRect(bounds, paint);
             }
             if (cx >= boundsLeft && cx <= boundsRight && cy>=boundsTop && cy <= boundsBottom ){
                 plow.set(boundsLeft, boundsTop, boundsRight, boundsBottom);
                 paint.setColor(Color.DKGRAY);
                 canvas.drawRect(plow, paint);
                 boxes[k][i]=1;
             }
              i=i/55;

            }