-
Notifications
You must be signed in to change notification settings - Fork 0
/
ICoordinateSystem.cs
50 lines (42 loc) · 1.59 KB
/
ICoordinateSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
namespace AbstractTileGame
{
public static class HexAxisHelpers
{
private const float Sqrt3 = 1.7320508075688772f;
public static (float X, float Y, float Z) CubeRound(in (float x, float y, float z) cube)
{
var rx = (float)Math.Round(cube.x);
var ry = (float)Math.Round(cube.y);
var rz = (float)Math.Round(cube.z);
float dx = Math.Abs(rx - cube.x);
float dy = Math.Abs(ry - cube.y);
float dz = Math.Abs(rz - cube.z);
if (dx > dy && dx > dz)
return (-ry - rz, ry, rz);
return dy > dz ? (rx, -rx - rz, rz) : (rx, ry, -rx - ry);
}
public static (float X, float Y, float Z) CartesianToCube(in (float x, float y) cartesian)
{
return AxialToCube(CartesianToAxial(cartesian));
}
public static (float Q, float R) CartesianToAxial(in (float x, float y) cartesian)
{
float q = (Sqrt3 * cartesian.x + cartesian.y) / 3;
float r = (2 * cartesian.y) / 3;
return (q, r);
}
public static (float X, float Y) RoundAxial(in (float Q, float R) axial)
{
return CubeToAxial(CubeRound(AxialToCube(axial)));
}
public static (float Q, float R) CubeToAxial(in (float x, float y, float z) cube)
{
return (cube.x, cube.z);
}
public static (float X, float Y, float Z) AxialToCube(in (float Q, float R) axial)
{
return (axial.Q, -axial.Q - axial.R, axial.R);
}
}
}