using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Collections;
namespace 方位角计算器
{
public partial class 方位角计算器 : Form
{
private string strline;
private string x;
public 方位角计算器()
{
InitializeComponent();
}
//新建公共字段
public string allstring = "";
//新建存放数据的类Point3d
public class Point3d
{ //新建字段用于储存坐标数据
public string name;
public double x, y, z;
//建立换算方位角方法
public static double Angle(Point3d p1, Point3d p2) //public static 表示公共的静态方法,可以通过类名调用
{
double x3 = p2.x - p1.x; //表示两点之间x的坐标差
double y3 = p2.y - p1.y; //表示两点之间y的坐标差
if (x3 > 0 && y3 >= 0)//x、y差都大于0,方位角等于atan
return Math.Atan(Math.Abs(y3 / x3));
else if (x3 < 0 && y3 >= 0) //x差小于0,y差大于或等于0,方位角等于PI减atan
return Math.PI - Math.Atan(Math.Abs(y3 / x3));
else if (x3 > 0 && y3 < 0) //x差大于0,y差小于0,方位角等于2PI减atan
return 2 * Math.PI - Math.Atan(Math.Abs(y3 / x3));
else if (x3 < 0 && y3 < 0)//x、y差都小于0,方位角等于PI加atan
return Math.PI + Math.Atan(Math.Abs(y3 / x3));
else
return 2 * Math.PI - Math.Atan(Math.Abs(y3 / x3));
}
}
//打开文件并读取到富文本框
private void button1_Click(object sender, EventArgs e)
{
//打开选择文件对话框
OpenFileDialog openFileDialog1 = new OpenFileDialog(); //动态初始化
//可以选择打开DAT文件或者txt文件
openFileDialog1.Filter = "DAT文件 (*.DAT)|*.DAT|(*.txt)|*.*";
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//将文件路径读取显示到textBox1中
textBox1.Text = openFileDialog1.FileName;
//使用StreamReader类来读取文件
FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
//FileStream 类,为文件提供 Stream(数据流),既支持同步读写操作,也支持异步读写操作。
//FileMode.open:指定操作系统应打开现有文件
// FileAccess.Read:对文件进行读取
StreamReader sr = new StreamReader(fs, Encoding.Default);
//StreamReader 类用于从流中读取字符串。
// 从数据流中读取每一行,直到文件的最后一行
sr.BaseStream.Seek(0, SeekOrigin.Begin);//(0, SeekOrigin.Begin)重置一个流到开始
this.richTextBox1.Text = "";
string strLine = sr.ReadLine();
//分割字符串,将数据显示到富文本框
while (strLine != null)
{
//用逗号分割字符串1
string[] b = strLine.Split(',');
//将点的编号单独一行,点的x、y、z坐标间用逗号分隔,在富文本框中显示数据
this.richTextBox1.Text += b[0] + "\r " + b[3] + "," + b[2] + "," + b[4] + "\r";
strLine = sr.ReadLine();
}
}
}
//将结果另存为
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
save.InitialDirectory = "C:\\"; //将C盘设为默认路径
save.Filter = "(*.txt)|*.*"; //设置保存文件格式为txt格式
save.RestoreDirectory = true;
//RestoreDirectory是控制当前程序中的System.Environment.CurrentDirectory的,也就是,当属性设置为true时,System.Environment.CurrentDirectory永远是程序从中启动的文件夹目录;而设置为false是,则每次使用OpenFileDialog选择完文件后,System.Environment.CurrentDirectory会变成最后一次打开文件的 目录
if (save.ShowDialog() == DialogResult.OK)
{
string str = save.FileName;
//向指定的文件中追加内容,如果文件不存在,则创建文件
StreamWriter sw = File.AppendText(str);
sw.Write(this.richTextBox2.Text);
sw.Flush();
sw.Close();
}
}
//清楚已计算的方位角
private void button4_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
}
//清楚已计算的方位角
private void button5_Click(object sender, EventArgs e)
{
richTextBox2.Text = "";
allstring = "";
}
//计算方位角
private void button3_Click(object sender, EventArgs e)
{
string mystr = richTextBox1.Text; //从富文本框读取数据
mystr = mystr.Replace("\n", ","); //遇到逗号换行,即一个字符串一行
string[] ss = mystr.Split(','); //用逗号分割字符串
int count = 0; //引入变量count用于字符串计数
for (int i = 0; i < mystr.Length; i++)//该循环用于获取字符串数
{
if (mystr[i] == ',')
count++;
}
Point3d[] pts = new Point3d[(count + 1) / 4]; //最后一个字符串后没有逗号,所以count要+1
for (int i = 0; i < (count + 1) / 4; i++) //读取x、y、z
{
pts[i] = new Point3d();
pts[i].name = string.Format(ss[4 * (i + 1) - 4]);
pts[i].x = double.Parse(ss[4 * (i + 1) - 3]);
pts[i].y = double.Parse(ss[4 * (i + 1) - 2]);
pts[i].z = double.Parse(ss[4 * (i + 1) - 1]);
}
for (int i = 0; i < pts.Length - 1; i++)
{
string name1 = pts[i].name;
string name2 = pts[i + 1].name;
string x1 = string.Format("{0:f3}", pts[i].x);//f为浮点型输出数据精度为三位小数
string x2 = string.Format("{0:f3}", pts[i + 1].x);
string y1 = string.Format("{0:f3}", pts[i].y);
string y2 = string.Format("{0:f3}", pts[i + 1].y);
string z1 = string.Format("{0:f3}", pts[i].z);
string z2 = string.Format("{0:f3}", pts[i + 1].z);
double rad = Point3d.Angle(pts[i], pts[i+1]);
double single = (180 / Math.PI) * rad;//计算方位角弧度
double singles = 3600 * single;
string single1 = string.Format("{0}", Math.Floor(singles / 3600));//Math.Floor 返回小于等于x的最大整数
string single2 = string.Format("{0}", Math.Floor((singles - ((int)single) * 3600) / 60));
string single3 = string.Format("{0}", Math.Floor(singles - ((int)single) * 3600 - (Math.Floor((singles - ((int)single) * 3600) / 60)) * 60));
string allpoints = string.Format("{0},{1},{2},{3}\r{4},{5},{6},{7}\r", name1, x1, y1, z1, name2, x2, y2, z2);
string chinse = string.Format("点{1}至点{0}的坐标方位角为{2}°{3}′{4}″\r", i + 2, i + 1, single1, single2, single3);
allstring = allstring + allpoints + chinse+"\r";
}
richTextBox2.Text = allstring;
}
//将读取的原始数据另存为
private void button6_Click(object sender, EventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
save.InitialDirectory = "C:\\";
save.Filter = "(*.txt)|*.*"; //打开text文件
save.RestoreDirectory = true;
if (save.ShowDialog() == DialogResult.OK)
{
string str = save.FileName;
//向指定的文件中追加内容,如果文件不存在,则创建文件
StreamWriter sw = File.AppendText(str);
sw.Write(this.richTextBox1.Text);
sw.Flush();
sw.Close();
}
}
}
}
运行程序:
测试数据、程序下载:https://download.csdn.net/download/HAIIAKU/20678461?spm=1001.2014.3001.5503