Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 包围盒逻辑清洁方式的数学公式?_Ios_Swift - Fatal编程技术网

Ios 包围盒逻辑清洁方式的数学公式?

Ios 包围盒逻辑清洁方式的数学公式?,ios,swift,Ios,Swift,下面我写了一些有点冗长的边界框逻辑。有没有办法使它更具可读性,使检查看起来更干净、更简洁 func validatePosition(position:SCNVector3, forNode node:SCNNode) -> SCNVector3 { var newPosition = position var maxVector = SCNVector3Zero var minVector = SCNVector3Zero let success = s

下面我写了一些有点冗长的边界框逻辑。有没有办法使它更具可读性,使检查看起来更干净、更简洁

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
    }

    if newPosition.x < minVector.x && newPosition.x < 0 {
        newPosition.x = minVector.x
    }

    if newPosition.y < minVector.y && newPosition.y < 0 {
        newPosition.y = minVector.y
    }

    if newPosition.z < minVector.z && newPosition.z < 0 {
        newPosition.z = minVector.z
    }

    if newPosition.x > maxVector.x && newPosition.x > 0 {
        newPosition.x = maxVector.x
    }

    if newPosition.y > maxVector.y && newPosition.y > 0 {
        newPosition.y = maxVector.y
    }

    if newPosition.z > maxVector.z && newPosition.z > 0 {
        newPosition.z = maxVector.z
    }

    return newPosition
}
func validatePosition(位置:SCInvector3,forNode:SCNNode)->SCInvector3{
var newPosition=位置
var maxVector=SCInvector3Zero
var minVector=SCInvector3zero
让success=self.actionDelegate?.getBoundingBox(&minVector,max:&maxVector)
guard success==true else{
返回新位置
}
如果newPosition.xmaxVector.x&&newPosition.x>0{
newPosition.x=maxVector.x
}
如果newPosition.y>maxVector.y&&newPosition.y>0{
newPosition.y=maxVector.y
}
如果newPosition.z>maxVector.z&&newPosition.z>0{
newPosition.z=maxVector.z
}
返回新位置
}
试试这个:

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
}
extension ClosedInterval{
函数钳位(值:绑定)->绑定{
返回self.start>value?self.start
:self.end<值?self.end
:价值
}
}
扩展转换程序3{
功能钳位(最小值:SCInvector3,最大值:SCInvector3)->SCInvector3{
设x=(最小x…最大x).夹具(self.x)
设y=(最小y…最大y).夹具(self.y)
设z=(最小值z…最大值z).夹具(self.z)
返回SCInvector3(x,y,z)
}
}
func validatePosition(位置:SCInvector3,forNode节点:SCNNode)->SCInvector3{
var newPosition=位置
var maxVector=SCInvector3Zero
var minVector=SCInvector3zero
让success=self.actionDelegate?.getBoundingBox(&minVector,max:&maxVector)
guard success==true else{
返回新位置
}
newPosition=位置.钳位(最小:最小向量,最大:最大向量)
返回新位置
}
试试这个:

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
}
extension ClosedInterval{
函数钳位(值:绑定)->绑定{
返回self.start>value?self.start
:self.end<值?self.end
:价值
}
}
扩展转换程序3{
功能钳位(最小值:SCInvector3,最大值:SCInvector3)->SCInvector3{
设x=(最小x…最大x).夹具(self.x)
设y=(最小y…最大y).夹具(self.y)
设z=(最小值z…最大值z).夹具(self.z)
返回SCInvector3(x,y,z)
}
}
func validatePosition(位置:SCInvector3,forNode节点:SCNNode)->SCInvector3{
var newPosition=位置
var maxVector=SCInvector3Zero
var minVector=SCInvector3zero
让success=self.actionDelegate?.getBoundingBox(&minVector,max:&maxVector)
guard success==true else{
返回新位置
}
newPosition=位置.钳位(最小:最小向量,最大:最大向量)
返回新位置
}