Example #1
0
        public void ConstructorQR()
        {
            AxialHexCoord axial = new AxialHexCoord( 1, 2 );

            Assert.That( axial.q, Is.EqualTo( 1 ) );
            Assert.That( axial.r, Is.EqualTo( 2 ) );
        }
Example #2
0
        AxialToPoint(AxialHexCoord hex)
        {
            float x = HexRadius * SQRT_3 * (hex.q + 0.5f * hex.r);
            float y = HexRadius * 1.5f * hex.r;

            return(new Vec2D(x, y));
        }
Example #3
0
        public void OperatorOverloadPlus()
        {
            AxialHexCoord axial = new AxialHexCoord( 1, 2 ) + new AxialHexCoord( 3, 4 );

            Assert.That( axial.q, Is.EqualTo( 4 ) );
            Assert.That( axial.r, Is.EqualTo( 6 ) );
        }
Example #4
0
        public void OperatorOverloadMinus()
        {
            AxialHexCoord axial = new AxialHexCoord( 4, 3 ) - new AxialHexCoord( 1, 2 );

            Assert.That( axial.q, Is.EqualTo( 3 ) );
            Assert.That( axial.r, Is.EqualTo( 1 ) );
        }
Example #5
0
        public void ConstructorParameterless()
        {
            AxialHexCoord axial = new AxialHexCoord();

            Assert.That( axial.q, Is.EqualTo( 0 ) );
            Assert.That( axial.r, Is.EqualTo( 0 ) );
        }
Example #6
0
        /// <summary>
        /// Get the point on the x-z cartesian plane where the center of the given hex is located.
        /// </summary>
        /// <param name="hex">A hex position, represented by an AxialHexCoord.</param>
        /// <returns>A Vec2D point of the hex's position on the x-y cartesian plane.</returns>
        public Vec2D AxialToPoint( AxialHexCoord hex )
        {
            float x = HexRadius * SQRT_3 * ( hex.q + 0.5f * hex.r );
            float y = HexRadius * 1.5f * hex.r;

            return new Vec2D( x, y );
        }
Example #7
0
        public void ToCubic()
        {
            CubicHexCoord cubic = new AxialHexCoord( 1, 2 ).ToCubic();

            Assert.That( cubic.x, Is.EqualTo(  1 ) );
            Assert.That( cubic.y, Is.EqualTo( -3 ) );
            Assert.That( cubic.z, Is.EqualTo(  2 ) );
        }
Example #8
0
        public void OperatorOverloadNotEquals()
        {
            bool isTrue  = new AxialHexCoord( 1, 2 ) != new AxialHexCoord( 3, 4 );
            bool isFalse = new AxialHexCoord( 1, 2 ) != new AxialHexCoord( 1, 2 );

            Assert.That( isTrue,  Is.True  );
            Assert.That( isFalse, Is.False );
        }
Example #9
0
        public void ConstructorAxial()
        {
            AxialHexCoord axial = new AxialHexCoord( 1, 2 );
            FloatAxial floatAxial = new FloatAxial( axial );

            Assert.That( floatAxial.q, Is.InRange<float>( 1f - EPSILON, 1f + EPSILON ) );
            Assert.That( floatAxial.r, Is.InRange<float>( 2f - EPSILON, 2f + EPSILON ) );
        }
Example #10
0
        Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            if (GetType() != obj.GetType())
            {
                return(false);
            }

            AxialHexCoord other = (AxialHexCoord)obj;

            return((this.q == other.q) && (this.r == other.r));
        }
