与位图和矩形相交/碰撞(intersect/collision with bitmap and rectangles)

编程入门 行业动态 更新时间:2024-10-28 14:30:03
位图和矩形相交/碰撞(intersect/collision with bitmap and rectangles)

当坐标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; }

I've been trying to write code when the coordinates cx/cy intersect with a rectangle/s the rectangle changes color. This has been driving me up the wall. Here is my code for the rectangles.

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; }

最满意答案

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

请注意, left , right , top和bottom的值实际上永远不会改变,因此如果在循环的第一次迭代中没有触发检查,那么它永远不会。

另外,请注意,在它相交的情况下,它会将颜色设置为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; }

Are you trying to inflate a rectangle, and change color when the intersection occurs?

Note that the values of left, right, top and bottom never actually change, so the if the check doesn't trigger in the first iteration of the loop, it never will.

Also, note that, in the case where it does intersect, it will set the color to DKGRAY, but then set it straight back to WHITE again on the next loop. Is this what you want?

I think you mean to do something like this. Here, we are comparing with the actual changing values.

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; }

更多推荐

本文发布于:2023-07-09 14:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1087117.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位图   矩形   intersect   rectangles   bitmap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!