18.Silver Challenge

In Line.swift:

struct Line {
    var begin = CGPoint.zero
    var end = CGPoint.zero

    var angle: Measurement<UnitAngle> {
        var angleInRads: Measurement<UnitAngle>
        angleInRads = Measurement(value: -atan2(Double(end.y - begin.y),Double(end.x - begin.x)), unit: .radians)
    
        return angleInRads.converted(to: .degrees)
    }
}

In DrawView.swift:

override func draw(_ rect: CGRect) {
    finishedLineColor.setStroke()
    for line in finishedLines {
        stroke(line)
    }
    
    // currentLineColor
    for (_, line) in currentLines {

        switch line.angle.value {
        case 0 ..< 90.0:
            UIColor.red.setStroke()
        case 90.0 ..< 180.0:
            UIColor.blue.setStroke()
        case -180.0 ..< -90.0:
            UIColor.yellow.setStroke()
        case -90.0 ..< 0:
            UIColor.green.setStroke()
        default:
            break
        }
        
        stroke(line)
    }
}

@Alex_Oliveira
A very nice use of the new Measurement class.

An alternative approach for getting a more broad spectrum of colours would be doing the following…

In DrawView.swift:

func color(startPoint from: CGPoint, endPoint end: CGPoint) -> UIColor {
    // Get the magnitude of rise and run
    let dx = end.x - from.x
    let dy = end.y - from.y
    
    // Get the angle in radians
    // Note: this function returns a range between 0 to Pi or 0 to -Pi
    var angle = -atan2(Double(dy), Double(dx))
    
    // Compensate for the 0 to -Pi range that's given by the atan2 function to
    // have a range that goes from 0 to 2 x Pi
    angle = (angle > 0) ? angle : (2 * M_PI) + angle
    // Convet the angle in degress to a fraction that will range between 0 to 1
    let percentAngle = angle / (2 * M_PI)
    
    // Create a color using the HSB scale as the hue is the component that controls the color range
    return UIColor(hue: CGFloat(percentAngle), saturation: 1, brightness: 1, alpha: 1)
}



override func draw(_ rect: CGRect) {

    finishedLineColor.setStroke()
    for line in finishedLines {
        stroke(line)
    }
    
    
    for (_, line) in currentLines {
        currentLineColor = color(startPoint: line.begin, endPoint: line.end)
        currentLineColor.setStroke()
        
        stroke(line)
    }
}

Ternary operator. Neat!
Thank you for reminding me of this tool.