Linee e Background

Ciao a tutti, ho un problema con il movimento di una linea: non capisco perchè se io voglio una linea che si muova a dx e a sin nel mio sketch questa si muove creando dello spazio bianco?

color red = #FF0004;
color yellow = #F2F700;
color blue = #0A30FF;
color black = #000000;
color white = #FFFFFF;

float x = 300;
float y1 = 300;
float y2 = 500;
float new_x= x;
float new_y1 = y1;
float new_y2 = y2;

int w = 600;
int h = 600;

float actualLinePosition = 0;
float newLinesPosition = 0;
float speed = 2.5;

int min = 0+200;
int max = w-100;

float latoArancio = x;
float latoRosso = y1;
float latoMagenta = y2;
float latoGiallo = h-latoRosso;
float latoAzzurro = h-latoMagenta; 
float latoBlu = w-latoArancio; 

void settings() {
  size(w, h);
}
void setup() {
  background(black);
  frameRate(30);
  actualLinePosition = random (min, max);
  newLinesPosition = random(min, max);
  surface.setTitle("Mondrian");
}

void draw() {

  if (actualLinePosition < newLinesPosition) {
    actualLinePosition += speed;
    if (actualLinePosition >= newLinesPosition) {
      actualLinePosition = newLinesPosition;
      newLinesPosition = random(min,max);
    }
  }  

  if (actualLinePosition > newLinesPosition) {
    actualLinePosition -= speed;
    if (actualLinePosition <= newLinesPosition) {
      actualLinePosition = newLinesPosition;
      newLinesPosition = random(min,max);
    }
  }  

  //if (new_y1 < y1) y1 = y1-speed;
  //if (new_y1 > y1) y1 = y1+speed;

  //if (new_y2 < y2) y2 = y2-speed;
  //if (new_y2 > y2) y2 = y2+speed;

  strokeWeight(13);
  stroke(white);
  //line(0, latoRosso, latoArancio, latoRosso);
  //line(latoArancio, latoMagenta, w, latoMagenta );
  line(actualLinePosition, 0, actualLinePosition, h);
}

Qualcuno sa il motivo?
Grazie

1 Like

Yes.

You forgot to say
background(black);
at start of draw().

Therefore, the white line is not deleted but stays on the screen.

So you get white areas.

Chrisir

color red = #FF0004;
color yellow = #F2F700;
color blue = #0A30FF;
color black = #000000;
color white = #FFFFFF;

float x = 300;
float y1 = 300;
float y2 = 500;
float new_x= x;
float new_y1 = y1;
float new_y2 = y2;

int w = 600;
int h = 600;

float actualLinePosition = 0;
float newLinesPosition = 0;
float speed = 2.5;

int min = 0+200;
int max = w-100;

float latoArancio = x;
float latoRosso = y1;
float latoMagenta = y2;
float latoGiallo = h-latoRosso;
float latoAzzurro = h-latoMagenta; 
float latoBlu = w-latoArancio; 

void settings() {
  size(w, h);
}
void setup() {
  background(black);
  frameRate(30);
  actualLinePosition = random (min, max);
  newLinesPosition = random(min, max);
  surface.setTitle("Mondrian");
}

void draw() {
  background(black);


  if (actualLinePosition < newLinesPosition) {
    actualLinePosition += speed;
    if (actualLinePosition >= newLinesPosition) {
      actualLinePosition = newLinesPosition;
      newLinesPosition = random(min, max);
    }
  }  

  if (actualLinePosition > newLinesPosition) {
    actualLinePosition -= speed;
    if (actualLinePosition <= newLinesPosition) {
      actualLinePosition = newLinesPosition;
      newLinesPosition = random(min, max);
    }
  }  

  //if (new_y1 < y1) y1 = y1-speed;
  //if (new_y1 > y1) y1 = y1+speed;

  //if (new_y2 < y2) y2 = y2-speed;
  //if (new_y2 > y2) y2 = y2+speed;

  strokeWeight(13);
  stroke(white);
  //line(0, latoRosso, latoArancio, latoRosso);
  //line(latoArancio, latoMagenta, w, latoMagenta );
  line(actualLinePosition, 0, actualLinePosition, h);
}
2 Likes

Also, your code is too complicate

Please understand why this code works

and how


float x = 300, y = 300;
float xAdd = 3, yAdd = 0;

void setup() {
  size(600, 600);
  background(0);
}

void draw() {
  background(0);

  // show 
  ellipse(x, y, 
    12, 12); 

  // move 
  x+=xAdd;
  y+=yAdd;

  // reflect 
  if (x<6) 
    xAdd= abs(xAdd); // always positive 
  if (x>width-6) 
    xAdd= -abs(xAdd); // always negative
}
//
2 Likes

grazie mille :Sorridi:

1 Like