Created
November 7, 2016 02:19
-
-
Save CosineP/e5df981c77889d0c57ea082594dbc9ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Code: | |
int dx = Math.floor((x - y + (current.y&1)) / 2); | |
int dy = x + y; | |
Description: | |
The dx is most complex. If this is your diamond: | |
1 2 3 | |
4 5 6 | |
7 8 9 | |
Then this is your zigzag (without the leading spaces for graphical representation) if you are on an even row | |
- - 1 - - | |
- 4 2 - - | |
- 7 5 3 - | |
- 8 6 - - | |
- - 9 - - | |
And this on an odd row: | |
- - 1 - - | |
- - 4 2 - | |
- 7 5 3 - | |
- - 8 6 - | |
- - 9 - - | |
Let's start with this simplified set (which ignores zigzag): | |
x = (x - y) / 2 | |
y = x + y | |
This creates this graph: | |
- - 1 - - | |
- -4-2- - | |
- 7 5 3 - | |
- -8-6- - | |
- - 9 - - | |
We'll look at the y first. Let's look at just y: | |
1 | |
2 4 | |
3 5 7 | |
6 8 | |
9 | |
For every increase in x, we go down 1, and for every increase in y, we go down one more. Thus dy=x+y. | |
The x is essentially the same, however we subtract y instead and divide by two. | |
Why divide by two? Because we're isometric and two rows down we've only moved back by one. The stray 1/2s are resolved later. | |
If you look at the diagram both work out. | |
Now look at the current graph versus desired - the current has 1/2 x positions where we'd put spaces. | |
If we're starting on an even row, we want them to round down, so we take Math.floor of the final result. | |
However if on an odd start, we want them to round up because we're already on a space. | |
So we add 1/2 in these cases, so that Math.floor will give us one higher. Simplified as (current.y&1)/2, giving: | |
dx = Math.floor((x-y)/2 + (current.y&1)/2) | |
Simplify by bringing out the 1/2: | |
int dx = Math.floor((x - y + (current.y&1)) / 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment