首先了解读取的文件结构,将每个点抽象成一个Point类,这样就方便后面我们的操作
class point
{
private String name;
private double x;
private double y;
private double z;
//1代表是关键点,0代表是散点
private int import;
private double s;
public point() { }
public point(String name,double x,double y,double z,int im)
{
this.name = name;
this.x = x;
this.y = y;
this.z = z;
this.import = im;
}
public string Name { get => name; set => name = value; }
public double X { get => x; set => x = value; }
public double Y { get => y; set => y = value; }
public double Z { get => z; set => z = value; }
public int Import { get => import; set => import = value; }
public double S { get => s; set => s = value; }
override
public String ToString()
{
return "点号"+name+"x:"+x+"y:"+y+"z:"+z+"im"+import;
}
}
做好准备工作之后可以开始读取文件了,通过循环读取每一行的数据,然后将数据封装进Point然后都Add()到list里面去(在这里我以自己的文件地址为例)
public void ReadFile()
{
String path = @"D:\系统默认\桌面\data.txt";
StreamReader read = null;
try
{
read = new StreamReader(path);
double h = Double.Parse(read.ReadLine().Split(',')[1]);
height = h;
read.ReadLine();
String[] pa = read.ReadLine().Split(',');
A1[0] = Double.Parse(pa[1]);
A1[1] = Double.Parse(pa[2]);
String[] pb = read.ReadLine().Split(',');
B[0] = Double.Parse(pb[1]);
B[1] = Double.Parse(pb[2]);
String temp = null;
read.ReadLine();
while((temp = read.ReadLine()) != null)
{
String[] tempArr = temp.Split(',');
String name = tempArr[0];
int import = 0;
if (name.Contains("K"))
{
import = 1;
}
var x = Double.Parse(tempArr[1]);
var y = Double.Parse(tempArr[2]);
var z = Double.Parse(tempArr[3]);
point po = new point(name,x,y,z,import);
list.Add(po);
}
}
catch(Exception e)
{
Console.WriteLine(e.StackTrace);
}
}
计算内插点:实现的思路为计算点到其他所有点的距离,将该距离赋值给对应点的属性s,然后使用list的OrderBy根据point对象的属性s进行升序排序,判断一下排除自身然后取前5个点进行计算内插点的高程
public double findp(point k)
{
List<point> lite = new List<point>();
for (int i = 0; i < list.Count(); i++)
{
var s = Math.Sqrt(Math.Abs(Math.Pow((k.X - list[i].X), 2) + Math.Pow((k.Y - list[i].Y), 2)));
list[i].S = s;
}
lite = list.OrderBy(p => p.S).ToList();
var sum1 = 0.0;
var sum2 = 0.0;
int count = 0;
for(int i = 1;i<lite.Count();i++)
{
if (k.Name != lite[i].Name)
{
sum1 += (lite[i].Z / lite[i].S);
sum2 += (1 / lite[i].S);
count++;
}
if(count >4)
{
break;
}
}
return sum1 / sum2;
}
计算纵断面长度
public void ComputeS()
{
foreach(point li in list)
{
if (li.Import == 1)
{
Kpo.Add(li);
}
}
var kArr = Kpo.ToArray();
for(int i = 0;i<kArr.Length-1;i++)
{
Zsum += Math.Sqrt((kArr[i+1].X-kArr[i].X)* (kArr[i + 1].X - kArr[i].X) + (kArr[i+1].Y-kArr[i].Y)*(kArr[i + 1].Y - kArr[i].Y));
}
}