1

我有一个通过 onTouchEvent() 方法设置两个变量 centerX、centerY 的位图。从这些 x,y 坐标中,我在位图上画了一个圆圈,并且可以通过滑动条将圆圈的像素更改为不同的 rgb 值。我用算法来定位圆的内部像素,但不幸的是,就目前而言,我必须逐个像素地搜索整个位图以定位圆的像素。这有一个巨大的方法调用开销,我想减少它。

我想做的是在圆圈周围创建一个边界框,这样我的算法搜索的空间就更少了,所以希望能加快速度。如何使用圆的 x,y 中心坐标和 50 的半径在圆周围创建一个矩形?

谢谢马特。

public void findCirclePixels(){ 


        for (int i=0; i < bgr.getWidth(); ++i) {
            for (int y=0; y < bgr.getHeight(); ++y) {

    if( Math.sqrt( Math.pow(i - centreX, 2) + ( Math.pow(y - centreY, 2) ) ) <= radius ){

                    bgr.setPixel(i,y,Color.rgb(Progress+50,Progress,Progress+100));
                }
            }
        }   

        }// end of changePixel()
4

2 回答 2

0

将外环限制从 circle.x - radius 更改为 circle.x + radius,将内环限制从 circle.y - radius 更改为 circle.y + radius。根据您的 x 和 y 可以是什么,您可能需要检查这些值中的任何一个是否小于 0 或大于图像宽度或高度的限制。

于 2011-05-16T15:43:19.600 回答
0

这工作得很好。

 public void findCirclePixels(){    



        for (int i=centreX-50; i < centreX+50; ++i) {
            for (int y=centreY-50; y <centreY+50 ; ++y) {

    if( Math.sqrt( Math.pow(i - centreX, 2) + ( Math.pow(y - centreY, 2) ) ) <= radius ){

                    bgr.setPixel(i,y,Color.rgb(Progress+50,Progress,Progress+100));
                }
            }
        }

        }// end of changePixel()
于 2011-05-16T16:06:49.303 回答