Created
December 25, 2021 16:46
-
-
Save tomasen/7d18fdb7a63fe769411c7197c921978d to your computer and use it in GitHub Desktop.
find a place by brutal force for WordCloudView
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
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