将 Struct 中定义的列表转换为 Int[ , ]

Converting a List defined in a Struct into Int[ , ]

我对以特定方式定义的列表有点费劲。是这样的情况: 我有一个名为 Edge 的结构,它由代码(如下)中的结构点中定义的两个点组成。

我的输入列表名为 "EDGE"。使用该列表我想产生两个输出:

列表 POINTS :包含出现在列表 "EDGE" 中的非重复点。 Int[] edgeIndices:表示与输入列表 "EDGE" 相同的东西,但我想要的不是点,而是之前创建的列表 "POINTS" 中定义的此类点的索引。 你能帮我解决这个问题吗?

请注意,使用 List 和 Int[] 而不是其他类型非常重要。

非常感谢!

using System;
using System.Collections.Generic;


namespace Test
{

struct Point
{
    public readonly double X;
    public readonly double Y;

    public Point(double x, double y)
    {
        X = x;
        Y = y;
    }
}

struct Edge
{
    public Point First;
    public Point Second;

    public Edge(Point First, Point Second)
    {
        this.First = First;
        this.Second = Second;
    }
}

class Program
{

    static void Main(string[] args)
    {


        ///////////////////////////INPUT////////////////////////////////////
        var EDGES = new List<Edge>();
        EDGES.Add(new Edge(new Point(5, 50), new Point(20, 100))); //EDGE 01
        EDGES.Add(new Edge(new Point(20, 100), new Point(30, 50))); //EDGE 12
        EDGES.Add(new Edge(new Point(30, 50), new Point(10, 0))); //EDGE 23
        EDGES.Add(new Edge(new Point(5, 50), new Point(30, 50))); //EDGE 02
        EDGES.Add(new Edge(new Point(5, 50), new Point(10, 0))); //EDGE 03
        EDGES.Add(new Edge(new Point(20, 100), new Point(80, 100))); //EDGE 14
        EDGES.Add(new Edge(new Point(10, 0), new Point(80, 100))); //EDGE 34

        ///////////////////////EXPECTED OUTPUTS/////////////////////////////
        ///
        ///POINTS (EXPECTED) as List<double[]>
        ///Index    X   Y
        ///  0      5   50
        ///  1      20  100
        ///  2      30  50
        ///  3      10  0
        ///  4      80  100
        ///
        ///MULTIARRAY (EXPECTED)
        ///static int[,] edgeIndices =
        ///    {
        ///        {0, 1}, {1, 2}, {2, 3}, {0, 2},
        ///        {0, 3}, {1, 4}, {3, 4}
        ///    };


    }

}
}

我建议您在 List() 中存储不同的点值。您可以使用以下代码来实现它:

using System;
using System.Collections.Generic;

namespace Test
{
struct Point
{
    public readonly double X;
    public readonly double Y;

    public Point(double x, double y)
    {
        X = x;
        Y = y;
    }
}

struct Edge
{
    public Point First;
    public Point Second;

    public Edge(Point First, Point Second)
    {
        this.First = First;
        this.Second = Second;
    }
}

class Program
{

    static void Main(string[] args)
    {
        ///////////////////////////INPUT////////////////////////////////////
        var EDGES = new List<Edge>();
        EDGES.Add(new Edge(new Point(5, 50), new Point(20, 100))); //EDGE 01
        EDGES.Add(new Edge(new Point(20, 100), new Point(30, 50))); //EDGE 12
        EDGES.Add(new Edge(new Point(30, 50), new Point(10, 0))); //EDGE 23
        EDGES.Add(new Edge(new Point(5, 50), new Point(30, 50))); //EDGE 02
        EDGES.Add(new Edge(new Point(5, 50), new Point(10, 0))); //EDGE 03
        EDGES.Add(new Edge(new Point(20, 100), new Point(80, 100))); //EDGE 14
        EDGES.Add(new Edge(new Point(10, 0), new Point(80, 100))); //EDGE 34

        //FLL POINTS CACHE
        var distinctPoints = new List<Point>();
        foreach (Edge edge in EDGES)
        {
            if (!distinctPoints.Contains(edge.First))
                distinctPoints.Add(edge.First);
            if (!distinctPoints.Contains(edge.Second))
                distinctPoints.Add(edge.Second);
        }

        //POINTS LIST OUTPUT
        for (int i = 0; i < distinctPoints.Count; i++)
        {
            Console.WriteLine("{0} {1} {2}", i, distinctPoints[i].X, distinctPoints[i].Y);
        }

        //FILL 2D ARRAY OF INDICES
        int[,] edgeIndices = new int[EDGES.Count, 2];
        for (int i = 0; i < EDGES.Count; i++)
        {
            edgeIndices[i, 0] = distinctPoints.IndexOf(EDGES[i].First);
            edgeIndices[i, 1] = distinctPoints.IndexOf(EDGES[i].Second);
        }

        //2D ARRAY OUTPUT
        for (int i = 0; i < edgeIndices.GetLength(0); i++)
        {
            Console.WriteLine("({0}, {1})", edgeIndices[i, 0], edgeIndices[i, 1]);                
        }

        Console.ReadKey();
    }

}
}

