Number spiral


C++


#include <iostream>
using namespace std;

// Vector
struct Vec {
	int x, y;
	Vec() {}
	Vec(int x, int y) : x(x), y(y) {}
	void operator +=(Vec b) {
		x += b.x;
		y += b.y;
	}
	Vec& operator *=(int s) {
		x *= s;
		y *= s;
	}
};

// Iterator
struct It {
	Vec p, d;
	int n, i;
	It() {
		p = Vec(0, 0);
		d = Vec(1, 0);
		n = i = 2;
	};
	void next() {
		p += d;
		--i;
		if(i * 2 % n == 0) {
			swap(d.x, d.y);
		}
		if(i == 0) {
			d *= -1;
			n += 2;
			i = n;
		}
	}
};

int main() {
	It it;
	for(int i = 0; i < 20; ++i) {
		cout << "(" << it.p.x << ", "<< it.p.y << ")--";
		it.next();
	}
}

Parent topics