fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Point {
  6. const int q_, r_;
  7. Point(int q, int r) : q_{q}, r_{r} {}
  8. inline void print() {
  9. cout << "q:" << q_ << ", r:" << r_ << endl;
  10. }
  11. };
  12.  
  13. class PointMap {
  14. public:
  15. const int RADIUS;
  16.  
  17. PointMap(int radius) : RADIUS{radius} {
  18. points = new Point**[2 * RADIUS + 1];
  19. for (int q = -RADIUS; q <= RADIUS; ++q) {
  20. int r1 = max(-RADIUS, -q - RADIUS);
  21. int r2 = min(RADIUS, -q + RADIUS);
  22. points[RADIUS + q] = new Point*[2 * RADIUS + 1]; // r2 - r1 + 1];
  23. for (int r = r1; r <= r2; ++r) {
  24. points[RADIUS + q][RADIUS + r] = new Point(q, r);
  25. points[RADIUS + q][RADIUS + r]->print();
  26. }
  27. }
  28. cout << "*** Ready constuctor ***\n\n";
  29. }
  30.  
  31. ~PointMap() {
  32. cout << "*** Delete Map ***\n\n";
  33. for (int q = -RADIUS; q <= RADIUS; ++q) {
  34. int r1 = max(-RADIUS, -q - RADIUS);
  35. int r2 = min(RADIUS, -q + RADIUS);
  36. for (int r = r1; r <= r2; ++r) {
  37. points[RADIUS + q][RADIUS + r]->print();
  38. delete points[RADIUS + q][RADIUS + r];
  39. }
  40. delete[] points[RADIUS + q];
  41. }
  42. delete[] points;
  43. }
  44.  
  45. void print(int row, int col) {
  46. points[RADIUS + row][RADIUS + col]->print();
  47. }
  48.  
  49. private:
  50. Point*** points;
  51. };
  52.  
  53. // class PointMap {
  54. // public:
  55. // const int DIMENSION;
  56.  
  57. // PointMap(int d) : DIMENSION{d} {
  58. // points = new Point**[DIMENSION];
  59. // for (int i = 0; i < DIMENSION; ++i) {
  60. // points[i] = new Point*[i + 1];
  61. // for (int j = 0; j < i + 1; ++j) {
  62. // points[i][j] = new Point(i, j);
  63. // points[i][j]->print();
  64. // }
  65. // }
  66. // cout << "*** Ready constuctor ***\n\n";
  67. // }
  68.  
  69. // ~PointMap() {
  70. // cout << "*** Delete Map ***\n\n";
  71. // for (int i = 0; i < DIMENSION; ++i) {
  72. // for (int j = 0; j < i + 1; ++j) {
  73. // delete points[i][j];
  74. // }
  75. // delete[] points[i];
  76. // }
  77. // delete[] points;
  78. // }
  79.  
  80. // void print(int row, int col) {
  81. // points[row][col]->print();
  82. // }
  83.  
  84. // private:
  85. // Point*** points;
  86. // };
  87.  
  88. int main() {
  89. ios::iostate status;
  90. while (true) {
  91. int DIMENSION;
  92. cout << "Radius (STR+D = Ende):";
  93. cin >> DIMENSION;
  94. status = cin.rdstate();
  95. if (cin.eof()) break;
  96. if (status) {
  97. cin.clear();
  98. cin.get();
  99. }
  100. else {
  101. PointMap* map = new PointMap(DIMENSION);
  102. // Hexagonale Map
  103. for (int q = -DIMENSION; q <= DIMENSION; ++q) {
  104. int r1 = max(-DIMENSION, -q - DIMENSION);
  105. int r2 = min(DIMENSION, -q + DIMENSION);
  106. for (int r = r1; r <= r2; ++r) map->print(q, r);
  107. }
  108.  
  109. // Dreieck Map
  110. // for (int i = 0; i < DIMENSION; ++i) {
  111. // for (int j = 0; j < i + 1; ++j) map->print(i, j);
  112. // }
  113. delete map;
  114. cout << "*** main function ***\n\n";
  115. }
  116. }
  117. }
Success #stdin #stdout 0s 4520KB
stdin
3
stdout
Radius (STR+D = Ende):q:-3, r:0
q:-3, r:1
q:-3, r:2
q:-3, r:3
q:-2, r:-1
q:-2, r:0
q:-2, r:1
q:-2, r:2
q:-2, r:3
q:-1, r:-2
q:-1, r:-1
q:-1, r:0
q:-1, r:1
q:-1, r:2
q:-1, r:3
q:0, r:-3
q:0, r:-2
q:0, r:-1
q:0, r:0
q:0, r:1
q:0, r:2
q:0, r:3
q:1, r:-3
q:1, r:-2
q:1, r:-1
q:1, r:0
q:1, r:1
q:1, r:2
q:2, r:-3
q:2, r:-2
q:2, r:-1
q:2, r:0
q:2, r:1
q:3, r:-3
q:3, r:-2
q:3, r:-1
q:3, r:0
*** Ready constuctor ***

q:-3, r:0
q:-3, r:1
q:-3, r:2
q:-3, r:3
q:-2, r:-1
q:-2, r:0
q:-2, r:1
q:-2, r:2
q:-2, r:3
q:-1, r:-2
q:-1, r:-1
q:-1, r:0
q:-1, r:1
q:-1, r:2
q:-1, r:3
q:0, r:-3
q:0, r:-2
q:0, r:-1
q:0, r:0
q:0, r:1
q:0, r:2
q:0, r:3
q:1, r:-3
q:1, r:-2
q:1, r:-1
q:1, r:0
q:1, r:1
q:1, r:2
q:2, r:-3
q:2, r:-2
q:2, r:-1
q:2, r:0
q:2, r:1
q:3, r:-3
q:3, r:-2
q:3, r:-1
q:3, r:0
*** Delete Map ***

q:-3, r:0
q:-3, r:1
q:-3, r:2
q:-3, r:3
q:-2, r:-1
q:-2, r:0
q:-2, r:1
q:-2, r:2
q:-2, r:3
q:-1, r:-2
q:-1, r:-1
q:-1, r:0
q:-1, r:1
q:-1, r:2
q:-1, r:3
q:0, r:-3
q:0, r:-2
q:0, r:-1
q:0, r:0
q:0, r:1
q:0, r:2
q:0, r:3
q:1, r:-3
q:1, r:-2
q:1, r:-1
q:1, r:0
q:1, r:1
q:1, r:2
q:2, r:-3
q:2, r:-2
q:2, r:-1
q:2, r:0
q:2, r:1
q:3, r:-3
q:3, r:-2
q:3, r:-1
q:3, r:0
*** main function ***

Radius (STR+D = Ende):