Repository: danielgindi/Charts
File: Source/Charts/Charts/PieRadarChartViewBase.swift (Swift)

Code before:
        }
        
        if _isRotating
        {
            _isRotating = false
        }
    }
    #endif
    
    private func resetVelocity()
    {
        velocitySamples.removeAll(keepingCapacity: false)
    }
    
    private func sampleVelocity(touchLocation: CGPoint)
    {
        let currentSample: AngularVelocitySample = {
            let time = CACurrentMediaTime()
            let angle = angleForPoint(x: touchLocation.x, y: touchLocation.y)
            return AngularVelocitySample(time: time, angle: angle)
        }()

        // Remove samples older than our sample time - 1 seconds
        // while keeping at least one samples
        let index = velocitySamples
            .dropLast()
            .lastIndex { $0.time < currentSample.time - 1 }
        if let index = index {
            velocitySamples.remove(at: index)
        }
        velocitySamples.append(currentSample)
    }

    private func calculateVelocity() -> CGFloat
    {
        guard var firstSample = velocitySamples.first,
            var lastSample = velocitySamples.last
            else { return 0 }

        // Look for a sample that's closest to the latest sample, but not the same, so we can deduce the direction
        let beforeLastSample = velocitySamples.last { $0.angle != lastSample.angle }
            ?? firstSample

        // Calculate the sampling time
        let timeDelta: CGFloat = {
            let delta = CGFloat(lastSample.time - firstSample.time)
            return delta == 0 ? 0.1 : delta
        }()

        // Calculate clockwise/ccw by choosing two values that should be closest to each other,
        // so if the angles are two far from each other we know they are inverted "for sure"

Review comment (suggestion):
I'm not sure what I was doing above. 
```
let index = velocitySamples.firstIndex { currentSample.time - velocitySamples[i].time > 1.0 }
    ?? velocitySamples.reversed.firstIndex
velocitySamples = velocitySamples.suffix(from: index)
```
Should achieve the same thing without writing a custom algorithm

Code after:
        }
        
        if _isRotating
        {
            _isRotating = false
        }
    }
    #endif
    
    private func resetVelocity()
    {
        velocitySamples.removeAll(keepingCapacity: false)
    }
    
    private func sampleVelocity(touchLocation: CGPoint)
    {
        let currentSample: AngularVelocitySample = {
            let time = CACurrentMediaTime()
            let angle = angleForPoint(x: touchLocation.x, y: touchLocation.y)
            return AngularVelocitySample(time: time, angle: angle)
        }()

        // Remove samples older than our sample time - 1 seconds
        // while keeping at least one sample
        
        var i = 0, count = velocitySamples.count
        while (i < count - 2)
        {
            if currentSample.time - velocitySamples[i].time > 1.0
            {
                velocitySamples.remove(at: 0)
                i -= 1
                count -= 1
            }
            else
            {
                break
            }

            i += 1
        }

        velocitySamples.append(currentSample)
    }

    private func calculateVelocity() -> CGFloat
    {
        guard var firstSample = velocitySamples.first,
            var lastSample = velocitySamples.last
            else { return 0 }