c# - 计算连接线数

标签 c#

我有一个问题。我使用 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/

相关文章:

c# - 将 Asp.Net WebApi 5.2.2 更新到 5.2.7 后捕获所有不工作的路由

c# - 如何从 Roslyn 脚本文件加载引用的脚本库?

c# - 如何将文本格式传递给 .cs 文件?

c# - 将依赖注入(inject)添加到 MVVM 应用程序

c# - Linq join() - 加入两个实体并选择一个

c# - 无法捕获 sleep /暂停消息 (winXP)

c# - C# Unity 中的正则表达式

c# - 如何将 List<T> 转换为 DataSet?

c# - 如何处理空套接字缓冲区

c# - Azure 函数有时不会将行插入 SQL Server