# Using sin() function

**URL:** <https://discourse.processing.org/t/using-sin-function/26293>\
**Category:** Coding Questions\
**Created:** [December 16, 2020, 6:17am UTC](https://discourse.processing.org/t/using-sin-function/26293 "2020-12-16T06:17:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yhlaskerimov](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yhlaskerimov/32/9725_2.png) [@yhlaskerimov](https://discourse.processing.org/u/yhlaskerimov)\
**Post date:** [December 16, 2020, 6:17am UTC](https://discourse.processing.org/t/using-sin-function/26293/1 "2020-12-16T06:17:28Z")

</div>

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](https://www.instagram.com/p/By53lvRn2ra/) .

I appreciate any help, thanks !

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [December 16, 2020, 8:18am UTC](https://discourse.processing.org/t/using-sin-function/26293/2 "2020-12-16T08:18:57Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![yhlaskerimov](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yhlaskerimov/32/9725_2.png) [@yhlaskerimov](https://discourse.processing.org/u/yhlaskerimov)\
**Post date:** [December 16, 2020, 9:45am UTC](https://discourse.processing.org/t/using-sin-function/26293/3 "2020-12-16T09:45:19Z")

</div>

Yes, thank you man. This what I wanted !

---

<div class="post-metadata">

**Author:** ![yhlaskerimov](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yhlaskerimov/32/9725_2.png) [@yhlaskerimov](https://discourse.processing.org/u/yhlaskerimov)\
**Post date:** [December 21, 2020, 12:50pm UTC](https://discourse.processing.org/t/using-sin-function/26293/4 "2020-12-21T12:50:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 21, 2020, 9:24pm UTC](https://discourse.processing.org/t/using-sin-function/26293/5 "2020-12-21T21:24:56Z")

</div>

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

```auto
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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/36b851956c0f0042fb4be1982d633dbe084de77d.png)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 22, 2020, 9:03am UTC](https://discourse.processing.org/t/using-sin-function/26293/6 "2020-12-22T09:03:19Z")

</div>

> **[Trigonometry Primer \\ Processing.org](https://processing.org/tutorials/trig/)**
