gpt4 book ai didi

c# - 从文本文件中读取三角形坐标并将它们放入三角形类 C#

转载 作者:行者123 更新时间:2023-12-02 17:31:47 25 4
gpt4 key购买 nike

我想做的是从名为“p102_triangles”的文本文件中读取三角形坐标,其数据如下:

-340,495,-153,-910,835,-947
-175,41,-421,-714,574,-645
-547,712,-352,579,951,-786
419,-864,-83,650,-399,171
-429,-89,-357,-930,296,-29
-734,-702,823,-745,-684,-62
...

到目前为止,我所做的是创建一个三角形类,它的所有顶点都接受三个 Point 类型值。每个点都有自己的 (x,y) 坐标。然后我逐行读取文件。在每一行中,我将逗号分隔的值分开,将它们转换为整数并将它们存储在大小为 6 的数组中(每个三角形有 3 个顶点的 6 个坐标)。我创建了一个三角形类的对象,并将该值分配给每个对象。所以基本上在每条线之后都会创建一个三角形对象。这是我的代码,它按照我想要的方式工作。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace triangleContainment
{
//Triangle class
public class triangle
{
public Point point1;
public Point point2;
public Point point3;
public triangle(Point point1, Point point2, Point point3)
{
this.point1 = point1;
this.point2 = point2;
this.point3 = point3;
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

//Global variables
List<int> pos = new List<int>(new int[6]);
List<triangle> triangle = new List<triangle>(new triangle[10000]);
private void readValues() //start of reading file function
{
char[] delimiterChars = { ',' };
System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\DaVinci\Google Drive\C# Cloud\triangleContainment\triangleContainment\p102_triangles.txt");

string line;
int index = 0;
while ((line = file.ReadLine()) != null)
{
string[] values = line.Split(delimiterChars);
int i = 0;
foreach (string v in values)
{
pos[i] = Int32.Parse(v);
i++;
}
triangle[index] = new triangle(new Point(pos[0], pos[1]), new Point(pos[2], pos[3]), new Point(pos[4], pos[5]));
index++;
}

}//end of reading file function
private void Form1_Load(object sender, EventArgs e)
{
readValues();
}
}
}

由于我是 C# 新手,所以我只是从头开始做。我怎样才能让它更好或更短?如何更有效地定义三角形类?除了集合之外我还可以使用其他东西吗?

最佳答案

您可以将解析作为 Triangle 类的一部分。您还应该添加一些错误检查。

public class Triangle {
public Point Point1 {get;}
public Point Point2 {get;}
public Point Point3 {get;}
public triangle(Point point1, Point point2, Point point3) {
this.point1 = point1;
this.point2 = point2;
this.point3 = point3;
}
public static Triangle Parse(string str) {
if (str == null) {
throw new ArgumentNullException(nameof(str));
}
var tok = str.Split(' ');
if (tok.Length != 6) {
throw new ArgumentException(nameof(str));
}
return new Triangle(
new Point(int.Parse(tok[0]), int.Parse(tok[1]))
, new Point(int.Parse(tok[2]), int.Parse(tok[3]))
, new Point(int.Parse(tok[4]), int.Parse(tok[5]))
);
}
}

不要像数组一样使用List。您不需要index,请使用Add

像这样制作三角形列表:

List<Triangle> triangles = new List<Triangle>();
...
while ((line = file.ReadLine()) != null) {
triangles.Add(Triangle.Parse(line));
}

关于c# - 从文本文件中读取三角形坐标并将它们放入三角形类 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35006270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com