Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef SPIR2_H
- #define SPIR2_H
- #include <iostream>
- #include <array>
- #include "catch.hpp"
- #include <cmath>
- namespace sp2 {
- enum Corner
- {
- TOP = 1,
- BOTTOM = 2,
- LEFT = 4,
- RIGHT = 8
- };
- inline Corner operator|(Corner a, Corner b)
- {
- return static_cast<Corner>(static_cast<int>(a) | static_cast<int>(b));
- }
- std::pair<size_t, size_t> getCorner(size_t height, size_t width, size_t fullCycles, Corner corner) {
- size_t row, column;
- if (corner & LEFT) {
- column = fullCycles;
- }
- else if(corner & RIGHT) {
- column = width - 1 + fullCycles;
- }
- if(corner & TOP) {
- row = fullCycles;
- }
- else if(corner & BOTTOM) {
- row = height -1 + fullCycles;
- }
- return {row, column};
- }
- std::pair<size_t, size_t> getCoordsFromIndex(size_t index, size_t height, size_t width) {
- size_t fullCycles = 0;
- do {
- if(index < width) {
- auto topLeftCorner = getCorner(height, width, fullCycles, TOP | LEFT);
- return {topLeftCorner.first, topLeftCorner.second + index};
- }
- index -= width - 1; //from top left to top right corner
- if(index < height - 1) {
- auto topRightCorner = getCorner(height, width, fullCycles, TOP | RIGHT);
- return {topRightCorner.first + index, topRightCorner.second};
- }
- index -= height - 1; //from top right to bottom right corner
- if(index < width - 1) {
- auto bottomRightCorner = getCorner(height, width, fullCycles, BOTTOM | RIGHT);
- return {bottomRightCorner.first, bottomRightCorner.second - index};
- }
- index -= width - 1; //from bottom right to bottom left corner
- if(index < height - 1) {
- auto bottomLeftCorner = getCorner(height, width, fullCycles, BOTTOM | LEFT);
- return {bottomLeftCorner.first - index, bottomLeftCorner.second};
- }
- index -= height - 1; //from bottom left to top left corner
- width -= 2;
- height -= 2;
- fullCycles++;
- } while (true);
- }
- }
- #endif // SPIR2_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement