I want to print a chess/checkers board and with the help of a tutorial came to this code. I understand everything that's happening apart from this line: rect(i * squareSize,j * squareSize,squareSize, squareSize);
The thing I do not get is why you multiply i by the variable squareSize.
My first intuition was to the rects inside the if/else statements below the fill functions and not below the else.
Can anyone explain to me what I am missing and why my method does not work and multiplying by squareSize is necessary?
I have added my working code as a snippet below.
const WIDTH = 400;
const HEIGHT = 400;
const ROWS = 10;
const COLS = 10;
var squareSize = WIDTH/10;
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
drawBoard();
}
function drawBoard(){
for(var i = 0; i < ROWS; i++)
{
for(var j = 0; j < COLS; j++)
{
if((i+j) % 2 == 0)
{
fill(0);
}
else
{
fill(255);
}
rect(i * squareSize,j * squareSize,squareSize, squareSize);
}
}
noLoop();
}
html, body {
margin: 0;
padding: 0;
}
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.1/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.1/addons/p5.dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.1/addons/p5.sound.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<script src="sketch.js"></script>
</body>
</html>
if((i+j) % 2 == 0) { fill(0); rect(arg,arg,arg,arg); } else { fill(255); rect(arg,arg,arg,arg); } After your explanation I feel quite stupid because it is so obvious, but I guess I just had a blockade :) Thanks for your response. – user10218185 Aug 13 '18 at 20:59