Beispiel #1
0
        public HexCubeCoordinates RoundCube(float x, float y, float z)
        {
            var rx = (int)Math.Round(x);
            var ry = (int)Math.Round(y);
            var rz = (int)Math.Round(z);

            //var zero = rx + ry + rz;

            var xDiff = Math.Abs(rx - x);
            var yDiff = Math.Abs(ry - y);
            var zDiff = Math.Abs(rz - z);

            if (xDiff > yDiff && xDiff > zDiff)
            {
                rx = -ry - rz;
            }
            else if (yDiff > zDiff)
            {
                ry = -rx - rz;
            }
            else
            {
                rz = -rx - ry;
            }
            var cube = new HexCubeCoordinates(rx, ry, rz);

            return(cube);
        }
Beispiel #2
0
        public HexCubeCoordinates GetNeighbor(HexCubeCoordinates cube, Direction direction)
        {
            var offset   = GetNeighboringCube(direction);
            var neighbor = cube + offset;

            return(neighbor);
        }
Beispiel #3
0
        public Point2F FromCubeToPixel(HexCubeCoordinates cube)
        {
            var axial = CubeToAxial(cube);
            var pixel = FromAxialToPixel(axial);

            return(pixel);
        }
Beispiel #4
0
        public HexAxialCoordinates CubeToAxial(HexCubeCoordinates cube)
        {
            var q     = cube.X;
            var r     = cube.Z;
            var axial = new HexAxialCoordinates(q, r);

            return(axial);
        }
Beispiel #5
0
        public Point3F Lerp(HexCubeCoordinates fromCube, HexCubeCoordinates toCube, float t)
        {
            var x   = t.Lerp(fromCube.X, toCube.X);
            var y   = t.Lerp(fromCube.Y, toCube.Y);
            var z   = t.Lerp(fromCube.Z, toCube.Z);
            var foo = new Point3F(x, y, z);

            return(foo);
        }
Beispiel #6
0
        public HexCubeCoordinates AxialToCube(HexAxialCoordinates axial)
        {
            var x    = axial.Q;
            var z    = axial.R;
            var y    = -x - z;
            var cube = new HexCubeCoordinates(x, y, z);

            return(cube);
        }
Beispiel #7
0
        public int GetDistance(HexCubeCoordinates fromCube, HexCubeCoordinates toCube)
        {
            var diffX     = Math.Abs(fromCube.X - toCube.X);
            var diffY     = Math.Abs(fromCube.Y - toCube.Y);
            var diffZ     = Math.Abs(fromCube.Z - toCube.Z);
            var distance1 = (diffX + diffY + diffZ) / 2;
            var distance2 = Math.Max(Math.Max(diffX, diffY), diffZ);

            if (distance1 != distance2)
            {
                throw new Exception("distance1 not equal to distance2!");
            }

            return(distance1);
        }
Beispiel #8
0
        public HexCubeCoordinates[] GetAllNeighbors(HexCubeCoordinates cube)
        {
            var neighbors = new List <HexCubeCoordinates>();

            for (var i = 0; i < 8; i++)
            {
                var neighbor = GetNeighbor(cube, (Direction)i);
                if (neighbor != cube)
                {
                    neighbors.Add(neighbor);
                }
            }

            return(neighbors.ToArray());
        }
Beispiel #9
0
        public List <HexCubeCoordinates> GetLine(HexCubeCoordinates fromCube, HexCubeCoordinates toCube)
        {
            var distance = GetDistance(fromCube, toCube);

            var results = new List <HexCubeCoordinates>();

            for (var i = 0; i <= distance; i++)
            {
                var t    = 1.0f / distance * i;
                var lerp = Lerp(fromCube, toCube, t);
                var hex  = RoundCube(lerp.X, lerp.Y, lerp.Z);
                results.Add(hex);
            }

            return(results);
        }
Beispiel #10
0
        public override HexOffsetCoordinates CubeToOffsetCoordinates(HexCubeCoordinates cube)
        {
            var col = cube.X;
            var row = cube.Z;

            row += OffsetCoordinatesType switch
            {
                OffsetCoordinatesType.Even => (col + (col & 1)) / 2,
                OffsetCoordinatesType.Odd => (col - (col & 1)) / 2,
                _ => throw new ArgumentOutOfRangeException(nameof(OffsetCoordinatesType), OffsetCoordinatesType, $"OffsetCoordinatesType {OffsetCoordinatesType} is not supported.")
            };

            var offsetCoordinates = new HexOffsetCoordinates(col, row);

            return(offsetCoordinates);
        }
Beispiel #11
0
        public override HexCubeCoordinates OffsetCoordinatesToCube(HexOffsetCoordinates offset)
        {
            var col = offset.Col;
            var row = offset.Row;

            row -= OffsetCoordinatesType switch
            {
                OffsetCoordinatesType.Even => (col + (col & 1)) / 2,
                OffsetCoordinatesType.Odd => (col - (col & 1)) / 2,
                _ => throw new ArgumentOutOfRangeException(nameof(OffsetCoordinatesType), OffsetCoordinatesType, $"OffsetCoordinatesType {OffsetCoordinatesType} is not supported.")
            };
            var y    = -col - row;
            var cube = new HexCubeCoordinates(col, y, row);

            return(cube);
        }
Beispiel #12
0
        public static HexCubeCoordinates operator +(HexCubeCoordinates a, HexCubeCoordinates b)
        {
            var hexCube = new HexCubeCoordinates(a.X + b.X, a.Y + b.Y, a.Z + b.Z);

            return(hexCube);
        }
Beispiel #13
0
 public Point3F Lerp(HexCubeCoordinates fromCube, HexCubeCoordinates toCube, float t) => _hex.Lerp(fromCube, toCube, t);
Beispiel #14
0
 public int GetDistance(HexCubeCoordinates fromCube, HexCubeCoordinates toCube) => _hex.GetDistance(fromCube, toCube);
Beispiel #15
0
 public Point2F FromCubeToPixel(HexCubeCoordinates cube) => _hex.FromCubeToPixel(cube);
Beispiel #16
0
 public HexCubeCoordinates GetNeighbor(HexCubeCoordinates cube, Direction direction) => _hex.GetNeighbor(cube, direction);
Beispiel #17
0
 public List <HexCubeCoordinates> GetLine(HexCubeCoordinates fromCube, HexCubeCoordinates toCube) => _hex.GetLine(fromCube, toCube);
Beispiel #18
0
 public HexCubeCoordinates[] GetAllNeighbors(HexCubeCoordinates cube) => _hex.GetAllNeighbors(cube);
Beispiel #19
0
 public HexAxialCoordinates CubeToAxial(HexCubeCoordinates cube) => _hex.CubeToAxial(cube);
Beispiel #20
0
 public HexOffsetCoordinates CubeToOffsetCoordinates(HexCubeCoordinates cube) => _hex.CubeToOffsetCoordinates(cube);
Beispiel #21
0
        public HexCubeCoordinates Scale(int k)
        {
            var scaledCube = new HexCubeCoordinates(X * k, Y * k, Z * k);

            return(scaledCube);
        }
Beispiel #22
0
 public abstract HexOffsetCoordinates CubeToOffsetCoordinates(HexCubeCoordinates cube);