iosswiftcgpoint

What libraries do we need to include to use CGPoint and arc4random()?


It's my first program in Swift. The function returns two random locations (CGPoints) which are points on the perimeter of a circle of radius = variable (passed in by the calling program). The two CGPoints must also be greater than degreesApart around the perimeter of the circle. I have written the following code:

 func returnPerimeterPoints (radius: Int, degreesApart: Int) -> (CGPoint, CGPoint) {
    var perimeterPoint1 = CGPoint(x:Int(arc4random()%radius),y:Int(arc4random()%radius))
    var perimeterPoint2 = CGPoint(x:Int(arc4random()%radius),y:Int(arc4random()%radius))
return (perimeterPoint1, perimeterPoint2)
    }
print(returnPerimeterPoints(20, 24))

I have received the following errors:

cannot find 'CGPoint' in scope
cannot find 'arc4random' in scope

What libraries do i need to include and what can i do to get the CGPoints like i mentioned in the first paragraph?


Solution

  • CGPoint is defined in CoreGraphics, but will also be imported by default when importing a UI library like UIKit or SwiftUI.

    arc4random in part of Darwin (the kernel) but, again, will be imported automatically when you import an Apple framework that depends on it, like Foundation or even CoreGraphics as well.

    import CoreGraphics
    
    // ... your code here