数学公式的边界框逻辑的清洁方式?

时间:2016-07-13 08:32:58

标签: ios swift

下面我写了一些有点冗长的边界框逻辑。有没有办法让它更具可读性,所以支票看起来更干净,更简洁?

library(reshape2)
melt(foo)
#   value L1
#1 johnny  1
#2   joey  1
#3 deedee  3

1 个答案:

答案 0 :(得分:2)

试试这个:

extension ClosedInterval {
    func clamp(value : Bound) -> Bound {
        return self.start > value ? self.start
            : self.end < value ? self.end
            : value
    }
}

extension SCNVector3 {
    func clamp(min min:SCNVector3, max: SCNVector3) -> SCNVector3 {
        let x = (min.x...max.x).clamp(self.x)
        let y = (min.y...max.y).clamp(self.y)
        let z = (min.z...max.z).clamp(self.z)
        return SCNVector3(x, y, z)
    }
}

func validatePosition(position:SCNVector3, forNode node:SCNNode) -> SCNVector3 {
    var newPosition = position
    var maxVector = SCNVector3Zero
    var minVector = SCNVector3Zero

    let success = self.actionDelegate?.getBoundingBox(&minVector, max: &maxVector)
    guard success == true else {
        return newPosition
    }

    newPosition = position.clamp(min: minVector, max: maxVector)
    return newPosition
}