<commit_before>//
//  CircularScrollView.swift
//  RouletteTableView
//
//  Created by <NAME> on 2016. 4. 19..
//  Copyright © 2016년 <NAME>. All rights reserved.
//

import UIKit

class CircularScrollView: RotatableView {
    override init(frame: CGRect) {
        self.contentView = PolarView(radius: 0, angle: 0, frame: frame)
        self.contentView.layer.borderColor = UIColor.yellowColor().CGColor
        super.init(frame: frame)
        super.addSubview(self.contentView)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func addSubview(view: UIView) { }

    private var contentView: PolarView
    var contentOffset: Double {
        get {
            return -1 * self.offset
        }
        set {
            self.offset = -1 * newValue
        }
    }

    var polarCoordinatedSubviews: [PolarCoordinated] = []

    func addSubview(polarCoordinated view: PolarCoordinated) {
        if !self.polarCoordinatedSubviews.contains(view) {
            self.polarCoordinatedSubviews.append(view)
        }
        self.setNeedsLayout()
    }

    override func layoutSubviews() {
        self.contentView.frame.size = self.frame.size
        for subview in self.subviewsToShow {
            self.contentView.addSubview(subview)
        }
        for subview in self.contentView.subviews {
            if let polar = subview as? PolarCoordinated where !self.subviewsToShow.contains(polar){
                polar.removeFromSuperview()
            }
        }
        super.layoutSubviews()
    }

    var subviewsToShow: [PolarCoordinated] {
        let lower = self.contentOffset
        let upper = self.contentOffset + M_PI * 2
        return self.polarCoordinatedSubviews.filter { (p) -> Bool in
            return lower <= p.angle && p.angle < upper
        }
    }<commit_msg>Dragging<commit_after>//
//  CircularScrollView.swift
//  RouletteTableView
//
//  Created by <NAME> on 2016. 4. 19..
//  Copyright © 2016년 <NAME>. All rights reserved.
//

import UIKit

func - (a: CGPoint, b: CGPoint) -> CGPoint {
    return CGPointMake(a.x - b.x, a.y - b.y)
}

func distance(point: CGPoint) -> CGFloat {
    return sqrt(pow(point.x, 2) + pow(point.y, 2))
}

func getAngle(a a: CGPoint, b: CGPoint) -> Double {
    let distA = distance(a)
    let distB = distance(b)
    if distA == 0 || distB == 0 {
        return 0.0
    }

    var cosTheta = (a.x*b.x + a.y*b.y) / (distA * distB)
    // acos() returns NaN for |x| > 1.
    // Theoretically, |cosTheta| <= 1 but due to lack of floating point precision,
    // cosTheta could be slightly bigger or smaller than the range like 1.0000000000000002
    if cosTheta > 1 {
        cosTheta = 1.0
    } else if cosTheta < -1 {
        cosTheta = -1.0
    }
    let rad = acos(cosTheta)

    if a.x*b.y - a.y*b.x < 0 {
        return -rad.native
    }
    return rad.native
}

class CircularScrollView: RotatableView {
    override init(frame: CGRect) {
        self.contentView = PolarView(radius: 0, angle: 0, frame: frame)
        self.contentView.layer.borderColor = UIColor.yellowColor().CGColor
        super.init(frame: frame)
        super.addSubview(self.contentView)

        let draggingGestureRecognizer = UIPanGestureRecognizer(target: self, action: "dragging:")
        self.addGestureRecognizer(draggingGestureRecognizer)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private var contentView: PolarView
    var contentOffset: Double {
        get {
            return -1 * self.offset
        }
        set {
            self.offset = -1 * newValue
        }
    }

    var subviewsToShow: [PolarCoordinated] {
        let lower = self.contentOffset
        let upper = self.contentOffset + M_PI * 2
        return self.polarCoordinatedSubviews.filter { (p) -> Bool in
            return lower <= p.angle && p.angle < upper
        }
    }

    var polarCoordinatedSubviews: [PolarCoordinated] = []

    var formerPoint: CGPoint!
    func dragging(recognizer: UIPanGestureRecognizer) {
        switch recognizer.state {
        case .Began:
            self.formerPoint = recognizer.locationInView(self)
        case .Changed:
            let v1 = self.formerPoint - self.center
            let v2 = recognizer.locationInView(self) - self.center
            self.contentOffset += getAngle(a: v2, b: v1)
            self.formerPoint = recognizer.locationInView(self)
        case .Ended:
            self.formerPoint = nil
        case .Cancelled:
            self.formerPoint = nil
        default:
            break
        }
    }

    override func addSubview(view: UIView) { }

    func addSubview(polarCoordinated view: PolarCoordinated) {
        if !self.polarCoordinatedSubviews.contains(view) {
            self.polarCoordinatedSubviews.append(view)
        }
        self.setNeedsLayout()
    }

    override func layoutSubviews() {
        self.contentView.frame.size = self.frame.size
        for subview in self.subviewsToShow {
            self.contentView.addSubview(subview)
        }
        for subview in self.contentView.subviews {
            if let polar = subview as? PolarCoordinated where !self.subviewsToShow.contains(polar){
                polar.removeFromSuperview()
            }
        }
        super.layoutSubviews()
    }

{'fraction_non_alphanumeric': 0.10163507989594946, 'fraction_numerical': 0.0126347082868822, 'mean_word_length': 2.157184750733138, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5439159', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1623, 'n_words': 385}