WWDC23 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      

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.