I
I
Immorom2018-12-26 20:56:45
Java
Immorom, 2018-12-26 20:56:45

How to split a canvas into paintable segments in Processing?

I have been studying the Processing environment for half a year, but recently I stalled on one task: to write a program that divides the canvas into equal cells, on which you can draw.
Below is a program that "pixel by pixel" determines where the mouse is.

int r = 25; // Размер пикселя, на который будем делить холст

void setup() {
  size(251,251);
  noStroke();
  cursor(CROSS);
}

void draw() {
  background(255);
  for(int x = 0; x<width; x+=r) {       // Для каждого r(эр)-ного х...
    for(int y = 0; y<height; y+=r) {    // ...рисуем столбец квадратов
      if((mouseX>x)&&(mouseX<x+r)&&(mouseY>y)&&(mouseY<y+r)) // Проверка на то, находится ли мышь внутри квадрата
      { fill(255,0,0); } // И, в соответствии с результатом выше, закрашиваем квадраты в красный...
      else  { fill(255); } // ...или белый
      rect(x,y,r,r);
    }
  }
}

I want to do almost the same thing in the program for which I created the question, but in it the "pixels" will be painted over only by clicking on them, and their color will be "permanent".
I will be glad both direct hints on the code, and literature on this topic.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question