WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Day 45: Understanding Chessboard logic

Forums > 100 Days of SwiftUI

  • During the day nr. 45 topic nr. 3 I'm struggling for the first time with understanding logic behind the code. I can't contect the "dots" between the:
  1. "for...in..." if
  2. (row + column).isMultiple(of: 2)
  3. let rect = CGRect(x: startX, y: startY, width: columnsSize, height: rowSize)

(black and white chessboard with first top left rectangle being white)

My wrong understanding(example):

  • For row = 0 and column = 0 ............if (0 + 0).isMultiple(of: 2) .........is not multiple / 2

  • For row = 0 and column = 1 .............if (0 + 1).isMultiple(of: 2) .........is not multiple / 2

  • For row = 1 and column = 0.............. if (1 + 0).isMultiple(of: 2) ..........is not multiple / 2

  • Following this logic there should be 3 rectangles with the same color next to eachother

  • I assume that if (0 + 0).isMultiple(of: 2) is in fact multiple of 2 because in reality it's row: 1 and column 1 but writen down as a 0 in both cases (1 + 1 multiple of 2). Is that correct?

If it's correct then startX = 0, startY = 0 (that's the very corner of the view). How by applying startX = 0, startY = 0 into the :

  • let rect = CGRect(x: startX, y: startY, width: columnsSize, height: rowSize)
  • do we get black rectangles but not in the area attached with the 0,0 coordinations?

This is my first post with asking for help in any topic related with programming so forgive me mess if there is some. I hope it's readable. Thank you in advance.

struct Checkerboard: Shape {
    var rows: Int
    var columns: Int

    func path(in rect: CGRect) -> Path {
        var path = Path()

        let rowSize = rect.height / Double(rows)
        let columnsSize = rect.width / Double(columns)

        for row in 0..<rows {
            for column in 0..<columns {
                if (row + column).isMultiple(of: 2) {
                    let startX = columnsSize * Double(column)
                    let startY = rowSize * Double(row)

                    let rect = CGRect(x: startX, y: startY, width: columnsSize, height: rowSize)
                    path.addRect(rect)
                }
            }
        }
        return path
    }
}

1      

0 is a multiple of 2

  • 2 x 0 = 0
  • 2 x 1 = 2
  • 2 x 2 = 4
  • 2 x 3 = 6
  • etc...

so row, column

  • 0, 0 = 0 : black
  • 0, 1 = 1 : white
  • 0, 2 = 2 : black
  • 1, 0 = 1 : white
  • 1, 1 = 2 : black
  • 1, 2 = 3 : white
  • 2, 0 = 2 : black
  • 2, 1 = 3 : white
  • 2, 2 = 4 : black

put this is into a playground:

if 0.isMultiple(of: 2) {
    print("It's a multiple of 2")
} else {
    print("It's not a multiple of 2")
}

2      

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.