【发布时间】:2014-10-22 21:37:27
【问题描述】:
我正在尝试创建一个 Java 函数,通过将像素移动到图像的相对中心来对图像产生凸出效果。我首先取像素的(x,y)坐标,找到相对位移,x = x-(x/2) 并将其转换为极坐标形式 [rcos(a), rsin (一个)]。 r 通过以下方式找到:r = Math.sqrt(xx + yy)。使用 Math.atan2(y/x) 找到角度 a。使用 r' = 2r^1.5 找到新半径 (r')。但是,来自 [rcos(a), rsin(a)] 的新 x,y 值超出了图像的尺寸,并出现错误。
我犯了一个根本性的错误吗?
public void bulge()
{
double xval, yval = 0;
//loop through the columns
for(int x = 0; x < this.getWidth(); x++)
{
//loop through the rows
for(int y = 0; y < this.getHeight(); y++)
{
int redValue, greenValue, blueValue = 0;
double newRadius = 0;
Pixel pixel = this.getPixel(x,y);
redValue = pixel.getRed();
greenValue = pixel.getGreen();
blueValue = pixel.getBlue();
xval = x - (x/2);
yval = y - (y/2);
double radius = Math.sqrt(xval*xval + yval*yval);
double angle = Math.atan2(yval, xval);
newRadius = 2*(Math.pow(radius,1.5));
xval = (int)(newRadius*Math.sin(angle));
yval = (int)(newRadius*Math.cos(angle));
Pixel pixelNewPos = this.getPixel((int)xval, (int)yval);
pixelNewPos.setColor(new Color(redValue, greenValue, blueValue));
}
}
}
【问题讨论】:
-
为什么不确保它们不超过图像的大小?比如,把这些部分剪掉?
-
@Joehot200,我尝试使用: if(xval>this.getWidth()){xval = this.getWidth();} if(yval>this.getHeight()) {yval = this.getHeight();} 但它似乎不起作用。