.利用C#编程语言封装一个类,实现两点间距离计算、方位角计算、度分秒转弧度、弧度转度分秒、度分秒转度、度转度分秒、度转弧度、弧度转度的完整代码
时间: 2024-10-23 21:20:03 浏览: 31
编程实现角度转化.docx
在C#中,我们可以创建一个名为`GeometricCalculator`的类,包含这些功能方法。以下是完整的代码:
```csharp
using System;
using System.Math;
public class GeometricCalculator
{
// 计算两点之间的欧氏距离
public double CalculateDistance(double x1, double y1, double x2, double y2)
{
return Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2));
}
// 计算两点之间的方位角(假设x轴正向为0度)
public double CalculateBearing(double x1, double y1, double x2, double y2)
{
var bearing = Math.Atan2(y2 - y1, x2 - x1);
if (bearing < 0) bearing += 2 * Math.PI; // 将角度调整到0-2π范围内
return bearing * (180 / Math.PI); // 转换成度
}
// 度分秒转弧度
public double DegreesToRadians(double degrees)
{
return degrees * Math.PI / 180;
}
// 弧度转度分秒
public string RadiansToDegreesAndSeconds(double radians)
{
double degrees = radians * 180 / Math.PI;
int wholeDegrees = Convert.ToInt32(degrees);
double remainingDegrees = degrees % 1;
int minutes = (int)(remainingDegrees * 60);
double seconds = (remainingDegrees * 60) - minutes;
return $"{wholeDegrees}°{minutes}'{seconds}\"";
}
// 直接将度转换成度分秒
public string DegreeToDegreeMinuteSecond(double degree)
{
int minutes = (int)(degree * 60);
double remainingMinutes = (degree * 60) % 60;
int seconds = (int)(remainingMinutes * 60);
return $"{degree}°{minutes}'{seconds}\"";
}
// 度转弧度
public double DegreeToRadian(double degree)
{
return degree * Math.PI / 180;
}
// 弧度转度
public double RadianToDegree(double radian)
{
return radian * 180 / Math.PI;
}
}
```
你可以通过实例化这个类并调用相应的方法来使用这些功能。
阅读全文