Home > database >  How do I spot the smallest negative number using swift
How do I spot the smallest negative number using swift

Time:11-09

OK,

Having a mental block here... I got a range of vectors

node 0 SCNVector3(x: 8.208711e-08, y: 0.0, z: -1.9389672) 
node 1 SCNVector3(x: -0.93896717, y: 0.0, z: -1.0000001) 
node 2 SCNVector3(x: -8.208711e-08, y: 0.0, z: -0.06103283) 
node 3 SCNVector3(x: 0.93896717, y: 0.0, z: -0.99999994) 
node 4 SCNVector3(x: 0.0, y: 0.93896717, z: -1.0) 
node 5 SCNVector3(x: 0.0, y: -0.93896717, z: -1.0) 

And I want to select node2 since it has the smallest negative number. It's a stupid question, I know; I am sorry.

struct Sats {
    var theNode = SCNNode()
    var theIndex:Int = 7
}

if cubeNode != nil {
        cubeNode.enumerateChildNodes { (node, stop) in
            let search = /face(\d )/
            if let result = try? search.wholeMatch(in: node.name!) {
                print("node \(result.1) \(node.worldPosition) ")
                if node.worldPosition.z < -0.05 {
                    print("eureka \(theOne.theIndex)")
                    theOne.theNode = node
                    theOne.theIndex = Int(result.1)!
                }
            }
        }
    }

CodePudding user response:

If all your objects are in an array, you can use min(by:). Like a lot of Swift's sorting methods it takes a block with two values, and you return TRUE if the second one is larger than the first:

myVectors.min { $0.x < $1.x }

The returned value is an optional – it's either the smallest value in the array, or nil if the array was empty.

  • Related