public IActionResult Get([FromQuery] GridCellMapper gridMapper)
        {
            try
            {
                //First Convert from the mapped valudes to actual numeric grid values
                GridCellPosition position = new GridCellPosition(gridMapper.GetNumericColumn(),
                                                                 gridMapper.GetNumericRow());

                //Check that the position is valid
                if (_shapeProcessor.ValidateGridCellPosition(position))
                {
                    //Yep, all's Ok - go get the shape
                    IShape triangle = _shapeProcessor.GetShape(position);
                    return(Ok(triangle));
                }
                else
                {
                    //Not found
                    return(NotFound());
                }
            }
            catch (ArgumentOutOfRangeException ex)
            {
                //Out of range - means not found
                _logger.LogError(ex.Message);
                return(NotFound());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
            }

            //Something has gone wrong.
            return(BadRequest());
        }
Exemple #2
0
        private XYCoordinate CalculateUprightTriangleVertex3(GridCellPosition gridCellPosition)
        {
            int x = (gridCellPosition.Column + 1) * 5;
            int y = gridCellPosition.Row * 10;

            return(new XYCoordinate(x, y));
        }
Exemple #3
0
        private XYCoordinate CalculateInvertedTriangleVertex2(GridCellPosition gridCellPosition)
        {
            int x = gridCellPosition.Column * 5;
            int y = gridCellPosition.Row * 10;

            return(new XYCoordinate(x, y));
        }
Exemple #4
0
        private XYCoordinate CalculateInvertedTriangleVertex3(GridCellPosition gridCellPosition)
        {
            int x = (gridCellPosition.Column - 2) * 5;
            int y = (gridCellPosition.Row - 1) * 10;

            return(new XYCoordinate(x, y));
        }
Exemple #5
0
 private Triangle CreateInvertedRightAngleTriangle(GridCellPosition gridCellPosition)
 {
     return(CreateTriangle(
                CalculateInvertedTriangleVertex1(gridCellPosition),
                CalculateInvertedTriangleVertex2(gridCellPosition),
                CalculateInvertedTriangleVertex3(gridCellPosition)
                ));
 }
Exemple #6
0
        /// <summary>
        /// For a given shape, get the grid position it's in
        /// </summary>
        /// <param name="gridCellPosition"></param>
        /// <returns></returns>
        public IShape GetShape(GridCellPosition gridCellPosition)
        {
            if (!ValidateGridCellPosition(gridCellPosition))
            {
                throw new ArgumentOutOfRangeException();
            }

            return(GetTriangleFromPosition(gridCellPosition));
        }
Exemple #7
0
        public void ValidTestGridCellOne()
        {
            int Row    = 1;
            int Column = 1;

            GridCellPosition testPosition = new GridCellPosition(Column, Row);

            Assert.AreEqual(Row, testPosition.Row);
            Assert.AreEqual(Column, testPosition.Column);
        }
        public void InvalidGetShapeOutsideBoundaryTest_Col12_Row7()
        {
            GridCellPosition gridPos = new GridCellPosition(12, 7);

            Triangle actual;

            Assert.ThrowsException <ArgumentOutOfRangeException>(() =>
                                                                 actual = (Triangle)_gridShapeProcessor.GetShape(gridPos)
                                                                 );
        }
Exemple #9
0
        public void ValidTestGridProperties()
        {
            int Row    = 4;
            int Column = 7;

            GridCellPosition testPosition = new GridCellPosition(Column, Row);

            Assert.AreEqual(Row, testPosition.Row);
            Assert.AreEqual(Column, testPosition.Column);
        }
Exemple #10
0
        public void InvalidTestGridZero()
        {
            int Row    = 0;
            int Column = 0;

            GridCellPosition testPosition;

            Assert.ThrowsException <ArgumentOutOfRangeException>(() =>
                                                                 testPosition = new GridCellPosition(Column, Row)
                                                                 );
        }
Exemple #11
0
        public void InvalidTestNegativeColumn()
        {
            int Row    = 4;
            int Column = -1;

            GridCellPosition testPosition;

            Assert.ThrowsException <ArgumentOutOfRangeException>(() =>
                                                                 testPosition = new GridCellPosition(Column, Row)
                                                                 );
        }
Exemple #12
0
        /// <summary>
        /// Validate the Grid Position Provided
        /// </summary>
        /// <param name="gridCellPosition"></param>
        /// <returns>true if the grid is valid</returns>
        public bool ValidateGridCellPosition(GridCellPosition gridCellPosition)
        {
            if (gridCellPosition.Column < MIN_COLUMN_LENGTH ||
                gridCellPosition.Column > MAX_COLUMN_LENGTH ||
                gridCellPosition.Row < MIN_ROW_HEIGHT ||
                gridCellPosition.Row > MAX_ROW_HEIGHT)
            {
                return(false);
            }

            return(true);
        }
