尝试1和2:
注意: 删除了前几次尝试以缩小问题大小。请参阅社区wiki以查看之前的尝试。
尝试3:
根据fuzzy-waffle的示例,我已经实现了以下内容,但似乎无法正常工作。有什么想法我可能做错了吗?
ImageMatrix ImageMatrix::GetRotatedCopy(VDouble angle)
{
// Copy the specifications of the original.
ImageMatrix &source = *this;
ImageMatrix &target = CreateEmptyCopy();
double centerX = ((double)(source.GetColumnCount()-1)) / 2;
double centerY = ((double)(source.GetRowCount()-1)) / 2;
// Remember: row = y, column = x
for (VUInt32 y = 0; y < source.GetRowCount(); y++)
{
for (VUInt32 x = 0; x < source.GetColumnCount(); x++)
{
double dx = ((double)x) - centerX;
double dy = ((double)y) - centerY;
double newX = cos(angle) * dx - sin(angle) * dy + centerX;
double newY = cos(angle) * dy + sin(angle) * dx + centerY;
int ix = (int)round(newX);
int iy = (int)round(newY);
target[x][y][0] = source[ix][iy][0];
}
}
return target;
}
通过这个原型矩阵,我们可以更好地理解IT技术的相关概念。
1 2 1
0 0 0
-1 -2 -1
... prototype.GetRotatedCopy(0)(这是正确的)...
意思是获取原型对象的旋转副本,参数为0。
1 2 1
0 0 0
-1 -2 -1
... prototype.GetRotatedCopy(90) (incorrect) ...
-2 0 0
-2 0 2
0 0 2
... prototype.GetRotatedCopy(180)(不正确,但有一定的逻辑性?)...
原意:获取旋转180度后的原型副本(不正确,但有一定的逻辑性?)0 -1 -2
1 0 -1
2 1 0
...prototype.GetRotatedCopy(270)(不正确 - 为什么这与0度旋转相同?)...
意思是原型的GetRotatedCopy(270)方法有问题,为什么它和0度旋转一样呢?1 2 1
0 0 0
-1 -2 -1
解决方案:
正如Mark Ransom指出的那样,我应该使用弧度而不是角度;我已经根据以下方式调整了我的代码:
ImageMatrix ImageMatrix::GetRotatedCopy(VDouble degrees)
{
// Copy the specifications of the original.
ImageMatrix &source = *this;
ImageMatrix &target = CreateEmptyCopy();
// Convert degree measurement to radians.
double angle = degrees / 57.3;
// ... rest of code as in attempt #3 ...
感谢各位的帮助!
1 2 1
0 0 0
-1 -2 -1
1 2 1
0 0 0
-1 -2 -1
-1 0 1
-2 0 2
-1 0 1
-1 -2 -1
0 0 0
1 2 1
1 0 -1
2 0 -2
1 0 -1