<commit_before>//
//  GOLineSegment.swift
//  GridOptic
//
//  Created by NULL on 30/03/15.
//  Copyright (c) 2015 Echx. All rights reserved.
//

import UIKit

class GOLineSegment: GOSegment {
    var center: GOCoordinate
    var length: NSInteger
    
    //angle should be within [0, PI) from
    var direction: CGVector
    
    var line: GOLine {
        get {
            return GOLine(anyPoint: CGPointMake(CGFloat(self.center.x), CGFloat(self.center.y)), direction: self.direction)
        }
    }
    
    init(center: GOCoordinate, length: NSInteger, direction: CGVector) {
        self.center = center;
        self.length = length;
        if direction.dy <= 0 {
            self.direction = CGVectorMake(-direction.dx, -direction.dy)
        } else  {
            self.direction = direction
        }
    }
    
    var directionInRadianFromXPlus: CGFloat {
        get {
            return self.direction.angleFromXPlus
        }
    }
    
    var startPoint: CGPoint {
        get {
            let radDirection = self.directionInRadianFromXPlus
            let deltaX = -0.5 * CGFloat(self.length) * sin(radDirection)
            let deltaY = -0.5 * CGFloat(self.length) * cos(radDirection)
            return CGPointMake(CGFloat(center.x) + deltaX, CGFloat(center.y) + deltaY)
        }
    }
    
    var endPoint: CGPoint {
        get {
            let radDirection = self.directionInRadianFromXPlus
            let deltaX = 0.5 * CGFloat(self.length) * sin(radDirection)
            let deltaY = 0.5 * CGFloat(self.length) * cos(radDirection)
            return CGPointMake(CGFloat(center.x) + deltaX, CGFloat(center.y) + deltaY)
        }
    }
    
    override func getIntersactionPoint(ray: GORay) -> CGPoint? {
        if let lineIntersaction = GOLine.getIntersaction(line1: self.line, line2: ray.line) {
            let start = self.startPoint
            let end = self.endPoint
            
            //get the left and right most x of this line segment
            let leftX = start.x < end.x ? start.x : end.x
            let rightX = start.x < end.x ? end.x : start.x
            
            //if the intersaction point is not within [leftX, rightX], then there is no intersaction point
            if lineIntersaction.x < leftX || lineIntersaction.x > rightX {
                return nil
            } else {
                return lineIntersaction
            }
        }
        
        return nil
    }
}<commit_msg>get reflection (unfinished)<commit_after>//
//  GOLineSegment.swift
//  GridOptic
//
//  Created by NULL on 30/03/15.
//  Copyright (c) 2015 Echx. All rights reserved.
//

import UIKit

class GOLineSegment: GOSegment {
    var center: GOCoordinate
    var length: NSInteger
    
    //angle should be within [0, PI) from
    var direction: CGVector
    
    var line: GOLine {
        get {
            return GOLine(anyPoint: CGPointMake(CGFloat(self.center.x), CGFloat(self.center.y)), direction: self.direction)
        }
    }
    
    init(center: GOCoordinate, length: NSInteger, direction: CGVector) {
        self.center = center;
        self.length = length;
        if direction.dy <= 0 {
            self.direction = CGVectorMake(-direction.dx, -direction.dy)
        } else  {
            self.direction = direction
        }
    }
    
    var directionInRadianFromXPlus: CGFloat {
        get {
            return self.direction.angleFromXPlus
        }
    }
    
    var startPoint: CGPoint {
        get {
            let radDirection = self.directionInRadianFromXPlus
            let deltaX = -0.5 * CGFloat(self.length) * sin(radDirection)
            let deltaY = -0.5 * CGFloat(self.length) * cos(radDirection)
            return CGPointMake(CGFloat(center.x) + deltaX, CGFloat(center.y) + deltaY)
        }
    }
    
    var endPoint: CGPoint {
        get {
            let radDirection = self.directionInRadianFromXPlus
            let deltaX = 0.5 * CGFloat(self.length) * sin(radDirection)
            let deltaY = 0.5 * CGFloat(self.length) * cos(radDirection)
            return CGPointMake(CGFloat(center.x) + deltaX, CGFloat(center.y) + deltaY)
        }
    }
    
    override func getIntersactionPoint(ray: GORay) -> CGPoint? {
        if let lineIntersaction = GOLine.getIntersaction(line1: self.line, line2: ray.line) {
            let start = self.startPoint
            let end = self.endPoint
            
            //get the left and right most x of this line segment
            let leftX = start.x < end.x ? start.x : end.x
            let rightX = start.x < end.x ? end.x : start.x
            
            //if the intersaction point is not within [leftX, rightX], then there is no intersaction point
            if lineIntersaction.x < leftX || lineIntersaction.x > rightX {
                return nil
            } else {
                return lineIntersaction
            }
        }
        
        return nil
    }
    
    func getRefelctionRay(ray: GORay) -> GORay? {
        if self.isIntersacedWithRay(ray) {
            // calculate the ray
            let product = ray.direction.dx * self.direction.dx +
                            ray.direction.dy * self.direction.dy
            
        } else {
            return nil
        }
        return nil
    }
    
}
{'fraction_non_alphanumeric': 0.09261363636363637, 'fraction_numerical': 0.008333333333333333, 'mean_word_length': 1.7165637860082306, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '373653', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1597, 'n_words': 388}