optype.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #pragma once
  2. #ifndef __optype_h_
  3. #define __optype_h_
  4. #include <Windows.h>
  5. #include <assert.h>
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. using uint = unsigned int;
  10. using uchar = unsigned char;
  11. using std::map;
  12. using std::vector;
  13. #ifdef UNICODE
  14. using TString = std::wstring;
  15. #else
  16. using TString = std::string;
  17. #endif
  18. using bytearray = std::vector<uchar>;
  19. struct point_t
  20. {
  21. int x, y;
  22. point_t() : x(0), y(0) {}
  23. point_t(int x_, int y_) : x(x_), y(y_) {}
  24. bool operator<(const point_t& rhs) const
  25. {
  26. if (std::abs(y - rhs.y) < 9)
  27. return x < rhs.x;
  28. else
  29. return y < rhs.y;
  30. }
  31. bool operator==(const point_t& rhs) const { return x == rhs.x && y == rhs.y; }
  32. };
  33. using vpoint_t = std::vector<point_t>;
  34. //(5,3) --> (2, 2, 1)
  35. class NumberGen {
  36. int _q, _r;
  37. public:
  38. NumberGen(int n, int cnt) : _q(n / cnt), _r(n% cnt) {}
  39. int operator[](int idx) const { return idx < _r ? _q + 1 : _q; }
  40. };
  41. struct rect_t {
  42. rect_t() : x1(0), y1(0), x2(0), y2(0) {}
  43. rect_t(int x1_, int y1_, int x2_, int y2_)
  44. : x1(x1_), y1(y1_), x2(x2_), y2(y2_) {}
  45. int x1, y1;
  46. int x2, y2;
  47. int width() const { return x2 - x1; }
  48. int height() const { return y2 - y1; }
  49. rect_t& shrinkRect(int w, int h) {
  50. x2 -= w;
  51. y2 -= h;
  52. x2 += 1;
  53. y2 += 1;
  54. return *this;
  55. }
  56. bool valid() const { return 0 <= x1 && x1 < x2 && 0 <= y1 && y1 < y2; }
  57. void divideBlock(int count, bool vertical, std::vector<rect_t>& blocks) {
  58. assert(valid());
  59. assert(count > 0);
  60. blocks.resize(count);
  61. if (vertical) {
  62. NumberGen gen(height(), count);
  63. int basey = y1;
  64. for (int i = 0; i < count; ++i) {
  65. blocks[i] = rect_t(x1, basey, x2, basey + gen[i]);
  66. basey += gen[i];
  67. }
  68. }
  69. else {
  70. NumberGen gen(width(), count);
  71. int basex = x1;
  72. for (int i = 0; i < count; ++i) {
  73. blocks[i] = rect_t(basex, y1, basex + gen[i], y2);
  74. basex += gen[i];
  75. }
  76. }
  77. assert(blocks.back().x2 == x2);
  78. assert(blocks.back().y2 == y2);
  79. }
  80. };
  81. using vrect_t = std::vector<rect_t>;
  82. struct point_desc_t {
  83. int id;
  84. point_t pos;
  85. };
  86. using vpoint_desc_t = std::vector<point_desc_t>;
  87. #endif