swift3 - How do I create a circle packing algorithm for circles with unequal sizes in iOS? -
i trying pack bunch of round uiviews in hexagonal pattern. have different sizes.
first randomly generate uiviews , put them on screen shown:
have algorithm arranges views in circular pattern around center. algorithm:
func arrangeviews() { let viewcenter = self.view.center let radius: double = 50?? <- put here? views has different radius? var currentdistfromcenter: double = (radius * 2) var nummoved = 0 let amountofviews = views.count nummoved += 1 while nummoved < amountofviews { var numbertofit = double(m_pi / asin(radius / currentdistfromcenter)) if numbertofit > double(amountofviews - nummoved) { numbertofit = double(amountofviews - nummoved) } in 0 ..< int(numbertofit) { let currentview = views[nummoved] let angle = double(m_pi * 2.0 * double(i) / numbertofit) let x = double(viewcenter.x) + cos(angle) * currentdistfromcenter let y = double(viewcenter.y) + sin(angle) * currentdistfromcenter var newpoint = cgpoint(x: cgfloat(x), y: cgfloat(y)) views.first?.center = self.view.center if newpoint.x != currentview.frame.origin.x || newpoint.y != currentview.frame.origin.y { uiview.animate(withduration: 0.3, animations: { currentview.center = newpoint }) nummoved += 1 } } currentdistfromcenter += radius * 2 } }
here result after run function:
algorithm circles (views) same size. see don't lie next each other if had same size. shown here:
is there out there has form of clue change in algorithm can pack views different sizes?
here links i've encountered during research, haven't gotten me far because maths isn't strongest side:
http://www.optimization-online.org/db_file/2008/06/1999.pdf
packing different sized circles rectangle - d3.js
http://jsfiddle.net/tdzve/
thank in advance , happy programming!
as per result needed, prefer firstly draw 7 circles @ same center.
suppose red colored center circle of size (x, y, w, h)
then
1st (top left) - (x-w/2, y-(h+h/2), w, h) 2nd (top right) - (x+w/2, y-(h+h/2), w, h) 3rd (right) - (x+w, y, w, h) 4th (bottom right) - (x+w/2, y+(h+h/2), w, h) 5th (bottom left) - (x-w/2, y+(h+h/2), w, h) 6th (left) - (x-w, y, w, h)
this applied in cases of hexagonal part, w/h change each circle like
1st (top left) - (x-w/2, y-(h+h/2), w1, h1) 2nd (top right) - (x+w/2, y-(h+h/2), w2, h2) 3rd (right) - (x+w, y, w3, h3) 4th (bottom right) - (x+w/2, y+(h+h/2), w4, h4) 5th (bottom left) - (x-w/2, y+(h+h/2), w5, h5) 6th (left) - (x-w, y, w6, h6)
Comments
Post a Comment