Ejemplo n.º 1
0
        public void AddTwoShapes()
        {
            ShapeFactory factory   = new ShapeFactory();
            IShape       circle    = factory.Create("Circle", 2.0);
            IShape       rectangle = factory.Create("Rectangle", 2.0, 11.5);

            Assert.IsNotNull(circle);
            Assert.IsNotNull(rectangle);
        }
Ejemplo n.º 2
0
        // deserialize
        private void Deserialize()
        {
            if (!File.Exists("figures.txt"))
            {
                MessageBox.Show("Unexist figures.txt", "alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new NotImplementedException();
            }

            var f = new StreamReader("figures.txt");

            _shapes.Items.Clear();
            while (!f.EndOfStream)
            {
                string   s    = f.ReadLine();
                string[] data = s.Split(new char[] { ',' });
                try
                {
                    float  x      = float.Parse(data[1], CultureInfo.InvariantCulture.NumberFormat);
                    float  y      = float.Parse(data[2], CultureInfo.InvariantCulture.NumberFormat);
                    float  w      = float.Parse(data[3], CultureInfo.InvariantCulture.NumberFormat);
                    float  h      = float.Parse(data[4], CultureInfo.InvariantCulture.NumberFormat);
                    Point  a      = new Point((int)x, (int)y);
                    Point  b      = new Point((int)w, (int)h);
                    String type   = data[0];
                    var    figure = ShapeFactory.Create(type, a, b);
                    _shapes.Add(figure);
                }
                catch { }
            }
            f.Close();
        }
Ejemplo n.º 3
0
        private void BuildMovementService(MoveArea moveArea)
        {
            var shape = ShapeFactory <int> .Create(moveArea.Shape);

            var validHeight   = int.TryParse(moveArea.Dimension.Height, out int height);
            var validWidth    = int.TryParse(moveArea.Dimension.Width, out int width);
            var validMoveStep = int.TryParse(moveArea.MoveStep, out int moveStep);

            if (!validHeight || !validWidth || !validMoveStep || height == 0 || width == 0 || moveStep == 0)
            {
                throw new ArgumentException("Invalid dimension for the move area");
            }

            shape.Height = height;
            shape.Width  = width;


            _service = new MovementService <int, int>
                       (
                new Robo <int, int>(shape)
            {
                Step = moveStep
            }
                       );
        }
Ejemplo n.º 4
0
        public void CallWithInvalidArguments()
        {
            ShapeFactory factory = new ShapeFactory();

            Assert.Throws <ArgumentException>(
                () => factory.Create("Rectangle", "wrong", "arguments")
                );
        }
Ejemplo n.º 5
0
        public void AddSingleRectangle()
        {
            ShapeFactory factory   = new ShapeFactory();
            IShape       rectangle = factory.Create("Rectangle", 2.0, 11.5);

            Assert.IsNotNull(rectangle);
            Assert.That(rectangle.GetArea(), Is.EqualTo(23.0));
        }
Ejemplo n.º 6
0
        public void AddSingleCircle()
        {
            ShapeFactory factory = new ShapeFactory();
            IShape       circle  = factory.Create("Circle", 2.0);

            Assert.IsNotNull(circle);
            Assert.That(circle.GetArea(), Is.EqualTo(Math.PI * 4));
        }
Ejemplo n.º 7
0
        public void testReportForOneSquare()
        {
            Shapes shapes = new Shapes(new List <ShapeBase>
            {
                ShapeFactory.Create(ShapeType.Square, 1)
            }, Language.SP);

            Assert.AreEqual("<h1>Reporte de figuras</h1><br/>1 Cuadrado Área 1 Perímetro 4<br/>TOTAL:<br/>1 figuras Perímetro 4 Área 1", shapes.GenerateHtml());
        }
Ejemplo n.º 8
0
        public void AddNewWorkerByClient()
        {
            ShapeFactory factory = new ShapeFactory();

            factory.AddWorker(new SquareFactoryWorker());

            IShape square = factory.Create("Square", 12.0);

            Assert.IsNotNull(square);
            Assert.That(square.GetArea(), Is.EqualTo(144.0));
        }
Ejemplo n.º 9
0
 public override IShape CreateShape(TItem item)
 {
     if (item is TEdge)
     {
         return(ShapeFactory.Create <IVectorShape>());
     }
     else
     {
         return(ShapeFactory.Create <IRoundedRectangleShape>());
     }
 }
Ejemplo n.º 10
0
        public void ShouldCreateRectangleByName()
        {
            var expectedShapeType = typeof(Rectangle);
            var shapeType         = "Rectangle";

            var shapeFactory = new ShapeFactory();

            var shape = shapeFactory.Create(shapeType);

            Assert.AreSame(expectedShapeType, shape.GetType());
        }
Ejemplo n.º 11
0
        public void testReportForMoreShapesAndEnglishLanguage()
        {
            Shapes shapes = new Shapes(new List <ShapeBase>
            {
                ShapeFactory.Create(ShapeType.Circle, 1),
                ShapeFactory.Create(ShapeType.Square, 1),
                ShapeFactory.Create(ShapeType.EquilateralTriangle, 2),
                ShapeFactory.Create(ShapeType.Square, 2),
                ShapeFactory.Create(ShapeType.Square, 3),
                ShapeFactory.Create(ShapeType.Circle, 2)
            }, Language.EN);

            Assert.AreEqual("<h1>Shapes report</h1><br/>3 Squares Area 14 Perimeter 24<br/>2 Circles Area 3,93 Perimeter 9,42<br/>1 Triangle Area 1,73 Perimeter 6<br/>TOTAL:<br/>6 shapes Perimeter 39,42 Area 19,66", shapes.GenerateHtml());
        }
Ejemplo n.º 12
0
 private void MainForm_MouseDown(object sender, MouseEventArgs e)
 {
     if (e.Button == MouseButtons.Left)
     {
         isMouseLeftButtonDown = true;
         currentShape          = ShapeFactory.Create(currentType);
         if (currentShape != null)
         {
             currentShape.Color     = currentColor;
             currentShape.Thickness = currentThick;
             currentShape.StartX    = currentShape.EndX = e.X;
             currentShape.StartY    = currentShape.EndY = e.Y;
         }
     }
 }
Ejemplo n.º 13
0
        public void Create_A_New_Cube_When_Parameters_Are_Cube_Dimensions(float x, float y, float side)
        {
            //Arrange
            var cubeDimensions      = new CubeDimensions(new X(x), new Y(y), new Side(side));
            var intersectionService = Substitute.For <IIntersectionService>();

            //Act
            var cube = ShapeFactory.Create(intersectionService, cubeDimensions);

            //Assert
            cube.Should().BeOfType(typeof(Cube));
            ((Cube)cube).X.Value.Should().Be(x);
            ((Cube)cube).Y.Value.Should().Be(y);
            ((Cube)cube).Side.Value.Should().Be(side);
        }
Ejemplo n.º 14
0
        private static void Main(string[] args)
        {
            DependencyInyection();
            var cube1 = ShapeFactory.Create(_intersectionService, new CubeDimensions(new X(0), new Y(0), new Side(10)));
            var cube2 = ShapeFactory.Create(_intersectionService, new CubeDimensions(new X(5), new Y(5), new Side(7)));

            Console.WriteLine("Shape Intersection");
            Console.WriteLine($"-----------------");
            Console.WriteLine($"Shape 1: {cube1}");
            Console.WriteLine($"Shape 2: {cube2}");
            Console.WriteLine();
            Console.WriteLine($"Intersection result: \n");
            Console.WriteLine($"\t{cube1.IntersectWith(cube2)}");
            Console.ReadKey();
        }
Ejemplo n.º 15
0
        private void btnAddShape_Click(object sender, EventArgs e)
        {
            Shape3D newShape = null;
            string  shape    = cboxShape.SelectedItem.ToString();

            try
            {
                newShape = ShapeFactory.Create(shape, _mType, Double.Parse(txtPara1.Text), Double.Parse(txtPara2.Text));
            }
            catch (FormatException ef)
            {
                MessageBox.Show(ef.Message);
            }
            catch (NegativeException en)
            {
                MessageBox.Show(en.Message);
            }
            finally
            {
                if (newShape != null)
                {
                    shapeArr.Add(newShape);
                    txtAmountOfShape.Text  = Shape3D.Amount.ToString();
                    txtBallAmount.Text     = Ball.Amount.ToString();
                    txtCubeAmount.Text     = Cube.Amount.ToString();
                    txtCylinderAmount.Text = Cylinder.Amount.ToString();
                    txtPyramidAmount.Text  = Pyramid.Amount.ToString();
                    ShowAllShapeInfo(txtMessage);
                }
            }
            //switch (shape)
            //{
            //    case "球":
            //        shapeArr.Add(new Ball(Double.Parse(txtPara1.Text), _mType));    // new 出 Ball 物件, 並將其加入 shapeArr中
            //        break;
            //    case "立方體":
            //        shapeArr.Add(new Cube(Double.Parse(txtPara1.Text), _mType));
            //        break;
            //    case "圓柱體":
            //        shapeArr.Add(new Cylinder(Double.Parse(txtPara1.Text), Double.Parse(txtPara2.Text), _mType));
            //        break;
            //    case "金字塔":
            //        shapeArr.Add(new Pyramid(Double.Parse(txtPara1.Text), Double.Parse(txtPara2.Text), _mType));
            //        break;
            //    default:
            //        break;
            //}
        }
Ejemplo n.º 16
0
        public void Intersect_With_Another_Cube(float x1, float y1, float side1, float x2, float y2, float side2)
        {
            //Arrange
            var cubeDimensions1     = new CubeDimensions(new X(x1), new Y(y1), new Side(side1));
            var cubeDimensions2     = new CubeDimensions(new X(x2), new Y(y2), new Side(side2));
            var intersectionService = new IntersectionService();

            var cube1 = ShapeFactory.Create(intersectionService, cubeDimensions1);
            var cube2 = ShapeFactory.Create(intersectionService, cubeDimensions2);

            //Act
            var shapeIntersection = cube1.IntersectWith(cube2);

            //Assert
            shapeIntersection.ToString().Should().Be($"Intersection result of a Cube (X = {x1}, Y = {y1}, Side = {side1}) with a Cube (X = {x2}, Y = {y2}, Side = {side2})");
        }
Ejemplo n.º 17
0
 //------------------------------------------
 // Select or Create
 //------------------------------------------
 private void OnPanelMousePressed(object sender, MouseEventArgs e)
 {
     if (_drawing)
     {
         SearchForFigure(e.Location);
         _tl = e.Location;
         var figure = ShapeFactory.Create(_shapeName, _tl, _tl);
         if (figure != null)
         {
             _shapes.Add(figure);
         }
         _pressed = true;
     }
     else if (_shapes.Count() > 0)
     {
         Point point = e.Location;
         SearchForFigure(point);
     }
 }
Ejemplo n.º 18
0
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (editorMode && (e.Button == MouseButtons.Left))
            {
                shape.First = figures.Last().First;
                figures.Remove(figures.Last());
                RefreshCanvas();
                pictureBox1.Invalidate();
                pictureBox1.Update();
                shapePoints[1] = e.Location;
                isDraw         = true;
                return;
            }

            if (editorMode && (e.Button == MouseButtons.Right))
            {
                isMove      = true;
                shape.First = figures.Last().First;
                figures.Remove(figures.Last());
                RefreshCanvas();
                pictureBox1.Invalidate();
                pictureBox1.Update();
                offset.X = e.X - shape.First.X;
                offset.Y = e.Y - shape.First.Y;

                shapePoints[0] = new Point(shape.First.X + offset.X, shape.First.Y + offset.Y);
                shapePoints[1] = new Point(shape.Last.X + offset.X, shape.Last.Y + offset.Y);
                isDraw         = true;
            }
            else
            {
                isDraw         = true;
                shapePoints[0] = e.Location;
                if (figuresFactory != null)
                {
                    shape = figuresFactory.Create();
                }
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        /// ship id, ship shape pairs
        /// useful if we only want to draw the shapes
        /// </summary>
        private void BuildShapeRegistry()
        {
            try {
                ShapeRegistry.Clear();

                foreach (string file in fileList)
                {
                    JObject obj = JsonConvert.DeserializeObject <JObject>(File.ReadAllText(file));
                    string  id  = obj?.Value <string>("id");

                    //build the ship's shape
                    ShapeFactory shapeFac = new ShapeFactory();
                    float        scale    = obj.Value <float>("scale");
                    Shape        shape    = shapeFac.Create(obj.Value <JObject>("shape"), scale, SwinGame.PointAt(0, 0));          //done

                    if (id != null && shape != null && !ShapeRegistry.ContainsKey(id))
                    {
                        ShapeRegistry.Add(id, shape);
                    }
                }
            } catch (Exception e) {
                Console.WriteLine($"invalid file {e}");
            }
        }
Ejemplo n.º 20
0
        public void ShapeFactoryTest()
        {
            var    length            = 1;
            var    circleArea        = Math.Pow(length, 2) * Math.PI;
            var    squareArea        = Math.Pow(length, 2);
            double expectedShapeArea = 0;
            var    myFactory         = new ShapeFactory();
            var    shapes            = myFactory.Create(2).RandomShapes(length);

            foreach (var shape in shapes)
            {
                switch (shape.ToString())
                {
                case "ShapeLibrary.Model.Concrete.Circle":
                    expectedShapeArea = circleArea;
                    break;

                case "ShapeLibrary.Model.Concrete.Square":
                    expectedShapeArea = squareArea;
                    break;
                }
                Assert.AreEqual(expectedShapeArea, shape.Area);
            }
        }
Ejemplo n.º 21
0
        public void Execute(IWorld parameter)
        {
            this.lines       = System.IO.File.ReadAllLines(this.fileName, Encoding.UTF8);
            this.currentLine = -1;

            int currentStage    = -1;
            int currentLight    = -1;
            int currentObject   = -1;
            int currentFilter   = -1;
            int numberOfLights  = -1;
            int numberOfObjects = -1;
            int numberOfFilters = -1;
            int lastStage       = -1;
            int lastLight       = -1;
            int lastObject      = -1;
            int lastFilter      = -1;

            while (this.HasNextLine())
            {
                string     line       = this.NextLine();
                Parameters parameters = new Parameters();

                Token token = this.GetToken(line, parameters);

                switch (token)
                {
                case Token.Nothing:
                    return;

                case Token.Empty:
                    continue;

                case Token.Comment:
                    Logger.Debug("Ignore comment: {0}", line);
                    break;

                case Token.BeginElement:
                    string beginElementName = parameters.GetName();

                    switch (beginElementName)
                    {
                    case "stage":
                        int beginState = parameters.GetInt(1);

                        if (currentStage == -1)
                        {
                            currentStage = beginState;

                            Logger.Debug("BeginState({0})", beginState);
                        }

                        break;

                    case "light":
                        int beginLight = parameters.GetInt(1);

                        if (currentLight == -1)
                        {
                            currentLight = beginLight;
                            string lightType = parameters.GetString(2);

                            if (lightType == "ambient")
                            {
                                parameter.Lights.Insert(currentLight, new Light(LightType.AmbientLight));
                            }
                            else
                            {
                                parameter.Lights.Insert(currentLight, new Light(LightType.PointLight));
                            }

                            Logger.Debug("BeginLight({0})", beginLight);
                        }

                        break;

                    case "shape":
                        int beginShape = parameters.GetInt(1);

                        if (currentObject == -1)
                        {
                            currentObject = beginShape;
                            string objectType = parameters.GetString(2);

                            IShape shape = ShapeFactory.Create(objectType);

                            parameter.Shapes.Insert(currentObject, shape);

                            Logger.Debug("BeginShape({0})", beginShape);
                        }

                        break;

                    case "filter":
                        int beginFilter = parameters.GetInt(1);

                        if (currentFilter == -1)
                        {
                            currentFilter = beginFilter;
                            string filterType = parameters.GetString(2);

                            IFilter filter = FilterFactory.Create(filterType);

                            parameter.GetStage <PosProcessing>(currentStage).Filters.Insert(currentFilter, filter);

                            Logger.Debug("BeginFilter({0})", beginFilter);
                        }

                        break;
                    }

                    break;

                case Token.EndElement:
                    string endElementName = parameters.GetName();

                    switch (endElementName)
                    {
                    case "stage":
                        int endStage = parameters.GetInt(1);

                        if (endStage == currentStage)
                        {
                            lastStage    = currentStage;
                            currentStage = -1;

                            Logger.Debug("EndState({0})", endStage);
                        }

                        break;

                    case "light":
                        int endLight = parameters.GetInt(1);

                        if (endLight == currentLight)
                        {
                            lastLight    = currentLight;
                            currentLight = -1;

                            Logger.Debug("EndLight({0})", endLight);
                        }

                        break;

                    case "shape":
                        int endShape = parameters.GetInt(1);

                        if (endShape == currentObject)
                        {
                            lastObject    = currentObject;
                            currentObject = -1;

                            Logger.Debug("EndShape({0})", endShape);
                        }

                        break;

                    case "filter":
                        int endFilter = parameters.GetInt(1);

                        if (endFilter == currentFilter)
                        {
                            lastFilter    = currentFilter;
                            currentFilter = -1;

                            Logger.Debug("EndFilter({0})", endFilter);
                        }

                        break;
                    }

                    break;

                case Token.Camera:
                    IVector eye = parameters.GetVector(0);

                    IVector direction = parameters.GetVector(3);

                    parameter.Camera = new Camera(eye, direction);

                    Logger.Debug("Camera({0}, {1})", eye, direction);
                    break;

                case Token.NumberOfLights:
                    numberOfLights = parameters.GetInt(0);

                    Logger.Debug("Number of Lights: {0}", numberOfLights);
                    break;

                case Token.NumberOfObjects:
                    numberOfObjects = parameters.GetInt(0);

                    Logger.Debug("Number of Objects: {0}", numberOfObjects);
                    break;

                case Token.Attribute:

                    if (currentLight >= 0)
                    {
                        ILight light = parameter.Lights.ElementAt <ILight>(currentLight);

                        light.Parse(parameters);
                    }
                    else if (currentObject >= 0)
                    {
                        IShape shape = parameter.Shapes.ElementAt <IShape>(currentObject);

                        shape.Parse(parameters);
                    }
                    else if (currentFilter >= 0)
                    {
                        IFilter filter = parameter.GetStage <PosProcessing>(currentStage).Filters.ElementAt <IFilter>(currentFilter);

                        filter.Parse(parameters);
                    }
                    else if (currentStage == 2)
                    {
                        parameter.GetStage <Render>(currentStage).RayRender.Parse(parameters);
                    }

                    break;

                case Token.ProgramRender:
                    string renderProgram = parameters.GetString(0);

                    parameter.GetStage <Render>(currentStage).RayRender = new RayTracingRender();

                    break;

                case Token.NumberOfFilters:
                    numberOfFilters = parameters.GetInt(0);

                    break;

                case Token.Output:
                    int    outputWidth  = parameters.GetInt(0);
                    int    outputHeight = parameters.GetInt(1);
                    string outputFormat = parameters.GetString(2);

                    parameter.Image = new Image(outputWidth, outputHeight);

                    if (outputFormat == "s")
                    {
                        // Output to screen
                    }
                    else
                    {
                        parameter.GetStage <Output>(currentStage).Writer = new SceneWriter(outputFormat);

                        Logger.Debug("Output {0}x{1} to file {2}", outputWidth, outputHeight, outputFormat);
                    }

                    break;
                }
            }
        }
Ejemplo n.º 22
0
 public void CreatesNormalTriangle()
 {
     Assert.IsInstanceOf <TriangleShape>(factory.Create(new double[] { 1, 1, 1 }));
 }
Ejemplo n.º 23
0
        public void Shape_Factory_Should_Create_Correct_Shape_Object(string shape, Type type)
        {
            var result = ShapeFactory <int> .Create(shape);

            result.Should().BeOfType(type);
        }
Ejemplo n.º 24
0
        public void Shape_Factory_Should_Throw_ArgumentException_Exception(string shape, string exceptionMessage)
        {
            Action act = () => ShapeFactory <int> .Create(shape);

            act.Should().Throw <ArgumentException>().WithMessage(exceptionMessage);
        }
        public void Test()
        {
            IShape circle = ShapeFactory.Create(ShapeTypes.Circle);

            circle.Draw();
        }