这是我的输出:

这是我的实现:

using System;
using System.Collections.Generic;


namespace Test{

struct Point
{
    public readonly double X;
    public readonly double Y;

    public Point(double x, double y)
    {
        X = x;
        Y = y;
    }
}

struct Edge
{
    public Point First;
    public Point Second;

    public Edge(Point First, Point Second)
    {
        this.First = First;
        this.Second = Second;
    }
}

class Program
{

    static void Main(string[] args)
    {


        ///////////////////////////INPUT////////////////////////////////////
        var EDGES = new List<Edge>();
        EDGES.Add(new Edge(new Point(5, 50), new Point(20, 100))); //EDGE 01
        EDGES.Add(new Edge(new Point(20, 100), new Point(30, 50))); //EDGE 12
        EDGES.Add(new Edge(new Point(30, 50), new Point(10, 0))); //EDGE 23
        EDGES.Add(new Edge(new Point(5, 50), new Point(30, 50))); //EDGE 02
        EDGES.Add(new Edge(new Point(5, 50), new Point(10, 0))); //EDGE 03
        EDGES.Add(new Edge(new Point(20, 100), new Point(80, 100))); //EDGE 14
        EDGES.Add(new Edge(new Point(10, 0), new Point(80, 100))); //EDGE 34

        var POINTS = new List<double[]>(EDGES.Count * 2);
        FillPoints(EDGES, ref POINTS);
        for (int i = 0; i < POINTS.Count; i++)
        {
            Console.WriteLine("{0} {1} {2}", i, POINTS[i][0], POINTS[i][1]);
        }

        Console.WriteLine();

        var edgeIndices = new int[EDGES.Count, 2];
        FillEdges(EDGES, POINTS, ref edgeIndices);
        for (int i = 0; i < edgeIndices.GetLength(0); i++)
        {
            Console.WriteLine("({0}, {1})", edgeIndices[i, 0], edgeIndices[i, 1]);
        }
        Console.ReadKey(true);
    }
    static bool ListContainsPoint(List<double[]> POINTS, double[] POINT) 
    {
        bool found = false;
        for (int i = 0; i < POINTS.Count; i++)
        {
            var current = POINTS[i];
            if (current[0] == POINT[0] && current[1] == POINT[1]) 
            {
                found = true;
                break;
            }
        }
        return found;
    }
    static int FindFirst(List<double[]> POINTS, double[] POINT) 
    {
        int index = -1;
        for (int i = 0; i < POINTS.Count; i++)
        {
            if (POINTS[i][0] == POINT[0] && POINTS[i][1] == POINT[1])
            {
                index = i;
                break;
            }
        }
        return index;
    }
    static void FillPoints(List<Edge> EDGES, ref List<double[]> POINTS) 
    {
        for (int i = 0; i < EDGES.Count; i++)
        {
            var current = EDGES[i];
            var firstPoint = new double[]{current.First.X, current.First.Y};
            var secondPoint = new double[]{current.Second.X, current.Second.Y};
            var firstCheck = ListContainsPoint(POINTS, firstPoint);
            var secondCheck = ListContainsPoint(POINTS, secondPoint);
            if (!firstCheck) POINTS.Add(firstPoint);
            if (!secondCheck) POINTS.Add(secondPoint);
        }
    }
    static void FillEdges(List<Edge> EDGES, List<double[]> POINTS, ref int[,] edgeIndices) 
    {
        for (int i = 0; i < EDGES.Count; i++) 
        {
            edgeIndices[i, 0] = FindFirst(POINTS, new double[] { EDGES[i].First.X, EDGES[i].First.Y });
            edgeIndices[i, 1] = FindFirst(POINTS, new double[] { EDGES[i].Second.X, EDGES[i].Second.Y });
        }
    }
}

}