-
Notifications
You must be signed in to change notification settings - Fork 0
Landscape
Landscape is a 6-directional matrix. Coordinates are written as X-Y. Here is example of 4-3 landscape matrix:
Landscape is presented by a coded string containing the chars from base64 notation.
“A” means 0
“a” means 26
“0” means 52
“+” means 62
“/” means 63
Symbols 0-62 are used to identify province indexes, symbol 63 (“/”) is used to identify sea area.
Example landscape:
Legend:
blue region is sea,
yellow is province 0,
purple is province 1,
green is province 2
This can be presented by the next code: “//CCAACCABBB”. By rows:
//CC AACC ABBB
Trigonometry summary
We have 6 directions. Let’s code them the next way:
0 – Right
1 – Up-Right
2 – Up-Left
3 – Left
4 – Down-Left
5 – Down-Right
Let’s define arrays of direction sinuses/cosines:
cos = [1, 1 – D, -D, -1, -D, 1 – D];
sin = [0, -1, -1, 0, 1, 1];
where D = Y mod 2.
Verify formulas
For even rows, let’s check the neighbours of 2-1 cell:
D = 1 mod 2 = 1
Direction | Formula | Result |
0 – Right | (2 + 1)-(1 + 0) | 3-1 |
1 – Up-Right | (2 + 1 – D)-(1 – 1) | 2-0 |
2 – Up-Left | (2 – D)-(1 – 1) | 1-0 |
3 – Left | (2 – 1)-(1 + 0) | 1-1 |
4 – Down-Left | (2 – D)-(1 + 1) | 1-2 |
5 – Down-Right | (2 + 1 – D)-(1 + 1) | 2-2 |
Success!
For odd rows, let’s check the neighbours of 1-2 cell
D = 2 mod 2 = 0
Direction | Formula | Result |
0 – Right | (1 + 1)-(2 + 0) | 2-2 |
1 – Up-Right | (1 + 1 – D)-(2 – 1) | 2-1 |
2 – Up-Left | (1 – D)-(2 – 1) | 1-1 |
3 – Left | (1 – 1)-(2 + 0) | 0-2 |
4 – Down-Left | (1 – D)-(2 + 1) | 1-3 |
5 – Down-Right | (1 + 1 – D)-(2 + 1) | 2-3 |
Success!
Implementation
public class Dir6 { private static const COS :Array = [ 1, 1, 0, -1, 0, 1]; private static const SIN :Array = [ 0, -1, -1, 0, 1, 1]; private static const COS_D :Array = [ 0, -1, -1, 0, -1, -1]; public static function getTargetX(x:int, y:int, d:int):int { return x + COS[d] + COS_D[d] * (y % 2); } public static function getTargetY(x:int, y:int, d:int):int { return y + SIN[d]; } }