Using sin() function

Hi, everyone !

I want to ask, when using function sin(), how to control it ?

I mean, when sin() reaches to -1 I want to say stop and move after some time. For example, see this sketch .

I appreciate any help, thanks !

Interesting problem. The approach I would take would be to write your own function that uses sin(), but only some of the time. Like so:

void setup(){
  size(800,400);
}

void draw(){
  background(0);
  stroke(255);
  fill(255);
  translate(0,200);
  line(0,0,width,0);
  noStroke();
  for( int x = 0; x <width; x++){
    float a = map(x,0,width,0,TWO_PI+2);
    float y = sin_line(a,1);
    ellipse(x,-180*y,5,5);
  }
  
}

float sin_line(float a, float d) {
  // Takes an angle a, and a line length d, and returns the sin(a), except not really.
  // At the peaks of the sine curve are lines of length d where the wave stays at the peak.
  while( a < 0 ){
    a += TWO_PI;
  }
  while( a >= TWO_PI + d + d ){
    a -= TWO_PI + d + d;
  }
  // a is now in the range 0 to TWO_PI + 2 * d.
 
  if( a >= 0 && a < HALF_PI ){
    return sin(a);
  }
  if( a >= HALF_PI && a < HALF_PI + d ){
    return(1);
  }
  if( a >= HALF_PI + d && a < HALF_PI + d + PI ){
    return(sin(a-d));
  }
  if( a >= HALF_PI + d + PI && a < HALF_PI + d + PI + d){
    return(-1); 
  }
  if( a >= HALF_PI + d + PI + d){
    return(sin(a-(2*d))); 
  } 
  return(0); // Should not occur.
}

If you run this, you can see that it plots a sine wave, except the wave crests are not mountain peaks, but plateaus and plains.

Yes, thank you man. This what I wanted !

Hi, sorry for disturbing, Can I achieve this effect with cos() function ?

cosine is just the sin() function shifted by 90° or PI/2.

float ofSet = 0.05;
void setup() {
  size(600,400);
}

void draw() {
  background(0);
  fill(255,0,0);
  text("Cosine",40,50);
  fill(0,0,255);
  text("Sine",20,350);
  for(int i = 0; i < width; i++) {
    stroke(255,0,0);
    point(i,cos(i*ofSet)*100+height/2);
    line(i,cos(i*ofSet)*100+height/2,i+1,cos( (i+1)*ofSet)*100+height/2);
    
    stroke(0,0,255);
    point(i,sin(i*ofSet)*100+height/2);
    line(i,sin(i*ofSet)*100+height/2,i+1,sin( (i+1)*ofSet)*100+height/2);
  }
}

image