diff --git a/Plane.FormationCreator/App.xaml b/Plane.FormationCreator/App.xaml index a3bae2d..8c7a789 100644 --- a/Plane.FormationCreator/App.xaml +++ b/Plane.FormationCreator/App.xaml @@ -37,8 +37,8 @@ True="【编辑模式】" /> + False="平面视图" + True="立体视图" /> /// 设置单个虚拟ID @@ -94,75 +94,123 @@ namespace Plane.FormationCreator.ViewModels int colCount = SingleRowCount; int copterCount = _copterManager.Copters.Count; + /* if (copterCount % colCount != 0) { Alert.Show($"列数设置错误!"); return; } - + */ + //列数用户设置colCount + //计算行数rowCount int rowCount = copterCount / colCount; + //有多的飞机,多一行 + //有整除余,代表最后一行是否没有满 + bool havemode = (copterCount % colCount != 0); + + //if (havemode) + // rowCount++; int num = 1; - if (_copterManager.AcceptingControlCopters.Count() == 4) + //目前方案是 选中3架飞机,1号飞机,第一排任意飞机,第一列任意飞机(尽量远离1号机) + //用于计算横向和纵向的角度 + if (_copterManager.AcceptingControlCopters.Count() == 3) { var copters = _copterManager.AcceptingControlCopters.ToList(); var firstCopter = copters[0]; var secondCopter = copters[1]; var thirdCopter = copters[2]; - var fourthCopter = copters[3]; + // var fourthCopter = copters[3]; - //第一列角度 - double firstColazimuth = GeographyUtils.RadToDeg(GeographyUtils.CalcDirection2D( - firstCopter.Latitude, firstCopter.Longitude, - thirdCopter.Latitude, thirdCopter.Longitude)); - //最后列角度 - double lastColazimuth = GeographyUtils.RadToDeg(GeographyUtils.CalcDirection2D( - secondCopter.Latitude, secondCopter.Longitude, - fourthCopter.Latitude, fourthCopter.Longitude)); + //第一列角度(1,3) 用于计算每行第一架飞机 + float firstColDirect =(float) GeographyUtils.RadToDeg(firstCopter.CalcDirection2D(thirdCopter)); + //最后列角度(2,4) 用于计算每行最后一架飞机 + float lastColDirect =(float) GeographyUtils.RadToDeg(secondCopter.CalcDirection2D(fourthCopter)); + + + + /* + // //四个角从1号机开始选取(第一排第一个1, 第一排最后2, 最后一排第一个3 , 最后一排最后4) + //第一列角度(1,3) 用于计算每行第一架飞机 + float firstColazimuth = GeographyUtils.RadToDeg(GeographyUtils.CalcDirection2D( + firstCopter.Latitude, firstCopter.Longitude, + thirdCopter.Latitude, thirdCopter.Longitude)); + //最后列角度(2,4) 用于计算每行最后一架飞机 + float lastColazimuth = GeographyUtils.RadToDeg(GeographyUtils.CalcDirection2D( + secondCopter.Latitude, secondCopter.Longitude, + fourthCopter.Latitude, fourthCopter.Longitude)); + + */ ICopter destCopter = null; + ICopter sourceCopter = null; ICopter rowfirstCopter = null; ICopter rowlastCopter = null; + //行头位置,行尾位置,用ILocation2D不用ICopter是因为可能没有飞机 + ILocation2D rowLocfirst = null; + ILocation2D rowLoclast = null; + + //行查找步长 + int rowfindstep = 1; + //按行循环 for (int i = 0; i < rowCount; i++) { - + + //第一行开始结束是(1,2) if (i == 0) { rowfirstCopter = firstCopter; + rowLocfirst = rowfirstCopter; rowlastCopter = secondCopter; + rowLoclast = rowlastCopter; } + //最后一行(3,4) else if (i == rowCount - 1) { rowfirstCopter = thirdCopter; + rowLocfirst = rowfirstCopter; rowlastCopter = fourthCopter; + rowLoclast = rowlastCopter; } else + //中间的行找该行的第一架和最后一架飞机 { - rowfirstCopter = FindNextCopter(rowfirstCopter, firstColazimuth); - rowlastCopter = FindNextCopter(rowlastCopter, lastColazimuth); - } - - if (rowfirstCopter == null || rowlastCopter == null) - { - Alert.Show($"在确认第{i+1}列时候无法找到起始或结束飞机"); - return; + + rowLocfirst=rowLocfirst.CalcLatLngSomeMetersAway2D(firstColDirect, 1.5f); + rowLoclast = rowLoclast.CalcLatLngSomeMetersAway2D(firstColDirect, 1.5f); + + rowLocfirst.CalcLatLngSomeMetersAway2D(firstColDirect, 1.5f); + + + + rowfirstCopter = FindNextCopter(rowLocfirst, firstColDirect, 1); + rowlastCopter = FindNextCopter(rowLoclast, lastColDirect, 1); } + if (rowfirstCopter == null) + Message.Show($"无法找到第{i + 1}行的起始飞机"); + if (rowlastCopter == null) + Message.Show($"无法找到第{i + 1}行的结束飞机"); + rowfindstep = 1; + //每一行按列循环 for (int j = 0; j < colCount; j++) { - + + //计算每一行方向 double curRowAzimuth = GeographyUtils.RadToDeg(GeographyUtils.CalcDirection2D( rowfirstCopter.Latitude, rowfirstCopter.Longitude, rowlastCopter.Latitude, rowlastCopter.Longitude)); if (j == 0) { destCopter = rowfirstCopter; + sourceCopter = rowfirstCopter; } else if (j == colCount - 1) { destCopter = rowlastCopter; } else + //按行查找飞机 { ICopter tempCopter = null; tempCopter = _copterManager.Copters.FirstOrDefault(o => o.VirtualId == num); @@ -173,19 +221,36 @@ namespace Plane.FormationCreator.ViewModels tempCopter = _copterManager.Copters.FirstOrDefault(o => o.VirtualId == num); } - - destCopter = FindNextCopter(destCopter, curRowAzimuth); + //根据当前飞机destCopter,按curRowAzimuth方向按间距1.5米找下一个飞机 + destCopter = FindNextCopter(sourceCopter, curRowAzimuth, rowfindstep); if (destCopter == null) { - Alert.Show($"在寻找第{num}号位置时候无法找到对应位置飞机"); - return; + Message.Show($"在寻找第{num}号位置时候无法找到对应位置飞机"); + //增加步长继续找 + rowfindstep++; + + + //Alert.Show($"在寻找第{num}号位置时候无法找到对应位置飞机"); + //return; + } + else + { + rowfindstep = 1; + sourceCopter = destCopter; + + + } } - destCopter.VirtualId = num; + if (destCopter != null) + { + //赋予虚拟ID + destCopter.VirtualId = num; - Message.Show($"飞机{destCopter.Name} 虚拟ID = {destCopter.VirtualId}"); + Message.Show($"飞机{destCopter.Name} 虚拟ID = {destCopter.VirtualId}"); + } num++; } } @@ -196,37 +261,78 @@ namespace Plane.FormationCreator.ViewModels } - private ICopter FindNextCopter(ICopter startCopter, double azimuth) + private ICopter FindNextCopter(ILocation2D startLoc, double azimuth, int findstep) { ICopter retCopter = null; bool foundCopter = false; float stepLength = 0; + double mindis = 500.0f; + while (!foundCopter) { - stepLength += 1.5f; + mindis = 500.0f; + stepLength += 1.5f * findstep; + //计算startCopter按azimuth方向移动stepLength后的位置targetLatLng Tuple targetLatLng = GeographyUtils.CalcLatLngSomeMetersAway2D( - startCopter.Latitude, startCopter.Longitude, (float)azimuth, stepLength); + startLoc.Latitude, startLoc.Longitude, (float)azimuth, stepLength); + //所有没有虚拟ID的飞机 List remainCopters = _copterManager.Copters.Where(o => o.VirtualId == 0).ToList(); + + foreach (var copter in remainCopters) { + //计算这些飞机和targetLatLng的距离 double temp = GeographyUtils.CalcDistance2D(copter.Latitude, copter.Longitude, targetLatLng.Item1, targetLatLng.Item2); - if (temp < 1.5) + + //找最近的飞机 + if (temp < mindis) + { + mindis = temp; + retCopter = copter; + } + + + //如果目标点最近未标注飞机小于1米就认为找到了 + if (mindis < 1.0) { foundCopter = true; retCopter = copter; break; } } - + //不找更多步长 + break; if (stepLength > 15) { break; } } - return retCopter; + + if (foundCopter) + return retCopter; + else + return null; + } + + + + //根据当前位置和角度距离查找下一个位置 + private ILocation2D FindNextLocal(ILocation2D startLoc, float directionDegrees, float distance) + { + ILocation2D retLoc = null; + + + + //计算startCopter按azimuth方向移动stepLength后的位置targetLatLng + Tuple targetLatLng = GeographyUtils.CalcLatLngSomeMetersAway2D( + startLoc.Latitude, startLoc.Longitude, directionDegrees, distance); + return retLoc; + } } + + } diff --git a/Plane.FormationCreator/Views/CopterListView.xaml b/Plane.FormationCreator/Views/CopterListView.xaml index 0b5ec16..516459d 100644 --- a/Plane.FormationCreator/Views/CopterListView.xaml +++ b/Plane.FormationCreator/Views/CopterListView.xaml @@ -104,7 +104,7 @@ Margin="5,0,0,0" Command="{Binding ShowLackCopterNumsCommand}"/>