我正在努力解决以特定方式定义的列表。情况就是这样: 我有一个名为Edge的Struct,由在代码中的struct Point中定义的两个Point组成(下面)。
我的输入列表被称为" EDGE"。使用该列表我想产生两个输出:
列表点:包含列表中出现的非重复点" 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}
/// };
}
}
}
答案 0 :(得分:1)
我建议你在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();
}
}
}
这是我的输出:
答案 1 :(得分:1)
这是我的实施:
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 });
}
}
}
}