SectorSearch.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 Point MovePointByVectorAndAngle(double angle, double distance, Point startPoint, double[] vector)
  26. {
  27. ///定义并输出新坐标点
  28. Point finalPoint = new Point();
  29. //计算已知向量与x轴正方向的夹角
  30. double vectorAngle = CalculateVectorAngle(vector);
  31. //计算目标点向量与x轴正方向的夹角
  32. vectorAngle += angle;
  33. //计算目标点向量夹角的正弦、余弦值
  34. double vectorAngleSin = Math.Sin(vectorAngle);
  35. double vectorAngleCos = Math.Cos(vectorAngle);
  36. //ArrayList<Double> XY = new ArrayList<Double>();
  37. finalPoint.lon = startPoint.lon + distance * vectorAngleCos;
  38. finalPoint.lat = startPoint.lat + distance * vectorAngleSin;
  39. return finalPoint;
  40. }
  41. // 主逻辑
  42. public static List<Point> sectorSearch(Point basePoint, double searchAngle, double sweepWidth)
  43. {
  44. List<Point> boundaryPoints = new List<Point>();
  45. List<Point> outputWaypoints = new List<Point>();
  46. double Angle = searchAngle * Math.PI / 180;
  47. double[] baseDir = { 0, 1 };
  48. int numPoints = (int)(360 / searchAngle);
  49. boundaryPoints.Add(basePoint);
  50. double rs = sweepWidth / Math.Sin(searchAngle / 2);
  51. for (int i = 0; i < numPoints; i++)
  52. {
  53. Point midpoint = MovePointByVectorAndAngle(-i * Angle, rs, basePoint, baseDir);
  54. boundaryPoints.Add(midpoint);
  55. midpoint.lat = basePoint.lat;
  56. midpoint.lon = basePoint.lon;
  57. }
  58. int[] num = new int[(int)(3 * (180 / searchAngle) + 1)];
  59. num[0] = 0;
  60. for (int i = 1, n = 1, m = 1; n < 3 * (180 / searchAngle); i += 2)
  61. {
  62. m = i;
  63. num[n] = m; n++;
  64. m = i + 1;
  65. num[n] = m; n++;
  66. m = 0;
  67. num[n] = m; n++;
  68. m = (int)(i + 1 + 180 / searchAngle);
  69. num[n] = m; n++;
  70. m = (int)(i + 180 / searchAngle);
  71. num[n] = m; n++;
  72. m = 0;
  73. num[n] = m; n++;
  74. }
  75. for (int i = 0; i < num.Length; i++)
  76. {
  77. Point p = new Point();
  78. p.lat = boundaryPoints[num[i]].lat;
  79. p.lon = boundaryPoints[num[i]].lon;
  80. outputWaypoints.Add(p);
  81. }
  82. return outputWaypoints;
  83. }
  84. }