如何创建一个圆的边界框android
我有一个位图,通过 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()
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将外循环限制从circle.x - radius 更改为circle.x + radius,并将内循环限制从circle.y - radius 更改为circle.y + radius。根据 x 和 y 的大小,您可能需要检查这些值中的任何一个是否小于 0 或大于图像宽度或高度的限制。
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.
这很好用。
This worked fine.