我有问题。我创建了以下代码来检查我的三角形形成有多少个共享边:
public class TriangleRegistryList
{
public float x1 { get; set; }
public float y1 { get; set; }
public float x2 { get; set; }
public float y2 { get; set; }
public float x3 { get; set; }
public float y3 { get; set; }
public int ShapeNum { get; set; }
public TriangleRegistryList()
{
this.AdjacentShapeNumbers = new List<int>();
}
public List<int> AdjacentShapeNumbers { get; set; }
public IEnumerable<(float x, float y)> GetPoints()
{
yield return (x1, y1);
yield return (x2, y2);
yield return (x3, y3);
}
public bool IsAdjacentTo(TriangleRegistryList other)
{
var isAdjacentTo =
GetPoints().Intersect(other.GetPoints()).Count() >= 2;
if (isAdjacentTo)
{
if(other.ShapeNum != 0)
{
AdjacentShapeNumbers.Add(other.ShapeNum);
}
}
return isAdjacentTo;
}
}
public static class EnumerableExtensions
{
public static IEnumerable<(T first, T second)> GetPairs<T>(this IEnumerable<T> list)
{
return list.SelectMany((value, index) => list.Skip(index + 1),
(first, second) => (first, second));
}
}
然后我创建如下列表:
triangles = new List<TriangleRegistryList>();
triangles.Add(new TriangleRegistryList
{
x1 = (float)405,
y1 = (float)701.4806,
x2 = (float)675,
y2 = (float)701.4806,
x3 = (float)540,
y3 = (float)935.3074
});
triangles.Add(new TriangleRegistryList
{
x1 = (float)135,
y1 = (float)701.4806,
x2 = (float)405,
y2 = (float)701.4806,
x3 = (float)270,
y3 = (float)935.3074
});
triangles.Add(new TriangleRegistryList
{
x1 = (float)270,
y1 = (float)935.3074,
x2 = (float)540,
y2 = (float)935.3074,
x3 = (float)405,
y3 = (float)701.4806
});
最后,我调用该方法将所有三角形边相互比较。
int sharedEdges = triangles.GetPairs().Where(t => t.first.IsAdjacentTo(t.second)).Count();
这很好用,但是现在我想将1个三角形与列表进行比较,因此我可以检查1个三角形与地层有多少个共享边。我已经创建了这个循环,但是我不知道如何继续:
foreach (TriangleRegistryList triangle in triangles)
{
int sharedEdges = triangles.GetPairs().Where(t => t.first.IsAdjacentTo(t.second)).Count();
}
如何仅将1个三角形与列表进行比较,以查看1个三角形与地层有多少个共享边?
答案 0 :(得分:0)
我将Triangle
的概念完全分开。尝试管理三角点属性并在同一类上维护相邻的分类帐,会使代码规模复杂化。
这是一个非常粗略的设计,说明如何开始朝这个方向前进:
public class Triangle
{
public PointF Point1 { get; }
public PointF Point2 { get; }
public PointF Point3 { get; }
public IEnumerable<PointF> Points => new List<PointF> { Point1, Point2, Point3 };
public Triangle(PointF point1, PointF point2, PointF point3)
{
this.Point1 = point1;
this.Point2 = point2;
this.Point3 = point3;
}
public Triangle(float x1, float y1, float x2, float y2, float x3, float y3)
: this(new PointF(x1, y1), new PointF(x2, y2), new PointF(x3, y3)) { }
public bool IsAdjacentTo(Triangle other) => this.Points.Intersect(other.Points).Count() > 1;
}
public class TriangleRegistryList
{
public IList<Triangle> Triangles { get; }
public Dictionary<Triangle, List<Triangle>> AdjacentMap { get; }
public TriangleRegistryList(IEnumerable<Triangle> triangles)
{
this.Triangles = new List<Triangle>(triangles);
this.AdjacentMap = GetAdjacentMap();
}
private Dictionary<Triangle, List<Triangle>> GetAdjacentMap()
{
return Triangles.ToDictionary(t => t, a => Triangles.Where(b => b.IsAdjacentTo(a)).ToList());
}
}
这没有经过任何测试,但是您看到了各个类的分隔。