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

Code before:
            let location = self.convert(theEvent.locationInWindow, from: nil)
            
            processRotationGestureEnded(location: location)
        }
        
        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
        _velocitySamples.removeAll { $0.time < currentSample.time - 1 }
        _velocitySamples.append(currentSample)
    }

    private func calculateVelocity() -> CGFloat
    {
        guard !_velocitySamples.isEmpty else { return 0 }

        var firstSample = _velocitySamples[0]
        var lastSample = _velocitySamples[_velocitySamples.endIndex - 1]
        
        // 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 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"
        let isClockwise: Bool = {
            let cw = lastSample.angle >= beforeLastSample.angle
            let isInverted = abs(lastSample.angle - beforeLastSample.angle) > 270.0
            return isInverted ? !cw : cw
        }()

        // Calculate the sampling time
        let timeDelta: CGFloat = {
            let d = lastSample.time - firstSample.time

Review comment (suggestion):
it's a bit obscure to say this change is equivalent. can you explain a little?
The main difference here is `while (i < count - 2)` so it seems the last one is not changed. but with the new change it will remove the last one if `$0.time < currentSample.time - 1`

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 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"
        let isClockwise: Bool = {