Example #11
0
        PointToDirectionInHex(Vec2D point)
        {
            AxialHexCoord axial = PointToCubic(point).ToAxial();
            //AxialHexCoord axial = PointToAxial( point );

            Vec2D center      = AxialToPoint(axial);
            Vec2D topLeft     = center - new Vec2D(0.5f * SQRT_3 * HexRadius, HexRadius);
            Vec2D fromTopLeft = point - topLeft;

            int hSlice = (int)Math.Floor(fromTopLeft.x / Slice.x);
            int vSlice = (int)Math.Floor(fromTopLeft.y / Slice.y);

            Vec2D withinSlice = new Vec2D(
                fromTopLeft.x % Slice.x,
                fromTopLeft.y % Slice.y
                );

            ParityEnum parity = ParityEnum.Even;

            if (((hSlice & 1) + (vSlice & 1)) == 1)
            {
                // One of the two dimensions is odd, otherwise parity is even by default.
                parity = ParityEnum.Odd;
            }

            //
            // Debugging output associated with a horrible bug in PointToCubic()
            //
            //Console.WriteLine( "Point: x: " + point.x + ", y: " + point.y );
            //Console.WriteLine( "Axial: q: " + axial.q + ", r: " + axial.r );
            //Console.WriteLine( "Center: x: " + center.x + ", y: " + center.y );
            //Console.WriteLine( "Top Left: x: " + topLeft.x + ", y: " + topLeft.y );
            //Console.WriteLine( "From Top Left: x: " + fromTopLeft.x + ", y: " + fromTopLeft.y );
            //Console.WriteLine( "Slice: x: " + Slice.x + ", y: " + Slice.y );
            //Console.WriteLine( "Slice Index: x: " + hSlice + ", y: " + vSlice );
            //Console.WriteLine( "Within Slice: x: " + withinSlice.x + ", y: " + withinSlice.y );
            //Console.WriteLine( "Parity: " + parity );

            // Compute and return the triangle by which the point is contained.
            bool          isInvalid = false;
            DirectionEnum direction = DirectionEnum.E;
            TriangleEnum  triangle  = WhichTriangle(withinSlice, parity);


            if (hSlice == 0)
            {
                switch (vSlice)
                {
                case 0:
                    if (triangle == TriangleEnum.Bottom)
                    {
                        direction = DirectionEnum.NW;
                    }
                    else
                    {
                        isInvalid = true;
                    }
                    break;

                case 1:
                    direction = (triangle == TriangleEnum.Top) ? DirectionEnum.NW : DirectionEnum.W;
                    break;

                case 2:
                    direction = (triangle == TriangleEnum.Top) ? DirectionEnum.W  : DirectionEnum.SW;
                    break;

                case 3:
                    if (triangle == TriangleEnum.Top)
                    {
                        direction = DirectionEnum.SW;
                    }
                    else
                    {
                        isInvalid = true;
                    }
                    break;

                default:
                    isInvalid = true;
                    break;
                }
            }
            else             // hSlice == 1
            {
                switch (vSlice)
                {
                case 0:
                    if (triangle == TriangleEnum.Bottom)
                    {
                        direction = DirectionEnum.NE;
                    }
                    else
                    {
                        isInvalid = true;
                    }
                    break;

                case 1:
                    direction = (triangle == TriangleEnum.Top) ? DirectionEnum.NE : DirectionEnum.E;
                    break;

                case 2:
                    direction = (triangle == TriangleEnum.Top) ? DirectionEnum.E  : DirectionEnum.SE;
                    break;

                case 3:
                    if (triangle == TriangleEnum.Top)
                    {
                        direction = DirectionEnum.SE;
                    }
                    else
                    {
                        isInvalid = true;
                    }
                    break;

                default:
                    isInvalid = true;
                    break;
                }
            }

            //
            // Debugging output associated with a horrible bug in PointToCubic()
            //
            //Console.WriteLine( "Triangle: " + triangle );
            //Console.WriteLine( "Direction: " + direction );
            //Console.WriteLine( "" );
            //Console.WriteLine( "" );

            if (isInvalid)
            {
                throw new InvalidOperationException("This should never happen!");
            }
            else
            {
                return(direction);
            }
        }
Example #12
0
 FloatAxial(AxialHexCoord axial)
 {
     this.q = (float)axial.q;
     this.r = (float)axial.r;
 }
Example #13
0
 /// <summary>
 /// Create a new FloatAxial given a AxialHexCoord.
 /// </summary>
 /// <param name="axial">Any AxialHexCoord representing a hex.</param>
 public FloatAxial( AxialHexCoord axial )
 {
     this.q = (float)axial.q;
     this.r = (float)axial.r;
 }