• C#实现不规则图形分割成多个矩形组合可视化工具, 核心是一个找最大内切矩形的算法


    有个项目,有些不规则区域,想转成尽可能少的小矩形。 用C#做了一个可视化的工具:

    忽略面积<= 4的:

    核心算法:一组二维数据找到其中最大内切矩形区域,并扩展了一下,把四周剩余区域切割递归找矩形。


    找最大矩形参考代码:
    https://stackoverflow.com/questions/7245/puzzle-find-largest-rectangle-maximal-rectangle-problem/20039017#20039017
    https://www.drdobbs.com/database/the-maximal-rectangle-problem/184410529?pgno=1

    其中最核心的找最大矩形类算法类:
     

    1. public class Rect
    2. {
    3. public int x1 = 0;
    4. public int y1 = 0;
    5. public int x2 = 0;
    6. public int y2 = 0;
    7. public Rect() { }
    8. public Rect(int _x1, int _y1, int _x2, int _y2)
    9. {
    10. x1 = _x1;
    11. y1 = _y1;
    12. x2 = _x2;
    13. y2 = _y2;
    14. }
    15. public override string ToString()
    16. {
    17. StringBuilder sb = new StringBuilder();
    18. sb.Append("[(");
    19. sb.Append(x1);
    20. sb.Append(",");
    21. sb.Append(y1);
    22. sb.Append(")-(");
    23. sb.Append(x2);
    24. sb.Append(",");
    25. sb.Append(y2);
    26. sb.Append(")]");
    27. return sb.ToString();
    28. }
    29. }
    30. class Point
    31. {
    32. public int x = 0;
    33. public int y = 0;
    34. public Point(int _x, int _y)
    35. {
    36. x = _x;
    37. y = _y;
    38. }
    39. }
    40. public class FindBiggestRectangle
    41. {
    42. public static int[] updateCache(int[] cache, byte[] matrixRow, int MaxX)
    43. {
    44. for (int m = 0; m < MaxX; m++)
    45. {
    46. if (0 == matrixRow[m])
    47. {
    48. cache[m] = 0;
    49. }
    50. else
    51. {
    52. cache[m]++;
    53. }
    54. }
    55. return cache;
    56. }
    57. public static void findMaxRectangle(byte[,] matrix, int width, int hight, ref Rect rect)
    58. {
    59. Point best_ll = new Point(0, 0);
    60. Point best_ur = new Point(-1, -1);
    61. int best_area = 0;
    62. int MaxX = width;
    63. int MaxY = hight;
    64. Stack stack = new Stack();
    65. int[] cache = new int[MaxX + 1];
    66. for (int m = 0; m != MaxX + 1; m++)
    67. {
    68. cache[m] = 0;
    69. }
    70. for (int n = 0; n != MaxY; n++)
    71. {
    72. int openWidth = 0;
    73. byte[] matrixRow = new byte[MaxX];
    74. for (int m = 0; m != MaxX; m++)
    75. {
    76. matrixRow[m] = matrix[m,n];
    77. }
    78. cache = updateCache(cache, matrixRow, MaxX);
    79. for (int m = 0; m != MaxX + 1; m++)
    80. {
    81. if (cache[m] > openWidth)
    82. {
    83. stack.Push(new Point(m, openWidth));
    84. openWidth = cache[m];
    85. }
    86. else if (cache[m] < openWidth)
    87. {
    88. int area;
    89. Point p;
    90. do
    91. {
    92. p = stack.Pop();
    93. area = openWidth * (m - p.x);
    94. if (area > best_area)
    95. {
    96. best_area = area;
    97. best_ll.x = p.x;
    98. best_ll.y = n;
    99. best_ur.x = m - 1;
    100. best_ur.y = n - openWidth + 1;
    101. }
    102. openWidth = p.y;
    103. } while (cache[m] < openWidth);
    104. openWidth = cache[m];
    105. if (openWidth != 0)
    106. {
    107. stack.Push(p);
    108. }
    109. }
    110. }
    111. }
    112. //Console.Out.Write("The maximal rectangle has area {0}.\n", best_area);
    113. //Console.Out.Write("Location: [col={0}, row={1}] to [col={2}, row={3}]\n", best_ll.x + 1, best_ll.y + 1, best_ur.x + 1, best_ur.y + 1);
    114. rect.x1 = best_ll.x;
    115. rect.y1 = best_ur.y;
    116. rect.x2 = best_ur.x;
    117. rect.y2 = best_ll.y;
    118. }
    119. }

     

    完整项目代码:
    https://download.csdn.net/download/zhenmu/86242488

     牵涉到的知识点:
    1. 图片的加载和像素解析,绘制到pictureBox上
    2.控制pitctureBox缩放(ctrl+滚轮)和移动
    3.动态生成bitmap,绘制点和矩形,显示到pictureBox上
    4.找出属于不同区域的相连的不规则图形对应的数据块
    5.不规则图形数据查找最大内嵌矩形算法
    6.拆分剩余上下左右4个区域,递归找到更小的矩形。

  • 相关阅读:
    家用电器行业数智化供应链系统:高效整合供应链,提升家电企业核心竞争力
    C++基础教程(转载)
    vue-cli4+vue2兼容安卓7(h5嵌入app),一步步排查发现问题并且解决(vue白屏)
    Linux必会100个命令(五十六)tcpdump命令
    使用Jmeter进行压力测试
    搭建微信小程序环境及项目结构介绍
    为机器学习算法准备数据(Machine Learning 研习之八)
    SwiftUI 4.0 如何轻松在 iOS 16 中设置 TextEditor 背景色
    C语言之共用体、枚举类型、typedef
    git commit规范
  • 原文地址:https://blog.csdn.net/zhenmu/article/details/125852065