如何在旋转前获得旋转坐标的位置?
How to get positions of rotated coordinate before rotation?
我正在尝试旋转图像,但旋转后的图像有一些孔洞或缺失像素。为了获得应该填充缺失像素的颜色,我需要获得缺失像素在未旋转图像中的位置。
为了计算旋转后的像素坐标,我这样做了
double rotationAsRadiant = Math.toRadians( 360 - rotationInDegrees );
double cos = Math.cos( rotationAsRadiant );
double sin = Math.sin( rotationAsRadiant );
int xAfterRotation = (int)( x * cos + y * sin );
int yAfterRotation = (int)( -x * sin + y * cos );
如何获得用于计算 xAfterRotation 和 yAfterRotation 的 x 和 y?
反转公式,如:
double rotationAsRadiant = Math.toRadians( rotationInDegrees );
double cos = Math.cos( rotationAsRadiant );
double sin = Math.sin( rotationAsRadiant );
for (int xAfter = 0; xAfter < width; xAfter++) {
for (int yAfter = 0; yAfter < height; yAfter++) {
int xBefore = (int)( xAfter * cos + yAfter * sin );
int yBefore = (int)( -xAfter * sin + yAfter * cos );
// paint pixel xAfter/yAfter using original from xBefore/yBefore
...
}
}
这样,您肯定会填充生成图像的所有像素,原始像素最接近确切位置。不会有空洞的。
您最初的方法是由“给定源像素在目标图像中的什么位置?”这一问题驱动的。不能保证结果会覆盖所有像素(您已经看到了漏洞)。
我的方法是围绕“对于给定的目标像素,我在哪里可以找到它的源?”这个问题。
我正在尝试旋转图像,但旋转后的图像有一些孔洞或缺失像素。为了获得应该填充缺失像素的颜色,我需要获得缺失像素在未旋转图像中的位置。
为了计算旋转后的像素坐标,我这样做了
double rotationAsRadiant = Math.toRadians( 360 - rotationInDegrees );
double cos = Math.cos( rotationAsRadiant );
double sin = Math.sin( rotationAsRadiant );
int xAfterRotation = (int)( x * cos + y * sin );
int yAfterRotation = (int)( -x * sin + y * cos );
如何获得用于计算 xAfterRotation 和 yAfterRotation 的 x 和 y?
反转公式,如:
double rotationAsRadiant = Math.toRadians( rotationInDegrees );
double cos = Math.cos( rotationAsRadiant );
double sin = Math.sin( rotationAsRadiant );
for (int xAfter = 0; xAfter < width; xAfter++) {
for (int yAfter = 0; yAfter < height; yAfter++) {
int xBefore = (int)( xAfter * cos + yAfter * sin );
int yBefore = (int)( -xAfter * sin + yAfter * cos );
// paint pixel xAfter/yAfter using original from xBefore/yBefore
...
}
}
这样,您肯定会填充生成图像的所有像素,原始像素最接近确切位置。不会有空洞的。
您最初的方法是由“给定源像素在目标图像中的什么位置?”这一问题驱动的。不能保证结果会覆盖所有像素(您已经看到了漏洞)。
我的方法是围绕“对于给定的目标像素,我在哪里可以找到它的源?”这个问题。