A float behaves like an int-value

Hi Coders!

I am trying to make my program draw lines, that become just a little bit thicker each step. But: The sketch does not react to my decimal numbers. I want to have a gradient in the strokeWeight of my lines. Is this possible?

void setup () {
  size(540, 540);
  background(0);

  fill(255;
}

void draw() {
  background(0);
  noStroke();

  for (float i = 0; i < a; i++) {
rect(width-i*10,0,i/20,height);
  }
}

Answers

  • Your code does not compile. You're missing a parenthesis, and you haven't defined a. Please post the code you're actually running, preferably as an MCVE.

  • edited November 2015

    It's very unusual to choose a non-whole datatype as the iterator counter.
    Unless you've got a good motive for it, just stick w/ regular int. 8-|

  • Hi guys, thanks! I've fixed it by changing some numbers

    int a;
    int w;
    int d = 1;
    
    color light = #90B390;
    color dark = #F4C1A8;
    
    float scaling = 0;
    
    void setup () {
      size(540, 540);
      background(dark);
      smooth();
      fill(light);
    }
    
    void draw() {
      background(dark);
      noStroke();
      for (int i = 0; i < a; i++) {
    rect(width-i*10,0,1+i/6,height);
      }
    
    
      // Loop it!
      if (w < 50) {
        w++;
      } else {
        w = 0;
        d *= -1;
      }
    
    
      a=a+d;
    
    }
    
  • I was wondering how I would do it and I came up with this:

    color light = #90B390;
    color dark = #F4C1A8;
    
    
    void setup () {
      size(540, 540);
      background(dark);
      smooth();
      fill(light);
    }
    
    float a = 0; 
    
    void draw() {
      background(dark);
      noStroke();
    
      a += radians(360/120); // full circle / the amount of frames it should take  
      float r = norm(sin(a), -1, 1);
    
      int n = int(r*55);
    
      for (int i = 0; i < n; i++) {
        rect(width-i*10, 0, 1+i/6, height);
      }
    
    }
    
  • edited November 2015 Answer ✓

    It is not fixed the lines get thicker every sixth line rather than each line.

    Change the rect statement to

    rect(width-i*10, 0, 1+i/6.0, height);

    The difference between i/6 and i/6.0 is

    i  i/6  i/6.0
    0   0   0.0
    1   0   0.16666667
    2   0   0.33333334
    3   0   0.5
    4   0   0.6666667
    5   0   0.8333333
    6   1   1.0
    7   1   1.1666666
    8   1   1.3333334
    9   1   1.5
    10  1   1.6666666
    11  1   1.8333334
    12  2   2.0
    13  2   2.1666667
    14  2   2.3333333
    15  2   2.5
    16  2   2.6666667
    17  2   2.8333333
    18  3   3.0
    19  3   3.1666667
    
  • Perfect! Thank you guys!!!

Sign In or Register to comment.