func hexagonPointByStep(user Point, length int64, block []Point) []Point { var retPoint, initPoint, tmp []Point curPointSet := set.NewSimpleSet() lastPointSet := set.NewSimpleSet() defer curPointSet.Clear() defer lastPointSet.Clear() initPoint = hexagonPoint(user) //删除有障碍物坐标 for _, v := range block { initPoint = RemovePoint(initPoint, v) } //加入集合 for _, v := range initPoint { curPointSet.Add(v) lastPointSet.Add(v) } var i int64 for i = 1; i < length; i++ { innerTmp := curPointSet.List() curPointSet.Clear() for _, v := range innerTmp { ptmp := v.(Point) //生成新的一圈坐标点 tmp = hexagonPoint(ptmp) //删除有障碍物坐标 for _, v := range block { tmp = RemovePoint(tmp, v) } //删除已经存在的点 for _, v := range tmp { if !lastPointSet.Has(v) { curPointSet.Add(v) lastPointSet.Add(v) } } } } for _, v := range lastPointSet.List() { retPoint = append(retPoint, v.(Point)) } return retPoint }
func AttkPointByStep(user Point, length int64) []Point { var retPoint, initPoint, tmp []Point if length == 0 { retPoint = append(retPoint, user) return retPoint } curPointSet := set.NewSimpleSet() lastPointSet := set.NewSimpleSet() initPoint = AttkHexagonPoint(user) for _, v := range initPoint { curPointSet.Add(v) lastPointSet.Add(v) } var i int64 for i = 1; i < length; i++ { innerTmp := curPointSet.List() curPointSet.Clear() for _, v := range innerTmp { ptmp := v.(Point) //生成新的一圈坐标点 tmp = AttkHexagonPoint(ptmp) for _, v := range tmp { if !lastPointSet.Contains(v) { curPointSet.Add(v) lastPointSet.Add(v) } } } } for _, v := range lastPointSet.List() { retPoint = append(retPoint, v.(Point)) } return retPoint }
//查找最短路径 func SearchShortestPath(posBegin, posEnd Point, block []Point) ([]Point, error) { var retPoint, tmpBuf []Point var specialPoint Point //可能重复的点 repeatNode := set.NewSimpleSet() type tmpNode struct { bestPos Point length int64 } var tmpList []tmpNode var tmp, bestNode, secNode tmpNode //目标坐标不能出现障碍物中 for _, v := range block { if v == posEnd { return retPoint, errors.New("目标坐标不该出现障碍物中") } } DO_AGAIN: retPoint = append(retPoint, posBegin) tmpBuf = hexagonPointByStep(posBegin, 1, block) for { for _, v := range tmpBuf { tmp.bestPos = v tmp.length = (v.x-posEnd.x)*(v.x-posEnd.x) + (v.y-posEnd.y)*(v.y-posEnd.y) tmpList = append(tmpList, tmp) if tmp.length == 0 { retPoint = append(retPoint, v) goto DO_END } } //寻找最佳节点 bestNode = tmpList[0] secNode = tmpList[0] for _, v := range tmpList { if v.length < bestNode.length { secNode = bestNode bestNode = v } } if bestNode.bestPos == specialPoint { bestNode = secNode } fmt.Println("bestNode:==>", bestNode) tmpList = nil //将当前最佳节点加入到路径中 retPoint = append(retPoint, bestNode.bestPos) //添加进重复的点 if !repeatNode.Contains(bestNode.bestPos) { repeatNode.Add(bestNode.bestPos) } else { specialPoint = bestNode.bestPos retPoint = nil goto DO_AGAIN } //计算当前节点的辐射范围 tmpBuf = hexagonPointByStep(bestNode.bestPos, 1, block) fmt.Println("again:==>", tmpBuf) } DO_END: return retPoint, nil }