Created
August 26, 2014 23:32
-
-
Save cruffenach/8d07e6b50dfdeb255e07 to your computer and use it in GitHub Desktop.
LogoFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LogoPathFactory.swift | |
// LogoFunhouse_Swift | |
// | |
// Created by Collin Ruffenach on 8/25/14. | |
// Copyright (c) 2014 Simple. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
import Surge | |
struct Logo { | |
var iterations : Int | |
var resolution : Int | |
var amplitude : Double | |
var phase : Double | |
var frequency : Double | |
var colors : [UIColor] | |
func points() -> [CGPoint] { | |
//for(int i = 1; i < resolution+1; i++) | |
//x = (0.5+0.5*amplitude*sinf(phase+frequency*(i*resolutionIncrement)))*cosf(i*resolutionIncrement); | |
//y = (0.5+0.5*amplitude*sinf(phase+frequency*(i*resolutionIncrement)))*sinf(i*resolutionIncrement); | |
var sketchpad = [Double](count: self.resolution, repeatedValue: 0.0) | |
let resolutionIncrement = Double(2.0*M_PI)/Double(self.resolution) | |
for i in 1 ..< self.resolution { | |
sketchpad[i] = Double(i) * resolutionIncrement | |
} | |
let resolutionIncrementSinCos = sincos(sketchpad) | |
sketchpad = mul(sketchpad, [Double](count: self.resolution, repeatedValue: self.frequency)) | |
sketchpad = add(sketchpad, [Double](count: self.resolution, repeatedValue: self.phase)) | |
sketchpad = sin(sketchpad) | |
sketchpad = mul(sketchpad, [Double](count: self.resolution, repeatedValue: 0.5*self.amplitude)) | |
sketchpad = add(sketchpad, [Double](count: self.resolution, repeatedValue: 0.5)) | |
let x = mul(resolutionIncrementSinCos.cos, sketchpad) | |
let y = mul(resolutionIncrementSinCos.sin, sketchpad) | |
var points = [CGPoint]() | |
for i in 0..<x.count { | |
var point = CGPoint(x: x[i], y: y[i]) | |
points.append(point) | |
} | |
return points | |
} | |
func pathPoints() -> [[CGPoint]] { | |
let phaseIncrement = Double(2.0*M_PI)/Double(self.iterations); | |
var shapes = [[CGPoint]]() | |
for i in 1...self.iterations { | |
shapes.append( | |
Logo(iterations: self.iterations, | |
resolution: self.resolution, | |
amplitude: self.amplitude, | |
phase: Double(i)*phaseIncrement, | |
frequency: self.frequency, | |
colors: [UIColor.redColor()] | |
).points() | |
) | |
} | |
return shapes | |
} | |
func paths(frame : CGRect) -> [UIBezierPath] { | |
var shapes = pathPoints() | |
var x = shapes.map({$0.map({$0.x})}).reduce([CGFloat](), combine: {$0 + $1}) | |
var y = shapes.map({$0.map({$0.y})}).reduce([CGFloat](), combine: {$0 + $1}) | |
var minPoint = CGPoint(x: x.reduce(CGFloat.max, combine: {min($0, $1)}), y: y.reduce(CGFloat.max, combine: {min($0, $1)})) | |
var maxPoint = CGPoint(x: x.reduce(CGFloat.min, combine: {max($0, $1)}), y: y.reduce(CGFloat.min, combine: {max($0, $1)})) | |
let xScaler = maxPoint.x - minPoint.x | |
let yScaler = maxPoint.y - minPoint.y | |
var normalizedShapes = shapes.map({$0.map({CGPoint(x: $0.x/xScaler, y: $0.y/yScaler)})}) | |
var paths = [UIBezierPath]() | |
for points in normalizedShapes { | |
var path = UIBezierPath() | |
for (index, point) in enumerate(points) { | |
var scaledPoint = CGPoint(x: point.x * CGRectGetWidth(frame), | |
y: point.y * CGRectGetHeight(frame)) | |
scaledPoint = CGPoint(x: scaledPoint.x + CGRectGetWidth(frame)/2.0, | |
y: scaledPoint.y + CGRectGetHeight(frame)/2.0) | |
if (index == 0) { | |
path.moveToPoint(scaledPoint) | |
} else { | |
path.addLineToPoint(scaledPoint) | |
} | |
} | |
path.closePath() | |
paths.append(path) | |
} | |
return paths | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment