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));
        }
    }

我覺得以上是 WPF 框架有帶的,但是一時半會沒有找到在哪定義的,因此就自己寫了一份

以上的 Pixel 和 Degree 的定義代碼在 GitHub 上開源,請看 Office Open XML 的測量單位

其他計算請參閱 根據SVG Arc求出其開始角、擺動角和橢圓圓心 - RyzenAdorer - 博客園

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章