反算坐标方位角小程序
本人是测绘工程专业大二学生,这学期学了C#面向对象程序设计。计算方位角在平差工作中挺繁多,本人有一点小想法就编了一个反算(知道坐标,算方位角)坐标方位角的小程序。现在发到这里,虽然不是什么好技术,但欢迎大家使用和反馈建议。
小程序的界面
代码如下
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;
namespace FANGWEIJIAO
{
//计算弧度
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Jisuan()
{
double X1 = Convert.ToDouble(x1.Text);
double X2 = Convert.ToDouble(x2.Text);
double Y1 = Convert.ToDouble(y1.Text);
double Y2 = Convert.ToDouble(y2.Text);
double Y = Y2 - Y1;
double X = X2 - X1;
double Z = Y / X;
double D1;
if (X1 == X2)
{
if (Y1 == Y2)
{
textBox5_dushu.Text = "两点重合,坐标方位角为任意值!";
}
if (Y1 < Y2)
{
textBox5_dushu.Text = "90°0′0″";
}
if (Y1 > Y2)
{
textBox5_dushu.Text = "270°0′0″";
}
}
if (X1 < X2)
{
if (Y1 < Y2)
{
D1 = Math.Atan(Z);
textBox5_dushu.Text = radtodms(D1);
}
if (Y1 == Y2)
{
textBox5_dushu.Text = "0°0′0″";
}
if (Y1 > Y2)
{
D1 = Math.Atan(-Z)+Math.PI*3/2;
textBox5_dushu.Text = radtodms(D1);
}
}
if (X2<X1)
{
if (Y1 < Y2)
{
D1 = Math.PI/2+Math.Atan(-Z);
textBox5_dushu.Text = radtodms(D1);
}
if (Y1 == Y2)
{
textBox5_dushu.Text = "180°0′0″";
}
if (Y2 < Y1)
{
D1 =Math.Atan(Z)+Math.PI;
textBox5_dushu.Text = radtodms(D1);
}
}
}
//弧度转角度
public string radtodms(double rad)
{
double sign = rad >= 0.0 ? 1.0 : -1.0;//判断正负
rad = Math.Abs(rad) * 180 / Math.PI;//将弧度取绝对值并转化为度
double[] d = new double[3];//新建一个长度为3的数组
d[0] = (int)rad;//取整获取度
d[1] = (int)((rad - d[0]) * 60);//取整获取分
d[2] = (rad - d[0] - d[1] / 60) * 60 * 60;//获取秒不取整
d[2] = Math.Round(d[2], 2);//将秒保留两位小数
if (d[2] == 60)
{
d[1] += 1;
d[2] -= 60;
if (d[1] == 60)
{
d[0] += 1;
d[1] -= 60;
}
}
d[0] = sign * d[0];//度前添加正负号
string s = Convert.ToString(d[0]) + "°" + Convert.ToString(d[1]) + "′" + Convert.ToString(d[2]) + "″";
//将度分秒赋值给文本框,并添加°′″
return s;
}
private void button1_Click(object sender, EventArgs e)
{
if (x1.Text == String.Empty || x2.Text == String.Empty || y1.Text == string.Empty || y2.Text == string.Empty)
{
MessageBox.Show("信息不完整!", "提示");
}
else
{
Jisuan();
}
}
//输入值约束
private void textBox1_X_KeyPress(object sender, KeyPressEventArgs e)
{
/*只能数字键、退格键、负号、小数点*/
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 &&
(int)e.KeyChar != 45 && (int)e.KeyChar != 46) e.Handled = true;
/*输入为负号和小数点时,且只能输入一次(负号只能最前面输入,小数点不可最前面输入)*/
if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 ||
((TextBox)sender).Text.IndexOf("-") >= 0)) e.Handled = true;
if (e.KeyChar == 46 && (((TextBox)sender).SelectionStart == 0 ||
((TextBox)sender).Text.IndexOf(".") >= 0)) e.Handled = true;
}
//清除按键
private void button2_Click(object sender, EventArgs e)
{
x1.Clear();
x2.Clear();
y1.Clear();
y2.Clear();
textBox5_dushu.Clear();
}
}
}
大家阅览或者使用过程中发现问题劳烦评论一下。谢谢各位大佬。