【日志】
2020/6/26 加入了double 类型的总度数。
一、角类
一般做工程方面的朋友少不了和角打交道,但是C# 中求角的三角函数要用Math.xx
本(懒)人不喜欢,还有,角一般有两种表示形式,一种是xx°xx'xx"
,一种是以弧度表示的。但是往往需要两者之间的转化。总之吧,处于种种原因,我萌生了做个角类的想法。因为之前定义过点类,所以做起来就得心应手了。
1. 角类使用介绍
1.构造函数1,将一串数据传到一个 Angle 中,顺便算出弧度
public Angle(int d = 0, int f = 0, double m = 0)
2.构造函数2,将弧度传到一个 Angle 中,顺便算出度分秒
public Angle(double rad)
3.构造函数3,将字符数组传到一个 Angle 中,顺便算出弧度
public Angle(string[] sArry)
4.构造函数4,将字符串传到一个 Angle 中,顺便算出弧度
public Angle(string str)
5.将角转化为字符串,sDelim是分隔符,默认'°′″'
public string ToString(string sDelim=null)
6.已重载+ - * / % > < == >= <= !=
7.二维旋转矩阵
Matrix R = an.R2;//数学坐标系,顺时针 all
8.三维旋转矩阵
Matrix Rx = an.Rx;//绕x轴旋转
Matrix Ry = an.Ry;//绕y轴旋转
Matrix Rz = an.Rz;//绕z轴旋转
9.三角函数 sin cos tan csc sec cot
double sin= an.sin;//示例;
2. 源码
其所依赖的矩阵类,可参看博客:https://blog.csdn.net/Gou_Hailong/article/details/98451032
public class Angle
{//角类
public int d; //coordinate
public int f;
public double m;
public double ad;
public double rad;
public int D
{ get { return d; } set { d = value; } }
public int F
{ get { return f; } set { f = value; } }
public double M
{ get { return m; } set { m = value; } }
public double Rad
{ get { return rad; } set { rad = value; } }
public double Ad
{ get { return ad; } set { ad = value; } }
public double sin//正弦
{ get { return Math.Sin(this.rad); } }
public double cos//余弦
{ get { return Math.Cos(this.rad); } }
public double tan//正切
{ get { return Math.Tan(this.rad); } }
public double csc//余割
{ get { return 1/Math.Sin(this.rad); } }
public double sec//正割
{ get { return 1/Math.Cos(this.rad); } }
public double cot//余切
{ get { return 1/Math.Tan(this.rad); } }
//The constructor 构造函数
public Angle(int d = 0, int f = 0, double m = 0)
{//构造函数1,将一串数据传到一个 Angle 中,顺便算出弧度
this.d = d;
this.f = f;
this.m = m;
this.rad = (d + f / 60.0 + m / 3600) * Math.PI / 180;
this.ad = this.rad * 180 / Math.PI;
}
public Angle(double rad)
{//构造函数2,将弧度传到一个 Angle 中,顺便算出度分秒
this.rad = rad; rad = rad / Math.PI * 180;//度
this.d = (int)rad; rad -= this.d; rad *= 60;//分
this.f = (int)rad; rad -= this.f; rad *= 60;//秒
this.m = rad;
this.ad = this.rad * 180 / Math.PI;
}
public Angle(string[] sArry)
{//构造函数3,将字符数组传到一个 Angle 中,顺便算出弧度
this.d = int.Parse(sArry[0]);
this.f = int.Parse(sArry[1]);
this.m = double.Parse(sArry[2]);
this.rad = (d + f / 60.0 + m / 3600) * Math.PI / 180;
this.ad = this.rad * 180 / Math.PI;
}
public Angle(Angle other)
{//拷贝构造函数
rad = other.rad;
d = other.d;
f = other.f;
m = other.m;
this.ad = this.rad * 180 / Math.PI;
}
public Angle(string str)
{//构造函数4,将字符串传到一个 Angle 中,顺便算出弧度
string[] sArry = str.Split(new string[] { " ", ",", ".", "°", "'","\"" }, StringSplitOptions.RemoveEmptyEntries);
this.d = int.Parse(sArry[0]);
this.f = int.Parse(sArry[1]);
this.m = double.Parse(sArry[2]);
this.rad = (d + f / 60.0 + m / 3600) * Math.PI / 180;
this.ad = this.rad * 180 / Math.PI;
}
//运算函数
public Angle Add(Angle other)
{
Angle result = new Angle(this);
result.rad += other.rad;
result.rad = result.rad % (Math.PI * 2);
return new Angle(result.rad);
}
public Angle Subtract(Angle other)
{
Angle result = new Angle(this);
result.rad -= other.rad;
result.rad = (result.rad + 2 * Math.PI) % (Math.PI * 2);
return new Angle(result.rad);
}
public Angle Multiply(double d)
{
Angle result = new Angle(this);
result.rad *= d;
result.rad = result.rad % (Math.PI * 2);
return new Angle(result.rad);
}
public Angle Divide(double d)
{
Angle result = new Angle(this);
result.rad /= d;
result.rad = result.rad % (Math.PI * 2);
return new Angle(result.rad);
}
public string ToString(string sDelim=null)
{//将角转化为字符串,sDelim是分隔符,默认'°′″'
if(sDelim==null)
return d.ToString()+"°"+f.ToString()+"'"+Math.Round(m,1).ToString()+"\"";
else return d.ToString() + sDelim + f.ToString() + sDelim + Math.Round(m, 1).ToString();
}
//重载运算符
public static Angle operator +(Angle m1, Angle m2) { return m1.Add(m2); }
public static Angle operator -(Angle m1, Angle m2) { return m1.Subtract(m2); }
public static Angle operator -(Angle m1) { return new Angle(-m1.rad); }
public static Angle operator *(double d, Angle m1) { return m1.Multiply(d); }
public static Angle operator *(Angle m1, double d) { return m1.Multiply(d); }
public static Angle operator /(Angle m1, double d) { return m1.Divide(d); }
public static Angle operator %(Angle m1, Angle m2) { return new Angle(m1.rad % m2.rad); }
public static bool operator >(Angle m1, Angle m2) { return m1.rad > m2.rad; }
public static bool operator <(Angle m1, Angle m2) { return m1.rad < m2.rad; }
public static bool operator ==(Angle m1, Angle m2) { return m1.rad == m2.rad; }
public static bool operator >=(Angle m1, Angle m2) { return m1.rad >= m2.rad; }
public static bool operator <=(Angle m1, Angle m2) { return m1.rad <= m2.rad; }
public static bool operator !=(Angle m1, Angle m2) { return m1.rad != m2.rad; }
//旋转矩阵
public Matrix R2
{//返回此角的二维旋转矩阵:数学坐标系顺时针旋转此角度
get {
Matrix result = new Matrix(2, 2);
result[0, 0] = this.cos; result[0, 1] = this.sin;
result[1, 0] = -this.sin; result[1, 1] = this.cos;
return result;
}
}
public Matrix Rx
{//返回此角的旋转矩阵:绕x轴顺时针旋转此角度
get
{
Matrix result = new Matrix(3, 3);
result[0, 0] = 1; result[0, 1] = 0; result[0, 2] = 0;
result[1, 0] = 0; result[1, 1] = this.cos; result[1, 2] = this.sin;
result[2, 0] = 0; result[2, 1] = -this.sin; result[2, 2] = this.cos;
return result;
}
}
public Matrix Ry
{//返回此角的旋转矩阵:绕y轴顺时针旋转此角度
get
{
Matrix result = new Matrix(3, 3);
result[0, 0] = this.cos; result[0, 1] = 0; result[0, 2] = -this.sin;
result[1, 0] = 0; result[1, 1] = 1; result[1, 2] = 0;
result[2, 0] = this.sin; result[2, 1] = 0; result[2, 2] = this.cos;
return result;
}
}
public Matrix Rz
{//返回此角的旋转矩阵:绕z轴顺时针旋转此角度
get
{
Matrix result = new Matrix(3, 3);
result[0, 0] = this.cos; result[0, 1] = this.sin; result[0, 2] = 0;
result[1, 0] = -this.sin; result[1, 1] = this.cos; result[1, 2] = 0;
result[2, 0] = 0; result[2, 1] = 0; result[2, 2] = 1;
return result;
}
}
}
二、里程类
public DK(double a)
{//构造函数2
this.a = a;
this.z = (int)(a/1000);
this.d = a % 1000;
}
public DK(DK other)
{//拷贝构造函数
a = other.a;
d = other.d;
z = other.z;
}
//运算函数
public DK Add(DK other)
{
DK result = new DK(this);
result.a += other.a;
return new DK(result.a);
}
public DK Add(double d)
{
DK result = new DK(this);
result.a += d;
return new DK(result.a);
}
public DK Subtract(DK other)
{
DK result = new DK(this);
result.a -= other.a;
return new DK(result.a);
}
public DK Subtract(double dd)
{
DK result = new DK(this);
result.a -= dd;
return new DK(result.a);
}
//重载运算符
public static DK operator +(DK m1, double m2) { return m1.Add(m2); }
public static DK operator -(DK m1, double m2) { return m1.Subtract(m2); }
public static DK operator +(DK m1, DK m2) { return m1.Add(m2); }
public static DK operator -(DK m1, DK m2) { return m1.Subtract(m2); }
public static bool operator >(DK m1, DK m2) { return m1.a > m2.a; }
public static bool operator <(DK m1, DK m2) { return m1.a < m2.a; }
public static bool operator ==(DK m1, DK m2) { return m1.a == m2.a; }
public static bool operator >=(DK m1, DK m2) { return m1.a >= m2.a; }
public static bool operator <=(DK m1, DK m2) { return m1.a <= m2.a; }
public static bool operator !=(DK m1, DK m2) { return m1.a != m2.a; }
}
此两类为计算 曲线道路坐标计算 而编。
欧了!