Add polygon shape
This commit is contained in:
23
StructureHelperCommon/Models/Shapes/IPolygonShape.cs
Normal file
23
StructureHelperCommon/Models/Shapes/IPolygonShape.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public interface IPolygonShape : IShape
|
||||
{
|
||||
IReadOnlyList<IVertex> Vertices { get; }
|
||||
bool IsClosed { get; set; }
|
||||
|
||||
IVertex AddVertex(IVertex vertex);
|
||||
IVertex InsertVertex(int index, IVertex vertex);
|
||||
IVertex AddVertexBefore(IVertex existing, IVertex vertex);
|
||||
IVertex AddVertexAfter(IVertex existing, IVertex vertex);
|
||||
|
||||
void RemoveVertex(IVertex vertex);
|
||||
void Clear();
|
||||
}
|
||||
|
||||
}
|
||||
15
StructureHelperCommon/Models/Shapes/IVertex.cs
Normal file
15
StructureHelperCommon/Models/Shapes/IVertex.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public interface IVertex : ISaveable
|
||||
{
|
||||
IPoint2D Point { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
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 interface IPolygonCalculator
|
||||
{
|
||||
double GetPerimeter(IPolygonShape polygon);
|
||||
double GetArea(IPolygonShape polygon);
|
||||
bool ContainsPoint(IPolygonShape polygon, IPoint2D point);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public class PolygonCalculator : IPolygonCalculator
|
||||
{
|
||||
public double GetPerimeter(IPolygonShape polygon)
|
||||
{
|
||||
if (polygon.Vertices.Count < 2)
|
||||
return 0;
|
||||
|
||||
double perimeter = 0;
|
||||
|
||||
for (int i = 0; i < polygon.Vertices.Count - 1; i++)
|
||||
{
|
||||
perimeter += Distance(polygon.Vertices[i].Point, polygon.Vertices[i + 1].Point);
|
||||
}
|
||||
|
||||
if (polygon.IsClosed && polygon.Vertices.Count > 2)
|
||||
{
|
||||
perimeter += Distance(polygon.Vertices[^1].Point, polygon.Vertices[0].Point);
|
||||
}
|
||||
|
||||
return perimeter;
|
||||
}
|
||||
|
||||
public double GetArea(IPolygonShape polygon)
|
||||
{
|
||||
if (!polygon.IsClosed || polygon.Vertices.Count < 3)
|
||||
return 0;
|
||||
|
||||
// Shoelace formula
|
||||
double sum = 0;
|
||||
for (int i = 0; i < polygon.Vertices.Count; i++)
|
||||
{
|
||||
var p1 = polygon.Vertices[i].Point;
|
||||
var p2 = polygon.Vertices[(i + 1) % polygon.Vertices.Count].Point;
|
||||
sum += (p1.X * p2.Y - p2.X * p1.Y);
|
||||
}
|
||||
|
||||
return Math.Abs(sum) / 2.0;
|
||||
}
|
||||
|
||||
public bool ContainsPoint(IPolygonShape polygon, IPoint2D point)
|
||||
{
|
||||
if (!polygon.IsClosed || polygon.Vertices.Count < 3)
|
||||
return false;
|
||||
|
||||
// Ray casting algorithm
|
||||
bool inside = false;
|
||||
int j = polygon.Vertices.Count - 1;
|
||||
|
||||
for (int i = 0; i < polygon.Vertices.Count; i++)
|
||||
{
|
||||
var pi = polygon.Vertices[i].Point;
|
||||
var pj = polygon.Vertices[j].Point;
|
||||
|
||||
if (((pi.Y > point.Y) != (pj.Y > point.Y)) &&
|
||||
(point.X < (pj.X - pi.X) * (point.Y - pi.Y) / (pj.Y - pi.Y) + pi.X))
|
||||
{
|
||||
inside = !inside;
|
||||
}
|
||||
j = i;
|
||||
}
|
||||
|
||||
return inside;
|
||||
}
|
||||
|
||||
private double Distance(IPoint2D p1, IPoint2D p2)
|
||||
{
|
||||
double dx = p2.X - p1.X;
|
||||
double dy = p2.Y - p1.Y;
|
||||
return Math.Sqrt(dx * dx + dy * dy);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
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 static class PolygonGeometryUtils
|
||||
{
|
||||
public static bool DoPolygonsEdgesIntersect(IPolygonShape polygon)
|
||||
{
|
||||
var vertices = polygon.Vertices;
|
||||
int n = vertices.Count;
|
||||
|
||||
if (n < 4)
|
||||
return false; // Triangles cannot self-intersect
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
var a1 = vertices[i].Point;
|
||||
var a2 = vertices[(i + 1) % n].Point;
|
||||
|
||||
for (int j = i + 1; j < n; j++)
|
||||
{
|
||||
var b1 = vertices[j].Point;
|
||||
var b2 = vertices[(j + 1) % n].Point;
|
||||
|
||||
// Skip adjacent edges (they share a vertex)
|
||||
if (AreAdjacent(i, j, n, polygon.IsClosed))
|
||||
continue;
|
||||
|
||||
if (SegmentsIntersect(a1, a2, b1, b2))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool AreAdjacent(int i, int j, int n, bool isClosed)
|
||||
{
|
||||
// Edges (i,i+1) and (j,j+1) are adjacent if:
|
||||
if (j == i) return true;
|
||||
if (j == i + 1) return true;
|
||||
if (i == j + 1) return true;
|
||||
|
||||
// Special case: first and last edges in closed polygon
|
||||
if (isClosed &&
|
||||
((i == 0 && j == n - 1) || (j == 0 && i == n - 1)))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool SegmentsIntersect(IPoint2D p1, IPoint2D p2, IPoint2D q1, IPoint2D q2)
|
||||
{
|
||||
return (Orientation(p1, p2, q1) * Orientation(p1, p2, q2) < 0) &&
|
||||
(Orientation(q1, q2, p1) * Orientation(q1, q2, p2) < 0);
|
||||
}
|
||||
|
||||
private static int Orientation(IPoint2D a, IPoint2D b, IPoint2D c)
|
||||
{
|
||||
double val = (b.Y - a.Y) * (c.X - b.X) -
|
||||
(b.X - a.X) * (c.Y - b.Y);
|
||||
|
||||
if (Math.Abs(val) < 1e-12) return 0; // Collinear
|
||||
return (val > 0) ? 1 : -1; // Clockwise / Counter-clockwise
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
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 PolygonUpdateStrategy : 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
71
StructureHelperCommon/Models/Shapes/PolygonShape.cs
Normal file
71
StructureHelperCommon/Models/Shapes/PolygonShape.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public class PolygonShape : IPolygonShape
|
||||
{
|
||||
private readonly List<IVertex> _vertices = new();
|
||||
|
||||
public Guid Id { get; }
|
||||
public IReadOnlyList<IVertex> Vertices => _vertices;
|
||||
public bool IsClosed { get; set; }
|
||||
|
||||
public PolygonShape(Guid id)
|
||||
{
|
||||
Id = id;
|
||||
}
|
||||
|
||||
public IVertex AddVertex(IVertex vertex)
|
||||
{
|
||||
_vertices.Add(vertex);
|
||||
return vertex;
|
||||
}
|
||||
|
||||
public IVertex InsertVertex(int index, IVertex vertex)
|
||||
{
|
||||
_vertices.Insert(index, vertex);
|
||||
return vertex;
|
||||
}
|
||||
|
||||
public IVertex AddVertexBefore(IVertex existing, IVertex vertex)
|
||||
{
|
||||
int index = CheckVertexExists(existing);
|
||||
_vertices.Insert(index, vertex);
|
||||
return vertex;
|
||||
}
|
||||
public IVertex AddVertexAfter(IVertex existing, IVertex vertex)
|
||||
{
|
||||
int index = CheckVertexExists(existing);
|
||||
_vertices.Insert(index + 1, vertex);
|
||||
return vertex;
|
||||
}
|
||||
|
||||
public void RemoveVertex(IVertex vertex)
|
||||
{
|
||||
if (!_vertices.Remove(vertex))
|
||||
throw new InvalidOperationException("The specified vertex was not found in the polygon.");
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_vertices.Clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private int CheckVertexExists(IVertex existing)
|
||||
{
|
||||
int index = _vertices.IndexOf(existing);
|
||||
if (index == -1)
|
||||
{
|
||||
throw new StructureHelperException("The specified vertex was not found in the polygon.");
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
19
StructureHelperCommon/Models/Shapes/Vertex.cs
Normal file
19
StructureHelperCommon/Models/Shapes/Vertex.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using System;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public class Vertex : IVertex
|
||||
{
|
||||
public Guid Id { get; }
|
||||
|
||||
public Vertex(Guid id)
|
||||
{
|
||||
Id = id;
|
||||
}
|
||||
|
||||
public IPoint2D Point { get; set; }
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user