From 51bb15e2a1627ec4ba4c09a5a03d03dbab8bb3b7 Mon Sep 17 00:00:00 2001 From: xyy <544939200@qq.com> Date: Wed, 6 May 2026 19:52:40 +0800 Subject: [PATCH] --- 头罩视野slove/头罩视野/Services/GetArea.cs | 116 ++++++++++----------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/头罩视野slove/头罩视野/Services/GetArea.cs b/头罩视野slove/头罩视野/Services/GetArea.cs index 88a8d90..ea43fb5 100644 --- a/头罩视野slove/头罩视野/Services/GetArea.cs +++ b/头罩视野slove/头罩视野/Services/GetArea.cs @@ -140,66 +140,66 @@ namespace 头罩视野.Services //} public static double CalculateEllipseArea(int[] lightData, List<(int m, int n)> lightPositions) -{ - // 日志:方法入口 - System.Diagnostics.Debug.WriteLine($"===== CalculateEllipseArea 开始 ====="); - System.Diagnostics.Debug.WriteLine($"lightData.Length = {lightData?.Length ?? 0}"); - System.Diagnostics.Debug.WriteLine($"lightPositions.Count = {lightPositions?.Count ?? 0}"); - System.Diagnostics.Debug.WriteLine($"totalLights = {totalLights}"); - - // 1. 参数校验 - if (lightData == null || lightPositions == null) - { - System.Diagnostics.Debug.WriteLine("错误:lightData 或 lightPositions 为 null"); - return double.NaN; - } - - if (lightData.Length != totalLights || lightPositions.Count != totalLights) - { - System.Diagnostics.Debug.WriteLine($"数据长度不匹配:lightData.Length={lightData.Length}, totalLights={totalLights}, lightPositions.Count={lightPositions.Count}"); - // 这里可以根据实际需要决定是否抛出异常或使用较小长度继续 - } - - // 2. 收集亮灯坐标 - List brightPoints = new List(); - for (int i = 0; i < totalLights && i < lightData.Length && i < lightPositions.Count; i++) - { - if (lightData[i] == 1) { - var (m, n) = lightPositions[i]; - System.Drawing.Point p = GetLightPoint(m, n); - brightPoints.Add(p); + // 日志:方法入口 + System.Diagnostics.Debug.WriteLine($"===== CalculateEllipseArea 开始 ====="); + System.Diagnostics.Debug.WriteLine($"lightData.Length = {lightData?.Length ?? 0}"); + System.Diagnostics.Debug.WriteLine($"lightPositions.Count = {lightPositions?.Count ?? 0}"); + System.Diagnostics.Debug.WriteLine($"totalLights = {totalLights}"); + + // 1. 参数校验 + if (lightData == null || lightPositions == null) + { + System.Diagnostics.Debug.WriteLine("错误:lightData 或 lightPositions 为 null"); + return double.NaN; + } + + if (lightData.Length != totalLights || lightPositions.Count != totalLights) + { + System.Diagnostics.Debug.WriteLine($"数据长度不匹配:lightData.Length={lightData.Length}, totalLights={totalLights}, lightPositions.Count={lightPositions.Count}"); + // 这里可以根据实际需要决定是否抛出异常或使用较小长度继续 + } + + // 2. 收集亮灯坐标 + List brightPoints = new List(); + for (int i = 0; i < totalLights && i < lightData.Length && i < lightPositions.Count; i++) + { + if (lightData[i] == 1) + { + var (m, n) = lightPositions[i]; + System.Drawing.Point p = GetLightPoint(m, n); + brightPoints.Add(p); + } + } + + System.Diagnostics.Debug.WriteLine($"收集到的亮灯点数:{brightPoints.Count}"); + + if (brightPoints.Count < 5) + { + System.Diagnostics.Debug.WriteLine($"警告:亮灯点不足5个(实际{brightPoints.Count}),无法拟合椭圆,返回 NaN"); + return double.NaN; + } + + // 3. 拟合椭圆 + var (cx, cy, a, b, area) = FitEllipse(brightPoints); + + System.Diagnostics.Debug.WriteLine($"拟合结果:cx={cx}, cy={cy}, a={a}, b={b}, area={area}"); + + if (double.IsNaN(a) || double.IsNaN(b) || double.IsNaN(area)) + { + System.Diagnostics.Debug.WriteLine("错误:拟合结果包含 NaN"); + return double.NaN; + } + + if (a <= 0 || b <= 0) + { + System.Diagnostics.Debug.WriteLine($"错误:椭圆半轴无效(a={a}, b={b}),面积计算将得到 NaN 或 0"); + return double.NaN; + } + + System.Diagnostics.Debug.WriteLine($"最终返回面积:{area}"); + return area; } - } - - System.Diagnostics.Debug.WriteLine($"收集到的亮灯点数:{brightPoints.Count}"); - - if (brightPoints.Count < 5) - { - System.Diagnostics.Debug.WriteLine($"警告:亮灯点不足5个(实际{brightPoints.Count}),无法拟合椭圆,返回 NaN"); - return double.NaN; - } - - // 3. 拟合椭圆 - var (cx, cy, a, b, area) = FitEllipse(brightPoints); - - System.Diagnostics.Debug.WriteLine($"拟合结果:cx={cx}, cy={cy}, a={a}, b={b}, area={area}"); - - if (double.IsNaN(a) || double.IsNaN(b) || double.IsNaN(area)) - { - System.Diagnostics.Debug.WriteLine("错误:拟合结果包含 NaN"); - return double.NaN; - } - - if (a <= 0 || b <= 0) - { - System.Diagnostics.Debug.WriteLine($"错误:椭圆半轴无效(a={a}, b={b}),面积计算将得到 NaN 或 0"); - return double.NaN; - } - - System.Diagnostics.Debug.WriteLine($"最终返回面积:{area}"); - return area; -} /// 生成设备全部243盏灯的(m,n)位置 上爪1条、下爪1条、左右共用1条,各81灯 public static System.Drawing.Point GetLightPoint(int m, int n) {