Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #ifndef SPIR2_H
  2. #define SPIR2_H
  3. #include <iostream>
  4. #include <array>
  5. #include "catch.hpp"
  6. #include <cmath>
  7.  
  8. namespace sp2 {
  9.  
  10. enum Corner
  11. {
  12.     TOP   = 1,
  13.     BOTTOM = 2,
  14.     LEFT = 4,
  15.     RIGHT = 8
  16. };
  17.  
  18. inline Corner operator|(Corner a, Corner b)
  19. {
  20.     return static_cast<Corner>(static_cast<int>(a) | static_cast<int>(b));
  21. }
  22.  
  23. std::pair<size_t, size_t> getCorner(size_t height, size_t width, size_t fullCycles, Corner corner) {
  24.     size_t row, column;
  25.     if (corner & LEFT) {
  26.         column = fullCycles;
  27.     }
  28.     else if(corner & RIGHT) {
  29.         column = width - 1 + fullCycles;
  30.     }
  31.     if(corner & TOP) {
  32.         row = fullCycles;
  33.     }
  34.     else if(corner & BOTTOM) {
  35.         row = height -1 + fullCycles;
  36.     }
  37.     return {row, column};
  38. }
  39.  
  40. std::pair<size_t, size_t> getCoordsFromIndex(size_t index, size_t height, size_t width) {
  41.     size_t fullCycles = 0;
  42.     do {
  43.         if(index < width) {
  44.             auto topLeftCorner = getCorner(height, width, fullCycles, TOP | LEFT);
  45.             return {topLeftCorner.first, topLeftCorner.second + index};
  46.         }
  47.         index -= width - 1; //from top left to top right corner
  48.  
  49.         if(index < height - 1) {
  50.             auto topRightCorner = getCorner(height, width, fullCycles, TOP | RIGHT);
  51.             return {topRightCorner.first + index, topRightCorner.second};
  52.         }
  53.         index -= height - 1; //from top right to bottom right corner
  54.  
  55.         if(index < width - 1) {
  56.             auto bottomRightCorner = getCorner(height, width, fullCycles, BOTTOM | RIGHT);
  57.             return {bottomRightCorner.first, bottomRightCorner.second - index};
  58.         }
  59.         index -= width - 1; //from bottom right to bottom left corner
  60.  
  61.         if(index < height - 1) {
  62.             auto bottomLeftCorner = getCorner(height, width, fullCycles, BOTTOM | LEFT);
  63.             return {bottomLeftCorner.first - index, bottomLeftCorner.second};
  64.         }
  65.         index -= height - 1; //from bottom left to top left corner
  66.  
  67.         width -= 2;
  68.         height -= 2;
  69.         fullCycles++;
  70.  
  71.     } while (true);
  72. }
  73. }
  74.  
  75.  
  76. #endif // SPIR2_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement