Creating a new List by rearranging two other lists

Feb 18 2016 12:02 PM
Hello,

So basically I have 2 string lists: (List points, List connections). The list "points" is a set of xyz coordinates and the list "connections" shows how the points are connected in the list in order to create triangles. What I want to do is to populate a list containing the triangles. Please note that the value -1 at the end of each line in "connections" can be excluded (it is there to say that a new triangle is coming).

The class ProcessTriangles I have created is supposed to get those triangles but when I compile the code nothing happens.

Can someone help please?

My code:

 
 
 
using System;
using System.Collections.Generic;

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

public Point(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
public override string ToString()
{
return String.Format("{0} {1} {2},", X, Y, Z);
}
public static Point FromString(string p)
{
string[] items = p.Replace(",", "").Split(' ');
double[] coords = new double[3];
for (int i = 0; i < 3; i++) coords[i] = double.Parse(items[i]);
return new Point(coords[0], coords[1], coords[2]);
}
}

struct Triangle
{
public readonly Point P1;
public readonly Point P2;
public readonly Point P3;

public Triangle(Point p1, Point p2, Point p3)
{
P1 = p1;
P2 = p2;
P3 = p3;
}
public override string ToString()
{
return String.Format("{0} {1} {2},", P1, P2, P3);
}
}

class Program
{
static void Main()
{
#region listDefinition
var points = new List<string>
{
"0 0 128.588459085565,",
"25 0 134.979628462965,",
"0 0 134.979628462965,",
"25 0 128.588459085565,",
"25 0 140.100717207301,",
"0 0 140.100717207301,",
"25 0 143.87494372892,",
"0 0 143.87494372892,",
"25 0 146.245863353464,",
"0 0 146.245863353464,",
"25 0 147.181161545899,",
"0 0 147.181161545899,",
"25 0 146.668001779529,",
"0 0 146.668001779529,",
"25 0 144.711312619297,",
"0 0 144.711312619297,",
"25 0 141.340784276868,",
"0 0 141.340784276868,",
"25 0 136.604499944182,",
"0 0 136.604499944182,",
"25 0 130.573315654463,",
"0 0 130.573315654463,",
"25 0 123.341482952817,",
"0 0 123.341482952817,",
"25 0 115.031289382498,",
"0 0 115.031289382498,",
"25 0 105.793445439687,",
"0 0 105.793445439687,"
};

var connections = new List<string>
{
"0, 1, 2,-1",
"1, 4, 2,-1",
"2, 4, 5,-1",
"4, 6, 5,-1",
"5, 6, 7,-1",
"6, 8, 7,-1",
"7, 8, 9,-1",
"8, 10, 9,-1",
"9, 10, 11,-1",
"10, 12, 11,-1",
"11, 12, 13,-1",
"12, 14, 13,-1",
"13, 14, 15,-1",
"14, 16, 15,-1",
"15, 16, 17,-1",
"16, 18, 17,-1",
"17, 18, 19,-1",
"18, 20, 19,-1",
"19, 20, 21,-1",
"20, 22, 21,-1",
"21, 22, 23,-1",
"22, 24, 23,-1",
"23, 24, 25,-1",
"24, 26, 25,-1",
"25, 26, 27,-1",
"0, 3, 1,-1"
};
#endregion

ProcessTriangles(points, connections);
PrintGroup(points, connections);
}

public static List<string> ProcessTriangles(List<string> points, List<string> connections)
{
List<string> trianglesOut = new List<string>();

for (int i = 0; i < connections.Count; i++)
{
string[] items = connections[i].Split(',');
int[] indices = new int[6];
for (int j = 0; j < 3; j++) indices[j] = int.Parse(items[j]);

Point v0 = Point.FromString(points[indices[0]]);
Point v1 = Point.FromString(points[indices[1]]);
Point v2 = Point.FromString(points[indices[2]]);

Triangle tri = new Triangle(v0, v1, v2);
trianglesOut.Add(tri.ToString());

--i;
}

return trianglesOut;
}

static void PrintGroup(List<string> points, List<string> connections)
{
Console.WriteLine("-- GROUP --");
Console.WriteLine("\nLIST1\n");
foreach (string point in points) Console.WriteLine(point);
Console.WriteLine("\nLIST2\n");
foreach (string connection in connections) Console.WriteLine(connection);
Console.WriteLine();
}
}
 

Answers (1)