This commit is contained in:
@@ -140,66 +140,66 @@ namespace 头罩视野.Services
|
|||||||
//}
|
//}
|
||||||
|
|
||||||
public static double CalculateEllipseArea(int[] lightData, List<(int m, int n)> lightPositions)
|
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<System.Drawing.Point> brightPoints = new List<System.Drawing.Point>();
|
|
||||||
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);
|
System.Diagnostics.Debug.WriteLine($"===== CalculateEllipseArea 开始 =====");
|
||||||
brightPoints.Add(p);
|
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<System.Drawing.Point> brightPoints = new List<System.Drawing.Point>();
|
||||||
|
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灯
|
/// 生成设备全部243盏灯的(m,n)位置 上爪1条、下爪1条、左右共用1条,各81灯
|
||||||
public static System.Drawing.Point GetLightPoint(int m, int n)
|
public static System.Drawing.Point GetLightPoint(int m, int n)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user