map() values from an array for a sine wave

edited July 2017 in Questions about Code

Hi everyone,

the following code I draw a sine wave.

    float xValue;
    float yValue[];
    float yScale[];


    void setup() {
      size(800, 400);
      background(0);
      noLoop();
      yValue = new float[width];
      yScale = new float[height];
      for (int i=0; i<width; i++) {
        xValue = map(i, width,0, 0, 4*PI);
        yValue[i] = sin(xValue)*height/2+height/2;       //<-- Currently I help myself by multiplication with height/2.
        //yScale[i] = map(yValue[i],199,201,0,height);  // <-- I wanna use map() for rescaling the y values but There is always an error
      }
    }

    void draw() {
      background(0);
      stroke(255);
      strokeWeight(2);
      for (int i=0; i<799; i++) { //<-- The same problem here. Whenever I set the width as limit.
        line(i, yValue[i], i+1, yValue[i+1]);
        println("yValue:"+yValue[i]);
      }
    }
Tagged:

Answers

  • edited July 2017

    Look at line 14. What are the range of values calculated for yValue[i]?

    The answer is 0 - height but in your map you are assuming that the range is 199 - 201

    Can you see your mistake? If so you should be able to fix this yourself, try it :)

  • As you know, sine outputs a value from -1 to 1. You are re-scaling it when you multiply it by height/2. You need to keep this into account when using the map function in line 15.

    Kf

  • edited July 2017

    @kfrajer he is also applying a translation with +height/2 ;)

Sign In or Register to comment.