开发者

using math pow, changing variables between double and int (android bitmap)

开发者 https://www.devze.com 2023-03-03 22:27 出处:网络
I\'m looping through a bitmap pixel by pixel to determin if that pixel is within a circle placed on the bitmap. i\'ve been kindly given the math to determin this but the algorithm is using pow(double,

I'm looping through a bitmap pixel by pixel to determin if that pixel is within a circle placed on the bitmap. i've been kindly given the math to determin this but the algorithm is using pow(double, double). i've tried casting and using Integer.doubleValue to no avail. has anyone got any ideas how to get the pow method to return an int which the sqrt() method needs? centreX and centreY are both int too.

[update] sorry sqrt returns and needs a double:)

thanks mat.

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

                    int aPixel = bgr.getPixel(i,y);

                      if( sqrt( pow(i - centreX, 2) + ( pow(y - centreY, 2开发者_JAVA技巧) ) ) <= radius ){

                        bgr.setPixel(i,y,Color.MAGENTA);
                    }
                }
            }


Thanks all,

My bad the pow() method is static from the Math class so needed the dot operator to access it statically. silly mistake:) eg Math.pow(d,d);


divide your code into more lines so its easier to spot the line that causes the error:

Keep in mind that this is pseudo code :-)

double a = pow(i - centreX, 2);
double b = pow(y - centreY, 2);

int c = (parse to int) (a + b)

int root = sqrt( c )

Then debug those lines to spot the error :-)


If I understood right and your question is how to cast double to int, why don't you use floor or ceil function inside Math?


Just introduce a constant rsquared = radius*radius and change your condition to if ((i-centreX)*(i-centreX)+(y-centreX)*(y-centreX) <= rsquared). No floating point needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号