SectorSearch.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. namespace SimulationCommon;
  2. public class LocationW
  3. {
  4. public double Lon { get; set; }
  5. public double Lat { get; set; }
  6. }
  7. public class SectorSearch
  8. {
  9. // 计算向量角度
  10. private static double CalculateVectorAngle(double[] vector)
  11. {
  12. double vectorCos = vector[0] / Math.Sqrt(vector[0] * vector[0] + vector[1] * vector[1]);
  13. double vectorAngle = 0;
  14. if (vector[1] >= 0)
  15. {
  16. vectorAngle = Math.Acos(vectorCos);
  17. }
  18. else if (vector[1] < 0)
  19. {
  20. vectorAngle = 2 * Math.PI - Math.Acos(vectorCos);
  21. }
  22. return vectorAngle;
  23. }
  24. // 根据角度和距离移动点
  25. private static LocationW MovePointByVectorAndAngle(double angle, double distance, LocationW startPoint, double[] vector)
  26. {
  27. double vectorAngle = CalculateVectorAngle(vector) + angle;
  28. return new LocationW
  29. {
  30. Lon = startPoint.Lon + distance * Math.Cos(vectorAngle),
  31. Lat = startPoint.Lat + distance * Math.Sin(vectorAngle)
  32. };
  33. }
  34. // 主逻辑
  35. public static List<Point> PerformSearch( double sweepWidth)
  36. {
  37. var boundaryPoints = new List<LocationW>();
  38. var outputWaypoints = new List<Point>();
  39. var basePoint = new LocationW();
  40. var baseDir = new double[] { 0, 1 };
  41. double searchAngle = 30;
  42. // Console.WriteLine("输入搜索角度:");
  43. // double searchAngle = double.Parse(Console.ReadLine());
  44. //
  45. // Console.WriteLine("输入扫描宽度:");
  46. // double sweepWidth = double.Parse(Console.ReadLine());
  47. double rs = sweepWidth / Math.Sin(searchAngle / 2);
  48. int numPoints = (int)(360 / searchAngle);
  49. boundaryPoints.Add(basePoint);
  50. for (int i = 0; i < numPoints; i++)
  51. {
  52. var midpoint = MovePointByVectorAndAngle(i * searchAngle, rs, basePoint, baseDir);
  53. boundaryPoints.Add(midpoint);
  54. midpoint.Lat = basePoint.Lat;
  55. midpoint.Lon = basePoint.Lon;
  56. }
  57. var num = new int[(int)(3 * (180 / searchAngle) + 1)];
  58. num[0] = 0;
  59. for (int i = 1, n = 1, m = 1; n < 3 * (180 / searchAngle); i += 2)
  60. {
  61. m = i;
  62. num[n++] = m;
  63. m = i + 1;
  64. num[n++] = m;
  65. m = 0;
  66. num[n++] = m;
  67. m = (int)(i + 1 + 180 / searchAngle);
  68. num[n++] = m;
  69. m = (int)(i + 180 / searchAngle);
  70. num[n++] = m;
  71. m = 0;
  72. num[n++] = m;
  73. }
  74. foreach (var index in num)
  75. {
  76. outputWaypoints.Add(new Point() { lat = boundaryPoints[index].Lat, lon = boundaryPoints[index].Lon });
  77. }
  78. return outputWaypoints;
  79. }
  80. }