Format
Plain text
Post date
2017-10-15 08:55
Période de publication
Illimité
  1. import Cocoa
  2. infix operator ⨂
  3. func ⨂<T> (lhs: [T], rhs: [T]) -> [[T]] {
  4. return lhs.flatMap { l in rhs.map { [l, $0] } }
  5. }
  6. enum CheckerStart: Int {
  7. case positive = 0
  8. case negative = 1
  9. }
  10. func checker(size: NSSize, checkerSize: Int = 10, start: CheckerStart = .positive) -> NSImage {
  11. let image = NSImage(size: size)
  12. let colTileNum = Int(ceil(size.width / CGFloat(checkerSize)))
  13. let rowTileNum = Int(ceil(size.height / CGFloat(checkerSize)))
  14. image.lockFocusFlipped(true)
  15. defer { image.unlockFocus() }
  16. NSColor.white.setFill()
  17. NSRect(origin: .zero, size: size).fill()
  18. NSColor.lightGray.setFill()
  19. (Array(0..<colTileNum) ⨂ Array(0..<rowTileNum))
  20. .filter { (el: [Int]) -> Bool in (el[0] + el[1]) % 2 == start.rawValue }
  21. .map { NSRect(x: $0[0] * checkerSize, y: $0[1] * checkerSize, width: checkerSize, height: checkerSize) }
  22. .forEach { $0.fill() }
  23. return image
  24. }
  25. let size = NSSize(width: 134, height: 122)
  26. let image = checker(size: size)
Télécharger Printable view

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text