Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

210
Visualizações
Using if statements to draw lines on top of random sequence of rectangles

I'm drawing rectangles in a random sequence, but i want to draw lines on TOP of these rectangles, in the same random sequence, to create a pattern as a layer on top of the rectangles. Why is the last part of my code where i try this is not visible, the lines are located after the rectangles so they should get drawn after as well and therefor on top?

<script src="https://cdn.jsdelivr.net/npm/processing-js@1.6.6/processing.min.js"></script>

<script type="application/processing">
int spacing = 10;
int x, y;
int spacing2 = 10;
int x2, y2;

void setup() {
  size(800, 800);
  noStroke();
  background(52, 122, 235);
}

void draw() {
  float j = random(2);
  if (j < 1) {
    if (j < 0.5) {
      fill(0, 0, 255);
    }
    if (j > 0.7) {
      fill(0, 0, 155);
    }
    rect(x, y, spacing, spacing);
  }

  x = x + spacing;
  if (x > width) {
    x = 0;
    y = y + spacing;
  }

  //WHY IS THE LINES NOT SHOWING UP?
  if (j > 1) {
    line(x, y, x + spacing, y + spacing); // "\"
  } else {
    line(x, y + spacing, x + spacing, y); // "/"
  }
}
</script>
<canvas></canvas>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  1. You never set a stroke, so the lines were invisible.
  2. You are updating the x and y values before you draw the line, so the line will be in the position of the next square. That means the lines will only remain visible in the locations where a square is not drawn.

<script src="https://cdn.jsdelivr.net/npm/processing-js@1.6.6/processing.min.js"></script>

<script type="application/processing">
int spacing = 10;
int x, y;
int spacing2 = 10;
int x2, y2;

void setup() {
  size(800, 800);
  noStroke();
  background(52, 122, 235);
}

void draw() {
  float j = random(2);
  if (j < 1) {
    noStroke();
    if (j < 0.5) {
      fill(0, 0, 255);
    }
    if (j > 0.7) {
      fill(0, 0, 155);
    }
    rect(x, y, spacing, spacing);
  }
  
  x = x + spacing;
  if (x > width) {
    x = 0;
    y = y + spacing;
  }

  // This wasn't showing up because there was no stroke color specified and
  // you call noStroke() in setup.
  stroke(255, 0, 0);
  if (j > 1) {
    line(x, y, x + spacing, y + spacing); // "\"
  } else {
    line(x, y + spacing, x + spacing, y); // "/"
  }
}
</script>
<canvas></canvas>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda