java – 如何将2d阵列旋转LESS超过90°,达到最佳近似值?

前端之家收集整理的这篇文章主要介绍了java – 如何将2d阵列旋转LESS超过90°,达到最佳近似值?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

假设我有一个以0°旋转存储的数组:

  1. 0 0 1 0 0
  2. 0 0 1 0 0
  3. 1 1 1 0 0
  4. 0 0 0 0 0
  5. 0 0 0 0 0

如果我通过,我希望它以良好的近似值返回,例如30°作为参数,它将是这样的:

  1. 0 0 0 1 0
  2. 1 1 0 1 0
  3. 0 0 1 0 0
  4. 0 0 0 0 0
  5. 0 0 0 0 0

45°会

  1. 1 0 0 0 1
  2. 0 1 0 1 0
  3. 0 0 1 0 0
  4. 0 0 0 0 0
  5. 0 0 0 0 0

I am aware of the solutions posted for 90° rotations.但我认为这不会对我有帮助吗?

我没有任何示例代码,因为我目前甚至不知道从哪里开始查找.如果有任何关键字我可以google指向我的方向我可以适应这个,这也将是伟大的.

Spectre在C#中的代码解决方案:

  1. class Rotation
  2. {
  3. public Rotation() {
  4. A = new int[xs,ys]{
  5. {0,9,0},{0,{9,};
  6. B = new int[xs,ys];
  7. deg = (float)(Math.PI / 180.0);
  8. }
  9. public const int xs = 7; // matrix size
  10. public const int ys = 7;
  11. const int x0 = 3; // rotation center cell
  12. const int y0 = 3;
  13. readonly float deg;
  14. public int[,] A;
  15. public int[,] B;
  16. //---------------------------------------------------------------------------
  17. public void rotcv(float ang) {
  18. rotcw(Rotation.x0,Rotation.y0,ang);
  19. }
  20. private void rotcw(int x0,int y0,float ang) // rotate A -> B by angle ang around (x0,y0) CW if ang>0
  21. {
  22. int x,y,ix0,iy0,ix1,iy1,q;
  23. double xx,yy,fx,fy,c,s;
  24. // circle kernel
  25. c = Math.Cos(-ang); s = Math.Sin(-ang);
  26. // rotate
  27. for (y = 0; y < ys; y++)
  28. for (x = 0; x < xs; x++)
  29. {
  30. // offset so (0,0) is center of rotation
  31. xx = x - x0;
  32. yy = y - y0;
  33. // rotate (fx,fy) by ang
  34. fx = ((xx * c) - (yy * s));
  35. fy = ((xx * s) + (yy * c));
  36. // offset back and convert to ints and weights
  37. fx += x0; ix0 = (int)Math.Floor(fx); fx -= ix0; ix1 = ix0 + 1; if (ix1 >= xs) ix1 = ix0;
  38. fy += y0; iy0 = (int)Math.Floor (fy); fy -= iy0; iy1 = iy0 + 1; if (iy1 >= ys) iy1 = iy0;
  39. // bilinear interpolation A[fx][fy] -> B[x][y]
  40. if ((ix0 >= 0) && (ix0 < xs) && (iy0 >= 0) && (iy0 < ys))
  41. {
  42. xx = (A[ix0,iy0]) + ((A[ix1,iy0] - A[ix0,iy0]) * fx);
  43. yy = (A[ix0,iy0]) * fx);
  44. xx = xx + ((yy - xx) * fy); q =(int) xx;
  45. }
  46. else q = 0;
  47. B[x,y] = q;
  48. }
  49. }
  50. }

测试:

  1. static void Main(string[] args)
  2. {
  3. Rotation rot = new Rotation();
  4. for (int x = 0; x < Rotation.xs; x++) {
  5. for (int y = 0; y < Rotation.xs; y++) {
  6. Console.Write(rot.A[x,y] + " ");
  7. }
  8. Console.WriteLine();
  9. }
  10. Console.WriteLine();
  11. float rotAngle = 0;
  12. while (true)
  13. {
  14. rotAngle += (float)(Math.PI/180f)*90;
  15. rot.rotcv(rotAngle);
  16. for (int x = 0; x < Rotation.xs; x++)
  17. {
  18. for (int y = 0; y < Rotation.xs; y++)
  19. {
  20. Console.Write(rot.B[x,y] + " ");
  21. }
  22. Console.WriteLine();
  23. }
  24. Console.WriteLine();
  25. Console.ReadLine();
  26. }
  27. }
最佳答案
好的,这是承诺的.第一个C代码

  1. //---------------------------------------------------------------------------
  2. #include

这里7×7预览15度的步骤:

preview

可能需要稍微调整一半的细胞或一些东西(中心根据我的喜好流血太多)

矩阵A是源,B是目标……

您还可以添加阈值…如:

  1. if (q>=5) q=9; else q=0;

猜你在找的Java相关文章