我正在努力尝试关联两个字符串列表(它们被称为"列表A和#34;以及" Pairs")。我的目标是计算由列表中引用的列表A中的点形成的行列式" Pairs"。
列表如下所示:
ListA(字符串)
[0]: "30 10 0," //These represent the coordinates x,y,z of a point
[1]: "50 10 0,"
[2]: "40 10 0,"
[3]: "10 10 0,"
[4]: "20 10 0,"
[5]: "60 10 0,"
[6]: "70 20 0,"
[7]: "75 15 0,"
[8]: "100 10 0,"
[9]: "80 10 0,"
列表对(字符串)
[0]: {5 4} //This indicates that the segment is formed by the points 5 and 4 in the List A
[1]: {0 1}
[2]: {6 7}
[3]: {6 5}
[4]: {9 6}
[5]: {2 3}
[6]: {8 9}
但是,我需要遵循一个特定的逻辑,因为我需要根据行列式的值填充两个列表。它们被称为" SegmentColl" 和" PointsColl" 。请参阅下面的解释和说明性示例:
说明
Given two generic segments a---------b and c---------d (stored in INT list pairs) and (ax,ay,az),(bx,by,bz),(cx,cy,cz),(dx,dy,dz) coordinates of such points
(stored in INT list A) the following conditions must be checked:
DET(a,b,c)= 0
DET(a,b,d)= 0
I have to compare each of the segments with the other ones and if both determinants are = 0 then I add the segment's indices to a list called
“segmentColl” and the points in List A that are referred by such indices in a list called “pointsColl”.
Example:
i) Compare segment(0) which is {5,4} in the "pairs" list against segment(1) which is {0 1}:
DET[5,4,0]=DET{[60 10 0], [20 10 0], [30, 10, 0]} = 0 (See the formula of the determinant in the struct Point)
DET(5,4,1)= DET{[60 10 0], [20 10 0] , [50, 10, 0]} =0
Both determinants are = 0 so:
1) I add the segment {0 1} to the FIRST row in INT LIST called “segmentColl”
SegmentColl= {[0,1],[ , ],[ , ],[ , ]}
2) I add the points represented by these indices {[20 20 0] [40 20 0]} to the FIRST row in INT LIST called “pointsColl”.
PointsColl= {[20 20 0] [40 20 0] , [...] [...] , [...] [...] , [...] [...]}
ii) Compare segment (0) against segment (2) doing DET(5,4,6) and DET(5,4,7). In this case
the determinants /=0 so they are not added to the FIRST row of “segmentColl” and “pointsColl”.
We keep on doing this for segment(0) against segment(3), then segment(0) against segment(4) and so on.
iii) Once segment(0) have been compared against all the other segments we move onto the next segment (1) and we do the same as with
segment (0). This will populate the SECOND row of the lists “segmentColl” and “pointsColl”.
Note: at this point we will also compare segment(1) with segment(0) again because I want it to be in the SECOND row of the lists if
the determinant is zero (which is the case).
We will repeat this process so each segment has been compared with ALL the others.
The expected result is:
SegmentColl:
{[0,1],[2,3],[5,4],[8,9]} // These four segments are collinear (det=0)
{[6,7],[9,6]} // These two segments are collinear (det=0)
PointsColl:
{[20 20 0] [40 20 0] , [...] [...] , [...] [...] , [...] [...]} // The points represented by the 4 segments in segmentColl (row 0)
{[70 20 0] [75 15 0] , [...] [...]} // The points represented by the two segments in segmentColl (row 1)
我的代码不是很好,但我认为那里的结构可能会有所帮助,特别是在计算行列式时。 :
using System;
using System.Collections.Generic;
//This struct have been previously used to define the List "pairs"
struct Pair
{
public int X { get; set; }
public int Y { get; set; }
public Pair(int x, int y)
: this()
{
X = x;
Y = y;
}
public override string ToString()
{
return String.Format("{0} {1}", this.X, this.Y);
}
}
// This struct has been previously used to define the points in List A and to provide a way to
// calculate the determinants
struct Point
{
public readonly int X;
public readonly int Y;
public readonly int Z;
public Point(int x, int y, int z)
{
X = x;
Y = y;
Z = z;
}
public override string ToString()
{
return String.Format("{0} {1} {2},", X, Y, Z);
}
private static double Determinant(Point a, Point b, Point c)
{
return (b.Y - a.Y) * (c.Z - a.Z) + (b.Z - a.Z) * (c.X - a.X) + (b.X - a.X) * (c.Y - a.Y) - (b.Z - a.Z) * (c.Y - a.Y) - (b.X - a.X) * (c.Z - a.Z) - (b.Y - a.Y) * (c.X - a.X);
}
public static bool Collinear(Point a, Point b, Point c)
{
return Determinant(a, b, c) == 0.0;
}
public static Point FromString(string p)
{
string[] items = p.Replace(",", "").Split(' ');
int[] coords = new int[3];
for (int i = 0; i < 3; i++) coords[i] = int.Parse(items[i]);
return new Point(coords[0], coords[1], coords[2]);
}
}
class Program
{
static void Main()
{
#region String Lists Input
// This list represent the x,y,z coordinates of points.
var ListA = new List<string>();
ListA.Add("30 10 0,");
ListA.Add("50 10 0,");
ListA.Add("40 10 0,");
ListA.Add("10 10 0,");
ListA.Add("20 10 0,");
ListA.Add("60 10 0,");
ListA.Add("70 20 0,");
ListA.Add("75 15 0,");
ListA.Add("100 10 0,");
ListA.Add("80 10 0,");
//This list have information about the connectivity of the points (indexes in List A forming the edges). For example
//the first pair (5,4) refers to the points "60 10 0," and "20 10 0," in List A.
var pairs = new List<Pair>();
pairs.Add(new Pair(5, 4));
pairs.Add(new Pair(0, 1));
pairs.Add(new Pair(6, 7));
pairs.Add(new Pair(6, 5));
pairs.Add(new Pair(9, 6));
pairs.Add(new Pair(2, 3));
pairs.Add(new Pair(8, 9));
#endregion
// Code goes here as per explanation
Console.ReadKey();
}}
我有点迷失,试图引用这两个列表并设置代码以使其工作。
如果你能帮助我,那就太棒了!
非常感谢!!