HomeiOS Developmentios - Dragon Curve fractal sample utilizing Swift?

ios – Dragon Curve fractal sample utilizing Swift?


I’ve this view of the Dragon Curve fractal sample utilizing Swift, however I’ve one problem not proven within the simulator. I’m not certain what went improper. Can somebody assist me to repair it?
sidenote: I exploit this webpage to put in writing my logic
https://rosettacode.org/wiki/Dragon_curve

enter image description here

class DragonCurveView: UIView {
    override func draw(_ rect: CGRect) {
        guard let context = UIGraphicsGetCurrentContext() else {
            return
        }
        
        let startPoint = CGPoint(x: rect.midX, y: rect.midY)
        let scale: CGFloat = 10.0
        let iterations = 12
        
        let factors = generateDragonCurve(iterations: iterations)
        let scaledPoints = scalePoints(factors, scale: scale)
        let translatedPoints = translatePoints(scaledPoints, startPoint: startPoint)
        
        drawDragonCurve(factors: translatedPoints, context: context)
    }
    
    non-public func generateDragonCurve(iterations: Int) -> [CGPoint] {
        var factors = [CGPoint(x: 0, y: 0)]
        
        for _ in 0..<iterations {
            let mirroredPoints = factors.reversed()
            let transformedPoints = mirroredPoints.map { CGPoint(x: -$0.y, y: $0.x) }
            factors.append(contentsOf: transformedPoints)
        }
        
        return factors
    }
    
    non-public func scalePoints(_ factors: [CGPoint], scale: CGFloat) -> [CGPoint] {
        return factors.map { CGPoint(x: $0.x * scale, y: $0.y * scale) }
    }
    
    non-public func translatePoints(_ factors: [CGPoint], startPoint: CGPoint) -> [CGPoint] {
        let xOffset = startPoint.x - factors.first!.x
        let yOffset = startPoint.y - factors.first!.y
        
        return factors.map { CGPoint(x: $0.x + xOffset, y: $0.y + yOffset) }
    }
    
    non-public func drawDragonCurve(factors: [CGPoint], context: CGContext) {
        guard let firstPoint = factors.first else {
            return
        }
        
        context.transfer(to: firstPoint)
        
        for i in 1..<factors.rely {
            let level = factors[i]
            context.addLine(to: level)
        }
        
        context.setStrokeColor(UIColor.white.cgColor)
        context.strokePath()
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments