Advertisement
HXXXXJ

Flowlayout rect

Mar 9th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.89 KB | None | 0 0
  1. struct Point {
  2.     let x : Double
  3.     let y : Double
  4. }
  5.  
  6.  
  7. struct Rect {
  8.     let h : Double
  9.     let w : Double
  10. }
  11.  
  12. func getPoint(_ out : Rect, _ array : [Rect] ) -> [Point]{
  13.     guard array.count > 0 else { return []}
  14.     var res = [Point]()
  15.     var currx: Double = 0
  16.     var curry: Double = 0
  17.     var maxCurrW : Double = 0
  18.     var i = 0
  19.    
  20.     while i < array.count {
  21.         let rect = array[i]
  22.         if rect.w + currx > out.w { return [] } // not valid input
  23.         if rect.h + curry > out.h {
  24.             if curry == 0 { return  [] } // not valid input
  25.             // need to col
  26.             curry = 0
  27.             currx += maxCurrW
  28.             maxCurrW = 0
  29.             continue
  30.         }
  31.         // put on current col
  32.         res.append(Point(x: currx, y: curry))
  33.         maxCurrW = max(maxCurrW, rect.w)
  34.         curry += rect.h
  35.         i += 1
  36.     }
  37.     return res
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement