採用值的NaN我有這個類上的圖像序列的像素的工作原理雙後0/0
private int makeRSR(int x, int y)
{
double bRed = 0;double bGreen = 0; double bBlue = 0;
for(int k: sequence.keySet())
{
BufferedImage img = sequence.get(k);
for(int i=0; i<iteration; i++){
for(int j=0;j<spray_int; j++){
int dist= ((int)(dis_int*Math.random()));
double theta=((int)(361*Math.random())) ;
double inc_x=dist * Math.cos((theta*(Math.PI/180)));
double inc_y=dist * Math.sin((theta*(Math.PI/180)));
int row=Math.abs((int)(inc_y+y));
int column=Math.abs((int)(inc_x+x));
if(row<1 || column<1 || row>altI-1 || column>largI-1){
row=Math.abs((int) ((altI-1)*Math.random()));
column=Math.abs((int) ((largI-1)*Math.random()));
}
Color c = new Color(img.getRGB(column,row));
red.add(c.getRed());
green.add(c.getGreen());
blue.add(c.getBlue());
}
Color c = new Color(img.getRGB(x,y));
red.add(c.getRed());
green.add(c.getGreen());
blue.add(c.getBlue());
double maxR=max(red);
double maxG=max(green);
double maxB=max(blue);
bRed += ((((double)c.getRed())/(maxR))*255.00);
bGreen += ((((double)c.getGreen())/(maxG))*255.00);
bBlue += ((((double)c.getBlue())/(maxB))*255.00);
red.clear();green.clear();blue.clear();
}
}
redValue=0;greenValue=0;blueValue=0;
redValue= (int) (bRed/(iteration*(sequence.size())));
greenValue= (int) (bGreen/(iteration*(sequence.size())));
blueValue=(int) (bBlue/(iteration*(sequence.size())));
bRed=0.0;bGreen=0.0;bBlue=0.0;
return new Color(redValue,greenValue,blueValue).getRGB();
}
的問題是,有時c.getRed()和MAXR都爲0(對於相同其他渠道),所以bRed失去其內容並且看重NaN,而且它永遠不會改變。是否有錯誤,我錯過了,還是必須通過添加檢查來防止0/0? 謝謝,再見
一個簡單的方法來避免它來決定你想要發生什麼,併產生一個避免這種情況的等式。例如'bRed + = 255.0 * c.getRed()/(maxR + 1e-3);' –
@PeterLawrey使用'if',然而,更清潔,恕我直言更簡單。 –
@ Anony-Mousse'if'或'maxR == 0? 0.0:......'可以更清楚,尤其是如果你想要的行爲不容易適合方程式。 –