# dotnet C# 根據橢圓長度和寬度和旋轉角計算出橢圓中心點的方法

``````    /// <summary>
/// 輔助進行橢圓點計算的類
/// </summary>
/// 我覺得這個類應該是框架有帶，或現成的方法，但是一時間沒找到
static class EllipseCoordinateHelper
{
/// <summary>
/// 計算橢圓中點座標
/// </summary>
/// <param name="widthRadius"></param>
/// <param name="heightRadius"></param>
/// <param name="rotationAngle"></param>
/// <returns></returns>
public static (Pixel x, Pixel y) GetEllipseCoordinate(Pixel widthRadius, Pixel heightRadius,
Degree rotationAngle)
{
// 以下爲橢圓兩個點的計算方法
// 算法請看 https://astronomy.swin.edu.au/cms/astro/cosmos/E/Ellipse

var absRotate = Math.Abs(rotationAngle.DoubleValue);
var rad = Math.Abs(absRotate - 90);
rad = rad * Math.PI / 180;
var tan = Math.Tan(rad);

var a = widthRadius.Value;
var b = heightRadius.Value;
var x = Math.Sqrt(1.0 / (1.0 / (a * a) + (tan * tan) / (b * b)));
var y = x * tan;

if (rotationAngle.DoubleValue < 0)
{
x = -x;
}

if (rotationAngle.DoubleValue > -90 && rotationAngle.DoubleValue < 90)
{
y = -y;
}

x = a + x;
y = b + y;

return (new Pixel(x), new Pixel(y));
}
}
``````