所以基本上我有2个字符串列表:(列表点,列表连接)。 列表"点"是一组xyz坐标和列表"连接"显示如何在列表中连接点以创建三角形。我想要做的是填充包含三角形的列表。请注意"连接"中每一行末尾的值-1可以排除(可以说新的三角形即将到来)。
我创建的类ProcessTriangles应该得到那些三角形但是当我编译代码时没有任何反应。
有人可以帮忙吗?
我的代码:
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();
}
}
答案 0 :(得分:1)
在ProcessTriangles()
方法中,在for
循环中,您--i;
只需删除它。
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());
//deleted --i;
}
答案 1 :(得分:1)
您的ProcessTriangles循环永远不会以app/mock-heroes.ts
--i
答案 2 :(得分:1)
以下LINQ语句将根据示例数据创建三角形:
var triangles = connections
.SelectMany((cs, i) => cs.Split(',')
.Select(c => new
{
triangleIndex = i,
pointIndex = int.Parse(c.Trim())
}))
.Where(tp => tp.pointIndex != -1)
.Join(points
.Select((ps, i) => new
{
index = i,
coords = ps.TrimEnd(new[] { ',' }).Split(' ')
.Select(p => double.Parse(p.Trim()))
.ToArray()
}), tp => tp.pointIndex, p => p.index, (tp, p) => new
{
tp.triangleIndex,
point = new Point(p.coords[0], p.coords[1], p.coords[2])
})
.GroupBy(j => j.triangleIndex, i => i.point)
.Select(g => g.ToArray())
.Select(g => new Triangle(g[0], g[1], g[2]))
.ToList();
输出:
- 0 0 128.588459085565,25 0 134.979628462965,0 0 134.979628462965 ,,
- 25 0 134.979628462965,25 0 140.100717207301,0 0 134.979628462965 ,,
- 0 0 134.979628462965,25 0 140.100717207301,0 0 140.100717207301 ,,
25 0 140.100717207301,25 0 143.87494372892,0 0 140.100717207301 ,,
等...