redrubia
redrubia

Reputation: 2366

Colouring between lines in Processing

Having drawn multiple lines using the sin function, I wondered how you would fill in the gap between each of the two lines

float a = 0.0;
float inc = TWO_PI/25.0;

for(int i=0; i<100; i=i+4) {
  line(i, 50, i, 50+sin(a)*40.0);
  a = a + inc;
}

Upvotes: 3

Views: 261

Answers (2)

maskacovnik
maskacovnik

Reputation: 3084

I would fill it like this:

(It is a pseudocode, You haven't specified the language)

EDIT as @GeorgeProfenza noticed in comment below, you have specified the language

float a = 0.0;
float inc = TWO_PI/100.0; //4x decreased inc

for(int i=0; i<100; i=i+1) { //4x increased count of looping
  if(i%4==0){
      stroke(0);
  }else{
      stroke(255,0,0);
  }
  line(i, 50, i, 50+sin(a)*40.0);
  a = a + inc;
}

I would paint lines close together and every fourth will be black

Upvotes: 1

George Profenza
George Profenza

Reputation: 51867

@maskacovnik's solution will work. You can also be a bit cheeky and simply draw a shape:

float a = 0.0;
float inc = TWO_PI/25.0;
beginShape();
for(int i=0; i<=100; i=i+4) {
  vertex(i, 50+sin(a)*40.0);
  a = a + inc;
}
endShape();

Here's a preview you can run(using js):

function setup() {
  createCanvas(100,100);
  background(192);
  var a = 0.0;
  var inc = TWO_PI/25.0;
  beginShape();
  for(var i=0; i<=100; i=i+4) {
    vertex(i, 50+sin(a)*40.0);
    a = a + inc;
  }
  endShape();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.6/p5.min.js"></script>

Upvotes: 4

Related Questions