We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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]);
}
}
Answers
Look at line 14. What are the range of values calculated for
yValue[i]
?The answer is
0 - height
but in yourmap
you are assuming that the range is199 - 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
@kfrajer he is also applying a translation with
+height/2
;)