コード例 #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);
        }
コード例 #2
0
        public HexCubeCoordinates GetNeighbor(HexCubeCoordinates cube, Direction direction)
        {
            var offset   = GetNeighboringCube(direction);
            var neighbor = cube + offset;

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

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

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

            return(scaledCube);
        }
コード例 #22
0
 public abstract HexOffsetCoordinates CubeToOffsetCoordinates(HexCubeCoordinates cube);