Untitled

mail@pastecode.io avatar
unknown
swift
2 years ago
13 kB
3
Indexable
Never
    func getDotPosition() {
        let mapCenter = currentDegree
        var mapMin = mapCenter - 90
        var mapMax = mapCenter + 90
        
        if (mapMin < 0) {
            mapMin += 360
        }
        
        if (mapMax > 360) {
            mapMax -= 360
        }
        
        
        var newDotDegree = -1
        if (dotDegree < mapCenter) {
            newDotDegree = mapCenter - dotDegree
        } else {
            newDotDegree = (mapMax - mapCenter) - (mapMax - dotDegree)
        }
        
        let a = cos(deg2rad(Double(newDotDegree)))
        let positionVertical = Double(distance) * Double(a)
        
        
        let r = 180 - (90 + newDotDegree)
        let c = cos(deg2rad(Double(r)))
        var positionHorizontal: Double = 0.0
        
        var count = -1
        var position = -1
        
        if (dotDegree < mapCenter) {
            count = mainListCount / 2
            positionHorizontal = Double(distance) * c
            position = (count - Int(positionHorizontal))
        } else {
            count = mainListCount
            positionHorizontal = Double(distance) * c
            
            let aaa = count - Int(positionHorizontal)
            if (aaa > Int(positionHorizontal)) {
                position = aaa
            } else {
                position = Int(positionHorizontal)
            }
        }
        
        var mapEntityOne = [MapCellEntity(dictionary: [String : Any]())]
        var mapEntity = [[MapCellEntity(dictionary: [String : Any]())]]

        mapEntityOne.removeAll()
        for _ in 0...49 {
            mapEntityOne.append(MapCellEntity(dictionary: [String : Any]()))
        }

        mapEntity.removeAll()
        for _ in 0...49 {
            mapEntity.append(mapEntityOne)
        }
        
        if dqwr.isEmpty { dqwr = mapEntity }

        let data = addDepths(dotPositionHorizontal: position, ddd: distance, depth: Double(depth), data: dqwr, positionVertical: Int(positionVertical))
        dqwr = data!
   
        MapperModel.shared.editData(mapID: selectedMapID)
    }
    
        func deg2rad(_ number: Double) -> Double {
        return number * .pi / 180
    }
    
    private func addDepths(dotPositionHorizontal: Int, ddd: Int, depth: Double, data: [[MapCellEntity]], positionVertical: Int ) -> [[MapCellEntity]]? {
        var data = data
        var distance = ddd
        var countNewCellsVertical = 0
        var countNewCellsHorizontal = 0
        let countCellsVertical = data.count
        let countCellsHorizontal = data[0].count
        
        let needCells = (positionVertical + Int((Double(ddd) / 100.0) * 10.0))
        var needResizeHor = false
        let ab = dotPositionHorizontal-Int((Double(ddd)/100.0)*10.0)
        let bc = dotPositionHorizontal+Int((Double(ddd)/100.0)*10.0)
        
        if (ab < 0) {
            needResizeHor = true
        }
        
        if (bc > countCellsHorizontal - 1) {
            needResizeHor = true
        }
        
        if (needCells > countCellsVertical) {
            countNewCellsVertical =
            Int((positionVertical + Int((Double(ddd) / 100.0) * 10.0) - countCellsVertical))
        }
        
        
        if (needResizeHor) {
            if (dotPositionHorizontal - Int((Double(ddd) / 100.0) * 10.0) < 0) {
                let a = countCellsHorizontal - dotPositionHorizontal + Int((Double(ddd) / 100.0) * 10.0)
                countNewCellsHorizontal = a - countCellsHorizontal
            }
            
            if (dotPositionHorizontal + Int((Double(ddd) / 100.0) * 10.0) > countCellsHorizontal - 1) {
                let a = countCellsHorizontal + dotPositionHorizontal + Int((Double(ddd) / 100.0) * 10.0)
                countNewCellsHorizontal = a - countCellsHorizontal
            }
            
        }
        
        if (countNewCellsVertical != 0) {
            for _ in 0...countNewCellsVertical {
                var dataList = [MapCellEntity]() //MutableList
                for _ in 0...data[0].count {
                    dataList.append(MapCellEntity.init(dictionary: Dictionary<String, Any>()))
                }
                data.append(dataList)
            }
        }
        
        if (countNewCellsHorizontal != 0) {
            let i = data.count-1
            var index = 0
            for _ in 0...i {
                
                for ih in 0...countNewCellsHorizontal {
                    data[index].append(MapCellEntity.init(dictionary: Dictionary<String, Any>()))
                    data[index].insert(MapCellEntity.init(dictionary: Dictionary<String, Any>()), at: 0) //вставляю пустой обьект MapCellEntity
                    
                }
                index+=1
            }
        }
        
        distance = positionVertical - 1
        var reversedData = data.reduce([],{ [$1] + $0 })
        reversedData[distance][(dotPositionHorizontal+countNewCellsHorizontal)].bottomFeature = nil //инит
        if (depth != 0.0) {
            reversedData[distance][(dotPositionHorizontal + countNewCellsHorizontal)].isExactValue = true
            reversedData[distance][(dotPositionHorizontal + countNewCellsHorizontal)].depthExactValue = Int(depth)
            reversedData[distance][(dotPositionHorizontal + countNewCellsHorizontal)].direction = dotDegree
            reversedData[distance][(dotPositionHorizontal + countNewCellsHorizontal)].distance = ddd
            reversedData[distance][(dotPositionHorizontal + countNewCellsHorizontal)].pole = "SW"
            reversedData[distance][(dotPositionHorizontal + countNewCellsHorizontal)].converter = nil //тут не может быть nil
            reversedData[distance][(dotPositionHorizontal + countNewCellsHorizontal)].converterCount = 10 //вводит юзер
            
            var childArray = [String]()
            let countOfCell = Int(Double(ddd) * 0.10)
            if (dotPositionHorizontal - countOfCell > -1 || dotPositionHorizontal + countOfCell <= data[0].count || distance - countOfCell > -1 || distance + countOfCell <= data.count) {
                
                if (ddd >= 30) {
                    let minPositionHorizontal =
                    (dotPositionHorizontal + countNewCellsHorizontal) - countOfCell
                    let maxPositionHorizontal =
                    dotPositionHorizontal + countOfCell + countNewCellsHorizontal
                    let minPositionVertical = distance - countOfCell
                    let maxPositionVertical = distance + countOfCell
                    
                    var currentPositionVertical = maxPositionVertical
                    var currentPositionHorizontal = maxPositionHorizontal
                    
                    currentPositionVertical = positionVertical - 1
                    currentPositionHorizontal = minPositionHorizontal
                    
                    while (currentPositionHorizontal < (dotPositionHorizontal + countNewCellsHorizontal) && currentPositionVertical < maxPositionVertical) {
                        var pos = currentPositionHorizontal
                        while (pos <= (dotPositionHorizontal + countNewCellsHorizontal)) {
                            reversedData[currentPositionVertical][pos].depth.append(depth)
                            childArray.append(reversedData[currentPositionVertical][pos].cellID)
                            pos+=1
                        }
                        currentPositionHorizontal+=1
                        currentPositionVertical+=1
                    }
                    
                    
                    currentPositionVertical = positionVertical - 1
                    currentPositionHorizontal = maxPositionHorizontal
                    
                    while (currentPositionHorizontal > (dotPositionHorizontal + countNewCellsHorizontal) && currentPositionVertical < maxPositionVertical) {
                        var pos = currentPositionHorizontal
                        while (pos > (dotPositionHorizontal + countNewCellsHorizontal)) {
                            reversedData[currentPositionVertical][pos].depth.append(depth)
                            
                            childArray.append(reversedData[currentPositionVertical][pos].cellID)
                            pos -= 1
                        }
                        
                        currentPositionHorizontal = currentPositionHorizontal-1
                        currentPositionVertical = currentPositionVertical+1
                    }
                    
                    
                    currentPositionVertical = positionVertical - 1
                    currentPositionHorizontal = minPositionHorizontal
                    
                    while (currentPositionHorizontal < (dotPositionHorizontal + countNewCellsHorizontal) && currentPositionVertical > minPositionVertical) {
                        var pos = currentPositionHorizontal
                        if (currentPositionVertical != positionVertical - 1) {
                            while (pos <= (dotPositionHorizontal + countNewCellsHorizontal)) {
                                reversedData[currentPositionVertical][pos].depth.append(depth)
                                
                                childArray.append(reversedData[currentPositionVertical][pos].cellID)
                                pos = pos+1
                            }
                        }
                        
                        currentPositionHorizontal = currentPositionHorizontal+1
                        currentPositionVertical = currentPositionVertical-1
                    }
                    
                    currentPositionVertical = positionVertical - 1
                    currentPositionHorizontal = maxPositionHorizontal
                    
                    while (currentPositionHorizontal > (dotPositionHorizontal + countNewCellsHorizontal) && currentPositionVertical > minPositionVertical) {
                        var pos = currentPositionHorizontal
                        if (currentPositionVertical != positionVertical - 1) {
                            while (pos > (dotPositionHorizontal + countNewCellsHorizontal)) {
                                reversedData[currentPositionVertical][pos].depth.append(depth)
                                
                                childArray.append(reversedData[currentPositionVertical][pos].cellID)
                                pos -= 1
                            }
                        }
                        
                        
                        currentPositionHorizontal = currentPositionHorizontal-1
                        currentPositionVertical = currentPositionVertical-1
                    }
                } else {
                    
                    let minPositionHorizontal =
                    (dotPositionHorizontal + countNewCellsHorizontal) - countOfCell
                    let maxPositionHorizontal =
                    dotPositionHorizontal + countOfCell + countNewCellsHorizontal
                    
                    
                    let minPositionVertical = distance - countOfCell
                    let maxPositionVertical = distance + countOfCell
                    
                    var currentPositionVertical = maxPositionVertical
                    var currentPositionHorizontal = maxPositionHorizontal
                    
                    
                    currentPositionVertical = minPositionVertical
                    currentPositionHorizontal = minPositionHorizontal
                    
                    while (currentPositionVertical <= maxPositionVertical) {
                        while (currentPositionHorizontal <= maxPositionHorizontal) {
                            reversedData[currentPositionVertical][currentPositionHorizontal].depth.append(depth)
                            childArray.append(reversedData[currentPositionVertical][currentPositionHorizontal].cellID)
                            currentPositionHorizontal+=1
                        }
                        currentPositionHorizontal = minPositionHorizontal
                        currentPositionVertical+=1
                    }
                }
                
                reversedData[distance][(dotPositionHorizontal+countNewCellsHorizontal)].childCells.removeAll()
                reversedData[distance][(dotPositionHorizontal+countNewCellsHorizontal)].childCells.append(contentsOf: childArray)
                return reversedData
            } else {
                return nil
            }
        } else {
            return reversedData
        }
    }