How to position CGRect paths neatly

167 views Asked by At

in my macOS app I create array of objects based on Cell class below

class Cell {

var cell = Path()
var live = false
private let gridOrigin = (CGFloat(10), CGFloat(10))
private var x = CGFloat(0)
private var y = CGFloat(0)

func createCell(column: CGFloat, row: CGFloat, cellSize: CGFloat) {
    x = gridOrigin.0 + CGFloat(column) * cellSize
    y = gridOrigin.1 + CGFloat(row) * cellSize
    cell.addRect(CGRect(x: x, y: y, width: cellSize, height: cellSize))
    cell.closeSubpath()
    live = Bool.random()
}
}

and I use this code to display them by this code

VStack(spacing: 0) {
        ForEach(0..<50) { column in
            HStack(spacing: 0) {
                ForEach(0..<50) { row in
                    let cellIndex = column * 50 + row
                    if cells[cellIndex].live {
                        cells[cellIndex].cell.fill().foregroundColor(.green)
                    } else {
                        cells[cellIndex].cell.fill().foregroundColor(.blue)
                    }
                }
            }
        }
    }

unfortunately rectangles are drawn with spaces (horizontal and vertical) even if individual Rect coordinates are calculated and set properly. Note that spaces are dynamically changing as I resize window. I tried to used LazyVGrid but it didn't help. Do you have any hint how to draw rects without spaces?How restc are shown now

0

There are 0 answers