how to create a circle's bounding box android
-
14-11-2019 - |
Question
I have a bitmap that sets two variables centreX, centreY through the onTouchEvent() method. From these x,y co-ordinates i draw a circle over the bitmap and can change the circle's pixels to different rgb values through a slideBar. I target the circle's inner pixels with an algorithm but unfortunately as it stands i have to search the entire bitmap pixel by pixel to target the circle's pixels. this has a massive method call overhead that i'd like to reduce.
What i'm thinking of doing is creating a bounding box around the circle so my algorithm has less space to search, so will speed things up hopefully. How can i create a rectangle arounf the circle using the circle's x,y centre co-ords and a radius of 50?
Thanks matt.
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()
Solution
Change your outer loop limits from circle.x - radius to circle.x + radius, and your inner loop limits from circle.y - radius to circle.y + radius. You may, depending on what your x and y can be, need to check if any of those values are less than 0 or greater than the limits of your images width or height.
OTHER TIPS
This worked fine.
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()