Created
January 27, 2016 09:09
-
-
Save andr-ggn/1c5cef1de827de269cd5 to your computer and use it in GitHub Desktop.
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
/// Start | |
func drawCircles(count: Int) { | |
var cX = [CGFloat!](count: count, repeatedValue: nil) | |
var cY = [CGFloat!](count: count, repeatedValue: nil) | |
var i: Int = 0 | |
var j: Int = 0 | |
var n = count | |
var cR: CGFloat = 45 | |
var dMinQ = pow(cR * 2, 2) | |
var distanciaQ: CGFloat = 0 | |
var dX: CGFloat = 0 | |
var dY: CGFloat = 0 | |
var ocurrencias = 0 | |
var cvW = radarContainer.frame.size.width | |
var cvH = radarContainer.frame.size.height | |
func createBall(cX: CGFloat, cY: CGFloat, cR: CGFloat, n: Int, ctx: UIView) { | |
let circle = NearbyUserView(frame: CGRect(x: cX, y: cY, width: cR * 2, height: cR * 2)) | |
// circle.backgroundColor = UIColor.blackColor() | |
ctx.addSubview(circle) | |
if n < latestCopyNearbyUsersArray.count { | |
C.In(n) | |
C.In(latestCopyNearbyUsersArray.count) | |
circle.imageURL = NSURL(string: latestCopyNearbyUsersArray[n].profileImageURL)! | |
circle.userFirstName = latestCopyNearbyUsersArray[n].firstName | |
circle.userName = latestCopyNearbyUsersArray[n].userName | |
circle.selectedIndexTag = n | |
circle.delegate = self | |
sequenceaAvatarViewsArray.append(circle) | |
avatarFeedViewsArray.append(NearbyUserFeedView()) | |
circle.alpha = 0 | |
UIView.animateWithDuration(Double(arc4random_uniform(UInt32(20))) / 10.0, animations: { () -> Void in | |
circle.alpha = 1 | |
}) | |
} else { | |
circle.userInteractionEnabled = false | |
circle.alpha = 1 /// Dummy! | |
circle.backgroundColor = UIColor.redColor() | |
} | |
userFeedCollectionView.reloadData() | |
} | |
func generateCenters(n: Int, cR: CGFloat, canvasW: CGFloat, canvasH: CGFloat) { | |
for (var i = 0; i < n; i++) { | |
cX[i] = round(((0.0...1.0).random() * (canvasW - cR * 2))) //+ cR; | |
cY[i] = round(((0.0...1.0).random() * (canvasH - cR * 2))) //+ cR; | |
if (i > 0) { | |
measureDistance(i); | |
} | |
print("P \(i) (\(cX[i]), \(cY[i])") | |
} | |
displayBalls(n); | |
} | |
func measureDistance(n: Int) { | |
for (i = 0; i < n; i++) { | |
dX = cX[n] - cX[i] | |
dY = cY[n] - cY[i] | |
distanciaQ = round(pow(dX, 2) + pow(dY, 2)) | |
//console.log("p"+n+" a p"+i+"="+Math.round(Math.pow(distanciaQ,0.5))); | |
if (distanciaQ < dMinQ) { | |
print("finding next...") | |
cX[n] = round(((0.0...1.0).random() * (cvW - CGFloat(cR * 2)))) //+ cR; | |
cY[n] = round(((0.0...1.0).random() * (cvH - CGFloat(cR * 2)))) //+ cR; | |
i = 0; | |
dX=cX[n]-cX[i]; | |
dY=cY[n]-cY[i]; | |
distanciaQ = round(pow(dX, 2) + pow(dY, 2)) | |
ocurrencias = ocurrencias + 1; | |
i = i - 1 | |
} | |
} | |
} | |
func displayBalls(n: Int) { | |
let sR: CGFloat = 0 | |
// var change = cR - sR | |
// sR = sR + ceil((change / 2)) | |
for (j=0; j<n; j++) { | |
var eR: CGFloat = sR + (cR / CGFloat(n)) * CGFloat(j) | |
if eR > cR { eR = cR } | |
createBall(cX[j], cY: cY[j], cR: cR /*sR*/, n: j, ctx: radarContainer) | |
} | |
} | |
generateCenters(n, cR: cR, canvasW: cvW, canvasH: cvH); | |
} | |
/// End |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// MARK: - Additional
private extension IntervalType {
func random() -> Bound {
let range = (end as! CGFloat) - (start as! CGFloat)
let randomValue = (CGFloat(arc4random_uniform(UINT32_MAX)) / CGFloat(UINT32_MAX)) * range + (start as! CGFloat)
return randomValue as! Bound
}
}