gpt4 book ai didi

c# - 计算连接线

转载 作者:行者123 更新时间:2023-11-30 22:54:32 25 4
gpt4 key购买 nike

我有一个问题。我使用 SkiaSharp 创建了一个 TriangleGrid。在绘制网格时,我将每个三角形信息保存在字典中。字典看起来像这样:

public class TriangleRegistryObject
{
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 bool Selected { get; set; }
public bool Visible { get; set; }
}

现在,当我选择一个三角形时,我将 bool 值 Selected 设置为 true。最后我想检查我选择的三角形是否相互连接。我以为我可以数出连接的线路。这是一个示例图像:enter image description here

现在我想计算 Selected=true 处的紫色线。我有每个坐标 (x1, y1) (x2, y2) 和 (x3, y3)。

更新:这是我使用的为我返回 0 的代码!

public static bool ValidLayout()
{
bool IsValid;
int sharedEdges;
int SelectedTriangles = TriangleRegistry.Count(tr => tr.Value.Selected.Equals(true));
var triangles = new List<TriangleRegistryList>();

foreach (KeyValuePair<string, TriangleRegistryObject> row in TriangleRegistry.Where(n => n.Value.Selected == true).ToList())
{
triangles.Add(new TriangleRegistryList { x1 = row.Value.x1,
y1 = row.Value.y1,
x2 = row.Value.x2,
y2 = row.Value.y2,
x3 = row.Value.x3,
y3 = row.Value.y3
});
}

sharedEdges = triangles.GetKCombs(2).Where(t => t.First().IsAdjacentTo(t.Skip(1).Take(1).Single())).Count();

if (sharedEdges >= (SelectedTriangles - 1))
{
IsValid = true;
}
else
{
IsValid = false;
}

return IsValid;
}

但我不知道如何相互比较坐标,计算连接线!

有人可以帮助我吗?

最佳答案

这是一个非常简单的解决方案。它绝对不是最有效的,但可以完成工作。

我已经向您的三角形类添加了一个方法,如果它与另一个三角形共享至少 2 个顶点,则该方法返回 true。

我还使用了一种查找不同排列的方法,该方法与讨论的方法稍作修改 here .

public class Program
{
public static void Main()
{
var triangles = new List<TriangleRegistryObject>{
new TriangleRegistryObject{x1=10,y1=10, x2=12,y2=10, x3=1,y3=11},
new TriangleRegistryObject{x1=9,y1=11, x2=11,y2=11, x3=10,y3=10},
new TriangleRegistryObject{x1=9,y1=11, x2=11,y2=11, x3=10,y3=12},
new TriangleRegistryObject{x1=34,y1=14, x2=15,y2=11, x3=10,y3=12},
};

var sharedEdges = triangles.GetPairs().Where(t => t.first.IsAdjacentTo(t.second)).Count();
Console.WriteLine($"Number shared edges: {sharedEdges}");
}
}

public class TriangleRegistryObject
{
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 bool Selected { get; set; }
public bool Visible { get; set; }

public IEnumerable<(float x, float y)> GetPoints()
{
yield return (x1, y1);
yield return (x2, y2);
yield return (x3, y3);
}

public bool IsAdjacentTo(TriangleRegistryObject other)
{
return this.GetPoints().Intersect(other.GetPoints()).Count() >= 2;
}
}

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));
}
}

关于c# - 计算连接线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56189312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com