public void Load(string fileName) { using (insideStream = new MemoryStream()) { using (outStream = new FileStream(fileName, FileMode.Open)) { dataInConverter = new TxtLoadAlgorythm(); dataInConverter.Convert(insideStream, outStream); } insideStream.Position = 0; string str; while ((insideStream.Length) != (insideStream.Position)) { str = ReadTagFromStream(); if (str.StartsWith("<Rect>")) { figureFactory.SetFigureType(FigureType.RECT); Figure figure = figureFactory.CreateFigure(); figure.LoadFromStream(insideStream); store.Add(figure); } else if (str.StartsWith("<Line>")) { figureFactory.SetFigureType(FigureType.LINE); Figure figure = figureFactory.CreateFigure(); figure.LoadFromStream(insideStream); store.Add(figure); } else if (str.StartsWith("<Ellipse>")) { figureFactory.SetFigureType(FigureType.ELLIPSE); Figure figure = figureFactory.CreateFigure(); figure.LoadFromStream(insideStream); store.Add(figure); } insideStream.ReadByte(); insideStream.ReadByte(); } } }
public void Execute() { var factory = new FigureFactory(); figure = factory.CreateFigure(code, x, y, width, height); if (isSticky) { figure.SetSticky(); } figures.AddLast(figure); }
public void CheckEqualsCompare() { IFigureFactory figureFactory = new FigureFactory(); IFigure figureA = figureFactory.CreateFigure(3, 5, 7); IFigure figureB = figureFactory.CreateFigure(3, 5, 7); bool result = figureA.Equals(figureB); Assert.AreEqual(result, true, "These triangles have been is equal"); figureB.Set(4, 5, 3); result = figureA.Equals(figureB); Assert.AreEqual(result, false, "These triangles have been is not equal"); figureA.Set(4, 6, 7); figureB.Set(6, 4, 7); result = figureA.Equals(figureB); Assert.AreEqual(result, true, "These triangles have been is equal"); }
public void СheckWithChgangeSidesList() { IFigureFactory figureFactory = new FigureFactory(); IFigure testCircle = figureFactory.CreateFigure(1); Assert.AreEqual(testCircle.Area, 3.14159, 0.0001, "The area of circle should have been equal"); testCircle.FigureSides = new double[] { 2 }; Assert.AreEqual(testCircle.Area, 12.566371, 0.0001, "The area of circle should have been equal"); Assert.AreEqual(testCircle.Type, "circle", "The name of object should have been equal"); IFigure testTriangle = figureFactory.CreateFigure(7, 3, 9); Assert.AreEqual(testTriangle.Area, 8.785642, 0.0001, "The area of triangle should have been equal"); testTriangle.FigureSides = new double[] { 13, 5, 14 }; Assert.AreEqual(testTriangle.Area, 32.496154, 0.0001, "The area of triangle should have been equal"); Assert.AreEqual(testTriangle.Type, "triangle", "The name of object should have been equal"); }
public void CheckSizeUpdateFunctions() { IFigureFactory figureFactory = new FigureFactory(); IFigure circle = figureFactory.CreateFigure(5); Assert.AreEqual(circle.Area, 78.539816, 0.0001, "The area of circle should have been equal"); circle.Set(7); Assert.AreEqual(circle.Area, 153.93804, 0.0001, "The area of circle should have been equal"); Assert.AreEqual(circle.UpdateArea(2), 12.566371, 0.0001, "The area of circle should have been equal"); Assert.AreEqual(circle.FigureSides[0], 2, "The radius of circle should have been equal"); IFigure triangle = figureFactory.CreateFigure(5, 5, 5); Assert.AreEqual(triangle.Area, 10.825318, 0.0001, "The area of triangle should have been equal"); triangle.Set(7, 3, 9); Assert.AreEqual(triangle.Area, 8.785642, 0.0001, "The area of triangle should have been equal"); Assert.AreEqual(triangle.UpdateArea(13, 5, 14), 32.496154, 0.0001, "The area of triangle should have been equal"); Assert.AreEqual(triangle.FigureSides.Sum(), 32, "The sum of sides should have been equal"); }
private Figure CreateFigure(Vector3 anchorPosition) { if (!HasSeed()) { return(null); } var seed = Seeds[0]; Seeds.Remove(seed); Figure figure = FigureFactory.CreateFigure(seed.type, seed, anchorPosition.z); return(figure); }
/// <summary> /// Обработчик нажатия мышки /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void MouseDown(object sender, MouseEventArgs e) { switch (e.Button) { case MouseButtons.Left: if (_polygon == null) { var polygon = FigureFactory.CreateFigure(ToolType.Polygon) as FillableFigure; if (polygon != null) { polygon.LineProperties.Color = _figureParameters.LineColor; polygon.LineProperties.Style = (DashStyle) _figureParameters.LineStyle; polygon.LineProperties.Thickness = _figureParameters.LineThickness; polygon.FillProperty.FillColor = _figureParameters.FillColor; _polygon = polygon; } if (_polygon != null) { _polygon.Points.AddPoint(new PointF(e.X, e.Y)); _polygon.Points.AddPoint(new PointF(e.X, e.Y)); } Canvas.Refresh(); } else { _polygon.Points.AddPoint(new PointF(e.X, e.Y)); Canvas.Refresh(); } break; case MouseButtons.Right: if (_polygon == null) { return; } OnFigureCreated(_polygon); _polygon = null; break; } }
/// <summary> /// Обработчик нажатия мышки /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void MouseDown(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) { return; } _line = FigureFactory.CreateFigure(ToolType.Line); _line.LineProperties.Color = _figureParameters.LineColor; _line.LineProperties.Style = (DashStyle)_figureParameters.LineStyle; _line.LineProperties.Thickness = _figureParameters.LineThickness; _line.Points.AddPoint(new PointF(e.X, e.Y)); _line.Points.AddPoint(new PointF(e.X, e.Y)); Canvas.Refresh(); }
public void LoadFigures() { if (openFileDialog.ShowDialog() == DialogResult.OK) { using (StreamReader sr = new StreamReader(openFileDialog.FileName)) { FigureStorage.Clear(); var count = Convert.ToInt32(sr.ReadLine()); IFigureFactory factory = new FigureFactory(); Figure figure; for (int i = 0; i < count; i++) { var code = sr.ReadLine(); figure = factory.CreateFigure(code); figure.Load(sr); FigureStorage.AddLast(figure); } } } }
/// <summary> /// Обработчик нажатия мышки /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void MouseDown(object sender, MouseEventArgs e) { switch (e.Button) { case MouseButtons.Left: if (_polyline == null) { _polyline = FigureFactory.CreateFigure(ToolType.Polyline); _polyline.LineProperties.Color = _figureParameters.LineColor; _polyline.LineProperties.Style = (DashStyle) _figureParameters.LineStyle; _polyline.LineProperties.Thickness = _figureParameters.LineThickness; _polyline.Points.AddPoint(new PointF(e.X, e.Y)); _polyline.Points.AddPoint(new PointF(e.X, e.Y)); Canvas.Refresh(); } else { _polyline.Points.AddPoint(new PointF(e.X, e.Y)); Canvas.Refresh(); } break; case MouseButtons.Right: if (_polyline == null) { return; } OnFigureCreated(_polyline); _polyline = null; break; default: throw new ArgumentOutOfRangeException(); } }
public void СheckAreaFromList() { List <SFigureTest> figureList = new List <SFigureTest>(); IFigureFactory figureFactory = new FigureFactory(); figureList.Add(new SFigureTest() { Sides = new double[] { 5 }, CheckArea = 78.539816, CheckName = "circle" }); figureList.Add(new SFigureTest() { Sides = new double[] { 2 }, CheckArea = 12.566371, CheckName = "circle" }); figureList.Add(new SFigureTest() { Sides = new double[] { 7 }, CheckArea = 153.93804, CheckName = "circle" }); figureList.Add(new SFigureTest() { Sides = new double[] { 13, 5, 14 }, CheckArea = 32.496154, CheckName = "triangle" }); figureList.Add(new SFigureTest() { Sides = new double[] { 5, 5, 5 }, CheckArea = 10.825318, CheckName = "triangle" }); figureList.Add(new SFigureTest() { Sides = new double[] { 7, 3, 9 }, CheckArea = 8.785642, CheckName = "triangle" }); figureList.ForEach(delegate(SFigureTest figure) { var(sides, checkArea, checkName, checkWeight) = figure; IFigure testFigure = figureFactory.CreateFigure(sides); Assert.AreEqual(testFigure.Area, checkArea, 0.0001, "The area of circle should have been equal"); }); }
public void CreateFigureFromList() { List <SFigureTest> figureList = new List <SFigureTest>(); IFigureFactory figureFactory = new FigureFactory(); figureList.Add(new SFigureTest() { Sides = new double[] { 5 }, CheckName = "circle" }); figureList.Add(new SFigureTest() { Sides = new double[] { 2 }, CheckName = "circle" }); figureList.Add(new SFigureTest() { Sides = new double[] { 7 }, CheckName = "circle" }); figureList.Add(new SFigureTest() { Sides = new double[] { 13, 5, 14 }, CheckName = "triangle" }); figureList.Add(new SFigureTest() { Sides = new double[] { 5, 5, 5 }, CheckName = "triangle" }); figureList.Add(new SFigureTest() { Sides = new double[] { 7, 3, 9 }, CheckName = "triangle" }); figureList.ForEach(delegate(SFigureTest figure) { var(sides, checkArea, checkName, checkWeight) = figure; IFigure testFigure = figureFactory.CreateFigure(sides); Assert.AreEqual(testFigure.ToString(), checkName); }); }
/// <summary> /// Method Read /// </summary> /// <returns>New List</returns> public List <IFigure> Read(string input) { List <IFigure> boxxml = new List <IFigure>(); XmlReader xreader = XmlReader.Create(input); Material material; Colors color = 0; FigureFactory factory = new FigureFactory(); while (xreader.Read()) { if (xreader.Name == "figure") { switch (xreader.GetAttribute("type")) { case "Circle": { float d; xreader.Read(); xreader.Read(); if (xreader.Name == "material") { material = (Material)Enum.Parse(typeof(Material), xreader.ReadInnerXml()); xreader.Read(); } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "color") { if (material == Material.Paper) { color = (Colors)Enum.Parse(typeof(Colors), xreader.ReadInnerXml()); xreader.Read(); } else { xreader.ReadInnerXml(); xreader.Read(); } } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "diameter") { d = float.Parse(xreader.ReadInnerXml()); } else { throw new Exception("Wrong input xml"); } IFigure figure = factory.CreateFigure(material, Form.Circle, new float[] { d }); if (material == Material.Paper && color != Colors.white) { ((IPaper)figure).Color = color; } boxxml.Add(figure); break; } case "Square": { float a; xreader.Read(); xreader.Read(); if (xreader.Name == "material") { material = (Material)Enum.Parse(typeof(Material), xreader.ReadInnerXml()); xreader.Read(); } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "color") { if (material == Material.Paper) { color = (Colors)Enum.Parse(typeof(Colors), xreader.ReadInnerXml()); xreader.Read(); } else { xreader.ReadInnerXml(); xreader.Read(); } } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "height") { a = float.Parse(xreader.ReadInnerXml()); } else { throw new Exception("Wrong input xml"); } IFigure figure = factory.CreateFigure(material, Form.Square, new float[] { a }); if (material == Material.Paper && color != Colors.white) { ((IPaper)figure).Color = color; } boxxml.Add(figure); break; } case "Rectangle": { float h, w; xreader.Read(); xreader.Read(); if (xreader.Name == "material") { material = (Material)Enum.Parse(typeof(Material), xreader.ReadInnerXml()); xreader.Read(); } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "color") { if (material == Material.Paper) { color = (Colors)Enum.Parse(typeof(Colors), xreader.ReadInnerXml()); xreader.Read(); } else { xreader.ReadInnerXml(); xreader.Read(); } } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "height") { h = float.Parse(xreader.ReadInnerXml()); xreader.Read(); } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "width") { w = float.Parse(xreader.ReadInnerXml()); } else { throw new Exception("Wrong input xml"); } IFigure figure = factory.CreateFigure(material, Form.Rectangle, new float[] { h, w }); if (material == Material.Paper && color != Colors.white) { ((IPaper)figure).Color = color; } boxxml.Add(figure); break; } case "Triangle": { float a, b, c; xreader.Read(); xreader.Read(); if (xreader.Name == "material") { material = (Material)Enum.Parse(typeof(Material), xreader.ReadInnerXml()); xreader.Read(); } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "color") { if (material == Material.Paper) { color = (Colors)Enum.Parse(typeof(Colors), xreader.ReadInnerXml()); xreader.Read(); } else { xreader.ReadInnerXml(); xreader.Read(); } } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "side_a") { a = float.Parse(xreader.ReadInnerXml()); xreader.Read(); } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "side_b") { b = float.Parse(xreader.ReadInnerXml()); xreader.Read(); } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "side_c") { c = float.Parse(xreader.ReadInnerXml()); } else { throw new Exception("Wrong input xml"); } IFigure figure = factory.CreateFigure(material, Form.Triangle, new float[] { a, b, c }); if (material == Material.Paper && color != Colors.white) { ((IPaper)figure).Color = color; } boxxml.Add(figure); break; } } } } xreader.Close(); return(boxxml); }
/// <summary> /// StreamReader /// </summary> /// <returns>List of figures from XML</returns> public List <IFigure> Read(string input) { FigureFactory factory = new FigureFactory(); List <IFigure> boxxml = new List <IFigure>(); StreamReader sr = new StreamReader(input); string doc = sr.ReadToEnd(); XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(doc); XmlElement xRoot = xdoc.DocumentElement; foreach (XmlNode xnode in xRoot) { XmlNode typefigure = xnode.Attributes.GetNamedItem("type"); switch (typefigure.Value) { case "Circle": { Material material = 0; Colors color = 0; float diameter = 1; foreach (XmlNode childnode in xnode.ChildNodes) { if (childnode.Name == "material") { material = (Material)Enum.Parse(typeof(Material), $"{childnode.InnerText}"); } if (material == Material.Paper) { if (childnode.Name == "color") { color = (Colors)Enum.Parse(typeof(Colors), $"{childnode.InnerText}"); } } if (childnode.Name == "diameter") { diameter = float.Parse(childnode.InnerText); } } IFigure figure = factory.CreateFigure(material, Form.Circle, diameter); if (material == Material.Paper && color != Colors.white) { ((IPaper)figure).Color = color; } boxxml.Add(figure); break; } case "Rectangle": { Material material = 0; Colors color = 0; float height = 1; float width = 1; foreach (XmlNode childnode in xnode.ChildNodes) { if (childnode.Name == "material") { material = (Material)Enum.Parse(typeof(Material), $"{childnode.InnerText}"); } if (material == Material.Paper) { if (childnode.Name == "color") { color = (Colors)Enum.Parse(typeof(Colors), $"{childnode.InnerText}"); } } if (childnode.Name == "height") { height = float.Parse(childnode.InnerText); } if (childnode.Name == "width") { width = float.Parse(childnode.InnerText); } } IFigure figure = factory.CreateFigure(material, Form.Rectangle, new float [] { height, width }); if (material == Material.Paper && color != Colors.white) { ((IPaper)figure).Color = color; } boxxml.Add(figure); break; } case "Square": { Material material = 0; Colors color = 0; float height = 1; foreach (XmlNode childnode in xnode.ChildNodes) { if (childnode.Name == "material") { material = (Material)Enum.Parse(typeof(Material), $"{childnode.InnerText}"); } if (material == Material.Paper) { if (childnode.Name == "color") { color = (Colors)Enum.Parse(typeof(Colors), $"{childnode.InnerText}"); } } if (childnode.Name == "height") { height = float.Parse(childnode.InnerText); } } IFigure figure = factory.CreateFigure(material, Form.Square, new float[] { height }); if (material == Material.Paper && color != Colors.white) { ((IPaper)figure).Color = color; } boxxml.Add(figure); break; } case "Triangle": { Material material = 0; Colors color = 0; float a = 1; float b = 1; float c = 1; foreach (XmlNode childnode in xnode.ChildNodes) { if (childnode.Name == "material") { material = (Material)Enum.Parse(typeof(Material), $"{childnode.InnerText}"); } if (material == Material.Paper) { if (childnode.Name == "color") { color = (Colors)Enum.Parse(typeof(Colors), $"{childnode.InnerText}"); } } if (childnode.Name == "side_a") { a = float.Parse(childnode.InnerText); } if (childnode.Name == "side_b") { b = float.Parse(childnode.InnerText); } if (childnode.Name == "side_c") { c = float.Parse(childnode.InnerText); } } IFigure figure = factory.CreateFigure(material, Form.Square, new float[] { a, b, c }); if (material == Material.Paper && color != Colors.white) { ((IPaper)figure).Color = color; } boxxml.Add(figure); break; } } } sr.Close(); return(boxxml); }
public void WriteTest() { FigureFactory factory = new FigureFactory(); BoxofFigures figures = new BoxofFigures(); float[] vs = new float[] { 3f, 2f }; float[] vs1 = new float[] { 31f }; float[] vs2 = new float[] { 3f, 2.1f, 5f }; float[] vs3 = new float[] { 11f }; float[] vs4 = new float[] { 17f, 0.9f }; float[] vs5 = new float[] { 9f, 7f }; float[] vs6 = new float[] { 7f }; float[] vs7 = new float[] { 7.3f, 19.2f }; float[] vs8 = new float[] { 5f, 2f, 6.2f }; float[] vs9 = new float[] { 3.7f, 2.51f }; float[] vs10 = new float[] { 11f, 22f }; figures.AddFigure(factory.CreateFigure(Material.Film, Form.Rectangle, vs)); figures.AddFigure(factory.CreateFigure(Material.Paper, Form.Circle, vs1)); figures.AddFigure(factory.CreateFigure(Material.Film, Form.Triangle, vs2)); figures.AddFigure(factory.CreateFigure(Material.Film, Form.Square, vs3)); figures.AddFigure(factory.CreateFigure(Material.Paper, Form.Rectangle, vs4)); figures.AddFigure(factory.CreateFigure(Material.Paper, Form.Rectangle, vs5)); figures.AddFigure(factory.CreateFigure(Material.Paper, Form.Square, vs6)); figures.AddFigure(factory.CreateFigure(Material.Paper, Form.Rectangle, vs7)); figures.AddFigure(factory.CreateFigure(Material.Film, Form.Triangle, vs8)); figures.AddFigure(factory.CreateFigure(Material.Paper, Form.Rectangle, vs9)); figures.AddFigure(factory.CreateFigure(Material.Film, Form.Rectangle, vs10)); figures.SaveXMLAll("output_1_all.xml"); figures.SaveXMLFilm("output_1_film.xml"); figures.SaveXMLPaper("output_1_paper.xml"); figures.SaveXML2All("output_2_all.xml"); figures.SaveXML2Film("output_2_film.xml"); figures.SaveXML2Paper("output_2_paper.xml"); figures.LoadXML("input1.xml"); figures.LoadXML2("input2.xml"); figures.SaveXMLAll("output_i1_all.xml"); figures.SaveXML2All("output_i2_all.xml"); Assert.IsNotNull(figures); }