Skip to content

Instantly share code, notes, and snippets.

@tomasen
Created December 25, 2021 16:46
Show Gist options
  • Save tomasen/7d18fdb7a63fe769411c7197c921978d to your computer and use it in GitHub Desktop.
Save tomasen/7d18fdb7a63fe769411c7197c921978d to your computer and use it in GitHub Desktop.
find a place by brutal force for WordCloudView
func findSafePlace(for size: CGSize, avoid occupiedAreas: [CGRect]) -> CGRect {
// keep trying random places until it fit
for _ in 0...100 {
let randomRect = CGRect(origin: CGPoint(x: CGFloat.random(in: 0...canvasSize.width),
y: CGFloat.random(in: 0...canvasSize.height)),
size: size)
var collision = false
for rect in occupiedAreas {
if rect.intersects(randomRect) {
collision = true
break
}
}
if !collision {
return randomRect
}
}
return CGRect.zero
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment