Skip to content

Instantly share code, notes, and snippets.

@miloyip
Last active April 12, 2016 15:30
Show Gist options
  • Save miloyip/035f4ff4a8c0bdf99bd8bcc100a11fcf to your computer and use it in GitHub Desktop.
Save miloyip/035f4ff4a8c0bdf99bd8bcc100a11fcf to your computer and use it in GitHub Desktop.
Equidistance traversal
#include <algorithm>
#include <vector>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
struct Point {
Point(int x, int y) : x(x), y(y), sqDist(x * x + y * y) {}
bool operator<(const Point& rhs) const { return sqDist < rhs.sqDist; }
int x, y, sqDist;
};
int main() {
const int r = 5;
const int size = r * 2 + 1;
vector<Point> points;
for (int y = -r; y <= r; y++)
for (int x = -r; x <= r; x++) {
Point p(x, y);
if (p.sqDist <= r * r)
points.push_back(p);
}
sort(points.begin(), points.end());
const Point* last = 0;
for (auto const& p : points) {
if (last) {
if (p.sqDist == last->sqDist)
cout << ", ";
else
cout << endl;
}
cout << "(" << p.x << "," << p.y << ")";
last = &p;
}
cout << endl << endl;
std::vector<int> image(size * size, -1);
last = 0;
int level = 0;
for (auto const& p : points) {
if (last && p.sqDist != last->sqDist)
level++;
image[(p.y + r) * size + p.x + r] = level;
last = &p;
}
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++)
if (image[y * size + x] == -1)
cout << " ";
else
cout << setw(2) << image[y * size + x];
cout << endl;
}
}
(0,0)
(1,0), (0,1), (0,-1), (-1,0)
(1,-1), (-1,-1), (-1,1), (1,1)
(2,0), (0,2), (-2,0), (0,-2)
(1,2), (-1,2), (1,-2), (-2,1), (-2,-1), (2,-1), (2,1), (-1,-2)
(2,2), (-2,2), (-2,-2), (2,-2)
(0,3), (-3,0), (0,-3), (3,0)
(1,3), (-1,3), (3,1), (-3,1), (-3,-1), (1,-3), (-1,-3), (3,-1)
(2,-3), (-3,-2), (3,-2), (-2,-3), (-3,2), (3,2), (-2,3), (2,3)
(0,-4), (-4,0), (4,0), (0,4)
(-4,1), (-4,-1), (4,1), (1,-4), (4,-1), (-1,-4), (-1,4), (1,4)
(3,-3), (-3,-3), (-3,3), (3,3)
(4,2), (4,-2), (-4,-2), (2,-4), (-4,2), (-2,-4), (-2,4), (2,4)
(-3,-4), (-5,0), (5,0), (4,3), (-3,4), (-4,3), (0,-5), (4,-3), (-4,-3), (3,-4), (3,4), (0,5)
13
131210 9101213
1311 8 7 6 7 81113
12 8 5 4 3 4 5 812
10 7 4 2 1 2 4 710
13 9 6 3 1 0 1 3 6 913
10 7 4 2 1 2 4 710
12 8 5 4 3 4 5 812
1311 8 7 6 7 81113
131210 9101213
13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment