Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <algorithm>
- using namespace std;
- // x >= y ? 1 : 0
- int x_ge_y(const int x, const int y)
- {
- return abs(abs(x - y) - 1 - abs(y - x - 1)) / 2;
- }
- // x < y ? 1 : 0
- int x_lt_y(const int x, const int y)
- {
- return 1 - x_ge_y(x, y);
- }
- int get_value(const int size, const int x, const int y)
- {
- //coords if point 0;0 is the center of the square
- const int x_centric = x - (size - 1) / 2;
- const int y_centric = y - (size - 1) / 2;
- //half-size and size of current sub-square
- const int current_subsquare_half = max(abs(x_centric), abs(y_centric));
- const int current_subsquare = current_subsquare_half * 2 + 1;
- //coords local for current subsquare (0;0 is top-left)
- const int x_local = x_centric + current_subsquare_half;
- const int y_local = y_centric + current_subsquare_half;
- //top-right = top-right triangle, includes main diagonal; bottom-left = bottom-left triangle
- const int top_right_value = x_ge_y(x_local, y_local) * (x_local + y_local);
- 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));
- //local sub-square value (point 0;0 contains value 0, proceed CW)
- const int inverted_value_local = top_right_value + bottom_left_value;
- //local sub-squre value, if we count from its center (center point contains 1)
- const int inverted_value = current_subsquare * current_subsquare - 1 - inverted_value_local;
- //proper final value
- return size * size - inverted_value;
- }
- int main()
- {
- cout << "------" << endl;
- const int size = 9;
- for (int i = 0; i < size; ++i)
- {
- for (int j = 0; j < size; ++j)
- {
- cout << setw(3) << get_value(size, j, i);
- };
- cout << endl;
- };
- cout << "------" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement