Add polygon to DTO convert strategy
This commit is contained in:
@@ -9,9 +9,9 @@ namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public interface IPolygonCalculator
|
||||
{
|
||||
double GetPerimeter(IPolygonShape polygon);
|
||||
double GetArea(IPolygonShape polygon);
|
||||
bool ContainsPoint(IPolygonShape polygon, IPoint2D point);
|
||||
double GetPerimeter(ILinePolygonShape polygon);
|
||||
double GetArea(ILinePolygonShape polygon);
|
||||
bool ContainsPoint(ILinePolygonShape polygon, IPoint2D point);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using StructureHelperCommon.Models.Shapes.Logics;
|
||||
using StructureHelperCommon.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public class LinePolygonShapeUpdateStrategy : IParentUpdateStrategy<ILinePolygonShape>
|
||||
{
|
||||
public bool UpdateChildren { get; set; } = true;
|
||||
|
||||
public void Update(ILinePolygonShape targetObject, ILinePolygonShape sourceObject)
|
||||
{
|
||||
CheckObject.IsNull(targetObject, sourceObject);
|
||||
if (ReferenceEquals(targetObject, sourceObject)) { return; }
|
||||
|
||||
// Update simple properties
|
||||
targetObject.IsClosed = sourceObject.IsClosed;
|
||||
if (UpdateChildren == true)
|
||||
{
|
||||
// Replace vertices
|
||||
targetObject.Clear();
|
||||
foreach (var vertex in sourceObject.Vertices)
|
||||
{
|
||||
// Copy the underlying point into a new vertex
|
||||
Vertex newVertex = GetVertexClone(vertex);
|
||||
targetObject.AddVertex(newVertex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Vertex GetVertexClone(IVertex vertex)
|
||||
{
|
||||
var updateStrategy = new VertexUpdateStrategy();
|
||||
Vertex newVertex = new(Guid.NewGuid());
|
||||
updateStrategy.Update(newVertex, vertex);
|
||||
return newVertex;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,7 +4,7 @@ namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public class PolygonCalculator : IPolygonCalculator
|
||||
{
|
||||
public double GetPerimeter(IPolygonShape polygon)
|
||||
public double GetPerimeter(ILinePolygonShape polygon)
|
||||
{
|
||||
if (polygon.Vertices.Count < 2)
|
||||
return 0;
|
||||
@@ -24,7 +24,7 @@ namespace StructureHelperCommon.Models.Shapes
|
||||
return perimeter;
|
||||
}
|
||||
|
||||
public double GetArea(IPolygonShape polygon)
|
||||
public double GetArea(ILinePolygonShape polygon)
|
||||
{
|
||||
if (!polygon.IsClosed || polygon.Vertices.Count < 3)
|
||||
return 0;
|
||||
@@ -41,7 +41,7 @@ namespace StructureHelperCommon.Models.Shapes
|
||||
return Math.Abs(sum) / 2.0;
|
||||
}
|
||||
|
||||
public bool ContainsPoint(IPolygonShape polygon, IPoint2D point)
|
||||
public bool ContainsPoint(ILinePolygonShape polygon, IPoint2D point)
|
||||
{
|
||||
if (!polygon.IsClosed || polygon.Vertices.Count < 3)
|
||||
return false;
|
||||
|
||||
@@ -9,10 +9,10 @@ namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public static class PolygonGeometryUtils
|
||||
{
|
||||
public static IPolygonShape GetTratsfromedPolygon(IPolygonShape polygon, double dx, double dy)
|
||||
public static ILinePolygonShape GetTratsfromedPolygon(ILinePolygonShape polygon, double dx, double dy)
|
||||
{
|
||||
IPolygonShape newPolygon = new PolygonShape(Guid.Empty);
|
||||
var updateLogic = new PolygonShapeUpdateStrategy();
|
||||
ILinePolygonShape newPolygon = new LinePolygonShape(Guid.Empty);
|
||||
var updateLogic = new LinePolygonShapeUpdateStrategy();
|
||||
updateLogic.Update(newPolygon, polygon);
|
||||
foreach (var item in newPolygon.Vertices)
|
||||
{
|
||||
@@ -21,7 +21,7 @@ namespace StructureHelperCommon.Models.Shapes
|
||||
}
|
||||
return newPolygon;
|
||||
}
|
||||
public static bool DoPolygonsEdgesIntersect(IPolygonShape polygon)
|
||||
public static bool DoPolygonsEdgesIntersect(ILinePolygonShape polygon)
|
||||
{
|
||||
var vertices = polygon.Vertices;
|
||||
int n = vertices.Count;
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using StructureHelperCommon.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public class PolygonShapeUpdateStrategy : IUpdateStrategy<IPolygonShape>
|
||||
{
|
||||
public void Update(IPolygonShape targetObject, IPolygonShape sourceObject)
|
||||
{
|
||||
CheckObject.IsNull(sourceObject);
|
||||
CheckObject.IsNull(targetObject);
|
||||
if (ReferenceEquals(targetObject, sourceObject)) { return; }
|
||||
|
||||
// Update simple properties
|
||||
targetObject.IsClosed = sourceObject.IsClosed;
|
||||
|
||||
// Replace vertices
|
||||
targetObject.Clear();
|
||||
foreach (var vertex in sourceObject.Vertices)
|
||||
{
|
||||
// Copy the underlying point into a new vertex
|
||||
Vertex newVertex = GetVertexClone(vertex);
|
||||
targetObject.AddVertex(newVertex);
|
||||
}
|
||||
}
|
||||
|
||||
private static Vertex GetVertexClone(IVertex vertex)
|
||||
{
|
||||
Point2D newVertexPoint = new(Guid.NewGuid())
|
||||
{
|
||||
X = vertex.Point.X,
|
||||
Y = vertex.Point.Y
|
||||
};
|
||||
Vertex newVertex = new(Guid.NewGuid())
|
||||
{
|
||||
Point = newVertexPoint
|
||||
};
|
||||
return newVertex;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,7 +24,7 @@ namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
ProcessCircles(targetObject, sourceCircle);
|
||||
}
|
||||
else if (sourceObject is IPolygonShape sourcePolygon)
|
||||
else if (sourceObject is ILinePolygonShape sourcePolygon)
|
||||
{
|
||||
ProcessPolygon(targetObject, sourcePolygon);
|
||||
}
|
||||
@@ -34,11 +34,11 @@ namespace StructureHelperCommon.Models.Shapes
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessPolygon(IShape targetObject, IPolygonShape sourcePolygon)
|
||||
private void ProcessPolygon(IShape targetObject, ILinePolygonShape sourcePolygon)
|
||||
{
|
||||
if (targetObject is IPolygonShape targetPolygon)
|
||||
if (targetObject is ILinePolygonShape targetPolygon)
|
||||
{
|
||||
var updateLogic = new PolygonShapeUpdateStrategy();
|
||||
var updateLogic = new LinePolygonShapeUpdateStrategy();
|
||||
updateLogic.Update(targetPolygon, sourcePolygon);
|
||||
}
|
||||
else
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using StructureHelperCommon.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public class VertexUpdateStrategy : IParentUpdateStrategy<IVertex>
|
||||
{
|
||||
public bool UpdateChildren { get; set; } = true;
|
||||
|
||||
public void Update(IVertex targetObject, IVertex sourceObject)
|
||||
{
|
||||
CheckObject.IsNull(targetObject, sourceObject);
|
||||
if (ReferenceEquals(targetObject, sourceObject)) { return; }
|
||||
if (UpdateChildren == true)
|
||||
{
|
||||
var newPoint = sourceObject.Point.Clone() as IPoint2D;
|
||||
targetObject.Point = newPoint;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user