I'm trying to display the x location when y has the highest value. In the if statement i tried to compare the y value with 120 but it seems it's never true so it doesn't display my x location using text function. I've also tried to round up the y value but still the result is not the one i wanted. Can anyone help me ?
Regards Antmar.
float x = 0.0;
float y = 0.0;
void setup() {
background(150);
size(800,200);
smooth();
line(0,100,width,100);
}
void draw() {
//background(255);
x += 0.5;
y =map (sin(x/20),-1,1,80,120) ;
noStroke();
fill(#BBFFDD);
ellipse(x, y , 1, 1);
if ( y < 119 && y > 118.94) {
textSize(10);
text(x,x-10,y+20);
}
println("x =",x,"|","y =",y);
}
I think I've figured out what your issue is. When I ran your code, it printed out your x-value at several places, but it was not doing this printing every time. To fix this, I first incremented x by PI/4
instead of .5, to make it more likely to be close to 120. Then, I looked at the values of y, and the highest one was just a shade under 120. To print out the x-value at that point, I made the if-condition the following, as to only catch that one y-value. I also added another if-condition to get the value of x at the local maximums of this sin function:
if ( y >= 100 + 19.999) {
textSize(10);
text(x,x-10,y+20);
}else if(y <= 100 - 19.999){
textSize(10);
text(x,x+10,y-10);
}
Edit: After looking at the values of x and y, I found that incrementing x by PI/4
leads to y having values of exactly 120 and 80 as minimums and maximums, so you can just check for those values instead, like so:
if (y == 120) {
textSize(10);
text(x,x-10,y+20);
}else if(y == 80){
textSize(10);
text(x,x+10,y-10);
}