What I'm trying to do is to read triangle coordinates from a text file named "p102_triangles" whose data is like this:
-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
...
What I did so far is making a triangle class accepting three Point type values for all the vertices. Each of these points have their own (x,y) coordinates. Then I read the file line by line. In each line I separate the comma separated values, convert them to integer and store them in an array of size 6 (each triangle has 6 coordination for 3 vertices). I make an object of class triangle and assign this values to each object. So basically after each line an objec of triangle is created. This is my code which works the way I want.
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();
}
}
}
Since I'm new to C# I just did it out of scratch. How can I make it better or shorter? How can I define my triangle class more efficient? Can I use anything other than collections?
You could make parsing part of Triangle
class. You should also add some error checking.
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]))
);
}
}
Do not use List
like an array. You do not need index
, use Add
instead.
Make your triangle list like this:
List<Triangle> triangles = new List<Triangle>();
...
while ((line = file.ReadLine()) != null) {
triangles.Add(Triangle.Parse(line));
}
Your Triangle
class
is essentially a class
with three Point
(which is a struct
). Therefore, it may be more representative to consider to put a Triangle
as a struct
too.
Alternatively, you could also use three Vector3
struct
instances in XNA
/ Unity
game library to define Triangle
. This is more commonly used, I believe. Note: as you could represent Triangle
in X,Y,Z
coordinates instead of X,Y
coordinate
Other than that, the way you do it (unless you want to process it further later on), IMHO, is good enough for your use.
If you just wanna make it shorter then I guess this will do... I re-created the triangle and point classes since I did it on console. Just disregard those.
class Program
{
static void Main(string[] args)
{
string fileName = "readLines.txt";
IEnumerable<string> lines = null;
List<Triangle> triangles = new List<Triangle>();
lines = File.ReadLines(fileName);
foreach(var line in lines)
{
string[] lineValues = line.Split(',');
triangles.Add(new Triangle(
new Point(int.Parse(lineValues[0]), int.Parse(lineValues[1])),
new Point(int.Parse(lineValues[2]), int.Parse(lineValues[3])),
new Point(int.Parse(lineValues[4]), int.Parse(lineValues[5]))));
}
Console.ReadLine();
}
}
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 class Point
{
public Point(int x, int y)
{
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.