Created
May 5, 2017 15:13
-
-
Save tikipatel/1fe4e657b8acfd2ead298beed9ead300 to your computer and use it in GitHub Desktop.
Sierpinski_Chaos
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
//: Playground - noun: a place where people can play | |
import UIKit | |
import Foundation | |
import PlaygroundSupport | |
let canvasWidth: CGFloat = 500.0 | |
let canvasHeight: CGFloat = 500.0 | |
let padding: CGFloat = 10.0 | |
let pointA: CGPoint = CGPoint(x: canvasWidth / 2.0, y: padding) | |
let pointB: CGPoint = CGPoint(x: padding, y: canvasHeight - padding) | |
let pointC: CGPoint = CGPoint(x: canvasWidth - padding, y: canvasHeight - padding) | |
// Create Live View | |
let canvasView = UIView(frame: CGRect(x: 0, y: 0, width: canvasWidth, height: canvasHeight)) | |
canvasView.backgroundColor = UIColor.black | |
PlaygroundPage.current.liveView = canvasView | |
PlaygroundPage.current.needsIndefiniteExecution = true | |
class ChoaticPoints { | |
private let chaosQueue = DispatchQueue(label: "chaos-queue") | |
// Vertex properties | |
private let vertexColor: UIColor = UIColor.red | |
private let vertexSize: CGSize = CGSize(width: 5, height: 5) | |
// Point properties | |
private let pointSize: CGSize = CGSize(width: 1.0, height: 1.0) | |
private var currentPosition: CGPoint = .zero | |
private func randomPoint() -> CGPoint { | |
let randomX: CGFloat = CGFloat(drand48()) * canvas.frame.size.width | |
let randomY: CGFloat = CGFloat(drand48()) * canvas.frame.size.height | |
return CGPoint(x: randomX, y: randomY) | |
} | |
var verticies: [CGPoint] | |
var canvas: UIView | |
init(canvas: UIView, boundaryPoints: [CGPoint]) { | |
self.canvas = canvas | |
self.verticies = boundaryPoints | |
} | |
private func randomVertex() -> CGPoint { | |
let randomIndex: Int = Int(arc4random_uniform(UInt32(verticies.count))) | |
return verticies[randomIndex] | |
} | |
func go() { | |
for vertex in verticies { | |
let vertex = UIView(frame: CGRect(origin: vertex, size: vertexSize)) | |
vertex.backgroundColor = .red | |
canvas.addSubview(vertex) | |
} | |
func move(fromPoint: CGPoint, toPoint: CGPoint) -> CGPoint { | |
let newX = (toPoint.x + fromPoint.x) / 2.0 | |
let newY = (toPoint.y + fromPoint.y) / 2.0 | |
return CGPoint(x: newX, y: newY) | |
} | |
let initialMove = UIView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 5, height: 5))) | |
initialMove.center = canvas.center | |
initialMove.backgroundColor = .blue | |
currentPosition = initialMove.center | |
canvasView.addSubview(initialMove) | |
chaosQueue.async { | |
for _ in 1...5000000 { | |
let newView = UIView(frame: CGRect(origin: .zero, size: self.pointSize)) | |
newView.backgroundColor = .white | |
newView.center = move(fromPoint: self.currentPosition, toPoint: self.randomVertex()) | |
DispatchQueue.main.async { | |
self.canvas.addSubview(newView) | |
} | |
self.currentPosition = newView.center | |
} | |
} | |
} | |
} | |
let choaticTriangle = ChoaticPoints(canvas: canvasView, boundaryPoints: [pointA, pointB, pointC]) | |
choaticTriangle.go() | |
// Doesn't work... need to figure out why... | |
/* | |
let squareA = CGPoint(x: padding, y: padding) | |
let squareB = CGPoint(x: canvasWidth - padding, y: padding) | |
let squareC = CGPoint(x: canvasWidth - padding, y: canvasHeight - padding) | |
let squareD = CGPoint(x: padding, y: canvasHeight - padding) | |
let chaoticSqure = ChoaticPoints(canvas: canvasView, boundaryPoints: [squareA, squareB, squareC, squareD]) | |
chaoticSqure.go() | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment