View difference between Paste ID: Eq0E2San and XvmTN651
SHOW: | | - or go back to the newest paste.
1
#include <iostream>
2
#include <iomanip>
3
#include <algorithm>
4
5
using namespace std;
6
7
// x >= y ? 1 : 0
8
int x_ge_y(const int x, const int y)
9
{
10
        return abs(abs(x - y) - 1 - abs(y - x - 1)) / 2;
11
}
12
13
// x < y ? 1 : 0
14
int x_lt_y(const int x, const int y)
15
{
16
        return 1 - x_ge_y(x, y);
17
}
18
19
int get_value(const int size, const int x, const int y)
20
{
21
	//coords if point 0;0 is the center of the square
22
        const int x_centric = x - (size - 1) / 2;
23
        const int y_centric = y - (size - 1) / 2;
24
25
	//half-size and size of current sub-square
26
        const int current_subsquare_half = max(abs(x_centric), abs(y_centric));
27
        const int current_subsquare = current_subsquare_half * 2 + 1;
28
29
	//coords local for current subsquare (0;0 is top-left)
30
        const int x_local = x_centric + current_subsquare_half;
31
        const int y_local = y_centric + current_subsquare_half;
32
33
	//top-right = top-right triangle, includes main diagonal; bottom-left = bottom-left triangle
34
        const int top_right_value = x_ge_y(x_local, y_local) * (x_local + y_local);
35
        const int bottom_left_value = x_lt_y(x_local, y_local) * (2 * (current_subsquare - 1) + ((current_subsquare - 1) - x_local) + ((current_subsquare - 1) - y_local));
36
37
	//local sub-square value (point 0;0 contains value 0, proceed CW)
38
        const int inverted_value_local = top_right_value + bottom_left_value;
39
	//local sub-squre value, if we count from its center (center point contains 1)
40
        const int inverted_value = current_subsquare * current_subsquare - 1 - inverted_value_local;
41
42
	//proper final value
43
        return size * size - inverted_value;
44
}
45
46
int main()
47
{
48
        cout << "------" << endl;
49
50
        const int size = 9;
51
52
        for (int i = 0; i < size; ++i)
53
        {
54
                for (int j = 0; j < size; ++j)
55
                {
56
                        cout << setw(3) << get_value(size, j, i);
57
                };
58
                cout << endl;
59
        };
60
61
        cout << "------" << endl;
62
        return 0;
63
}