Exemple #13
0
        private Triangle GetTriangleFromPosition(GridCellPosition gridCellPosition)
        {
            //if it's an even column it's an inverted right angled triangle,
            //else if it's an odd column it's upright

            if ((gridCellPosition.Column % 2) == 0)
            {
                return(CreateInvertedRightAngleTriangle(gridCellPosition));
            }

            return(CreateUprightRightAngleTriangle(gridCellPosition));
        }
        public void ValidGetGridCellPosition_Col9_Row3()
        {
            Triangle triangle = new Triangle(
                new List <XYCoordinate>()
            {
                new XYCoordinate(40, 30),
                new XYCoordinate(40, 20),
                new XYCoordinate(50, 30)
            }
                );

            GridCellPosition expected = new GridCellPosition(9, 3);

            GridCellPosition actual = _gridShapeProcessor.GetGridCellPosition(triangle);

            Assert.IsTrue(GridCellPositionTestHelper.TestGridCellPositionValues(expected, actual));
        }
        public void ValidGetGridCellPositionBoundary_Col12_Row6()
        {
            Triangle triangle = new Triangle(
                new List <XYCoordinate>()
            {
                new XYCoordinate(60, 50),
                new XYCoordinate(60, 60),
                new XYCoordinate(50, 50)
            }
                );

            GridCellPosition expected = new GridCellPosition(12, 6);

            GridCellPosition actual = _gridShapeProcessor.GetGridCellPosition(triangle);

            Assert.IsTrue(GridCellPositionTestHelper.TestGridCellPositionValues(expected, actual));
        }
        public void ValidGetShapeTest_Col9_Row3()
        {
            GridCellPosition gridPos = new GridCellPosition(9, 3);

            Triangle expected = new Triangle(
                new List <XYCoordinate>()
            {
                new XYCoordinate(40, 30),
                new XYCoordinate(40, 20),
                new XYCoordinate(50, 30)
            }
                );

            Triangle actual = (Triangle)_gridShapeProcessor.GetShape(gridPos);

            Assert.IsTrue(TriangleTestHelper.TestTriangleValues(expected, actual));
        }
        public void ValidGetShapeBoundaryTest_Col1_Row6()
        {
            GridCellPosition gridPos = new GridCellPosition(1, 6);

            Triangle expected = new Triangle(
                new List <XYCoordinate>()
            {
                new XYCoordinate(0, 60),
                new XYCoordinate(0, 50),
                new XYCoordinate(10, 60)
            }
                );

            Triangle actual = (Triangle)_gridShapeProcessor.GetShape(gridPos);

            Assert.IsTrue(TriangleTestHelper.TestTriangleValues(expected, actual));
        }
        public IActionResult Post([FromBody] Triangle triangle)
        {
            try
            {
                //Check that the triangle is valid
                if (_shapeProcessor.ValidateShape(triangle))
                {
                    //Yep, all's Ok
                    GridCellPosition position = _shapeProcessor.GetGridCellPosition(triangle);

                    //Convert from numeric grid, to mapped grid values
                    GridCellMapper gridMapper = new GridCellMapper();
                    gridMapper.SetGridMappedValues(position.Row, position.Column);

                    return(Created(Request.Path.ToString(), gridMapper));
                }
                else
                {
                    //Not found
                    return(NotFound());
                }
            }
            catch (ArgumentOutOfRangeException ex)
            {
                //Out of range - means not found
                _logger.LogError(ex.Message);
                return(NotFound());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
            }

            //Something has gone wrong.
            return(BadRequest());
        }
Exemple #19
0
 public static bool TestGridCellPositionValues(GridCellPosition expected, GridCellPosition actual)
 {
     return(expected.Column == actual.Column &&
            expected.Row == actual.Row);
 }
        public void ValidGridCellPositionTest_Col9_Row3()
        {
            GridCellPosition gridPos = new GridCellPosition(9, 3);

            Assert.IsTrue(_gridShapeProcessor.ValidateGridCellPosition(gridPos));
        }
        public void ValidGridCellPositionBoundaryTest_Col12_Row6()
        {
            GridCellPosition gridPos = new GridCellPosition(12, 6);

            Assert.IsTrue(_gridShapeProcessor.ValidateGridCellPosition(gridPos));
        }
        public void InvalidGridCellPositionOutsideBoundaryTest_Col12_Row7()
        {
            GridCellPosition gridPos = new GridCellPosition(12, 7);

            Assert.IsFalse(_gridShapeProcessor.ValidateGridCellPosition(gridPos));
        }