Created
March 23, 2022 05:58
-
-
Save sordina/f90aba67df46ddddaab1ac263f07eefc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import SwiftUI | |
import PlaygroundSupport | |
struct Foo : View { | |
var body: some View { | |
Circle() | |
.fill(.green) | |
.frame(width: 200, height: 200, alignment: .center) | |
} | |
} | |
struct Point : Hashable { | |
let x : Int | |
let y : Int | |
var tuple: (Int, Int) { | |
return (self.x, self.y) | |
} | |
} | |
struct Connection : Hashable { | |
let a : Point | |
let b : Point | |
} | |
var queue : [Point] = [Point(x:0, y:0)] | |
var connections : Dictionary<Point, Set<Point>> = [:] | |
var history : Set<Point> = [] | |
func pop() -> Point { | |
let e = queue[0] | |
queue.remove(at: 0) | |
return e | |
} | |
func push(_ n : Point) { | |
queue.append(n) | |
} | |
func push2(_ n : Point) { | |
queue.insert(n, at: 0) | |
} | |
func connect(_ a : Point, _ b : Point) { | |
connections[a, default: []].insert(b) | |
connections[b, default: []].insert(a) | |
} | |
func connectivity(_ a : Point) -> Int { | |
connections[a, default: []].count | |
} | |
func adjacent(_ p : Point) -> [Point] { | |
let (x, y) = p.tuple | |
return [ | |
(x+1,y), | |
(x-1,y), | |
(x,y+1), | |
(x,y-1), | |
] | |
.map {(i,j) in Point(x:i,y:j)} | |
.filter { q in q.x > -10 && q.y > -10 && q.x < 10 && q.y < 10 } | |
} | |
func shuf<T>(_ x : Array<T>) -> Array<T> { | |
if (1...10).randomElement() ?? 0 > 9 { | |
return x | |
} else { | |
return x.shuffled() | |
} | |
} | |
for _ in 1...600 { | |
let e = pop() | |
history.insert(e) | |
for n in shuf(adjacent(e)) { // }.shuffled() { | |
if connectivity(n) < 1 { | |
connect(e, n) | |
} | |
if !history.contains(n) { | |
push2(n) | |
} | |
} | |
} | |
var cvs = Canvas { context, size in | |
let st = StrokeStyle(lineWidth: 10, lineCap: .round, lineJoin: .round) | |
for (k, vs) in connections { | |
for v in vs { | |
var p = Path() | |
let kx = 20 * CGFloat(k.x)+size.width/2.0 | |
let ky = 20 * CGFloat(k.y)+size.height/2.0 | |
let vx = 20 * CGFloat(v.x)+size.width/2.0 | |
let vy = 20 * CGFloat(v.y)+size.height/2.0 | |
p.move(to: CGPoint(x: kx, y: ky)) | |
p.addLine(to: CGPoint(x: vx, y: vy)) | |
context.stroke(p, with: .color(.brown), style: st ) | |
} | |
} | |
} | |
.frame(width: 400, height: 400) | |
PlaygroundPage.current.setLiveView(cvs) | |
Author
sordina
commented
Mar 23, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment