Relaxing with Sine and Cosine

Last night I wanted to play with sine and cosine waves so I created this snippet:

Actionscript:
  1. var wave:Number = 0;
  2. addEventListener(Event.ENTER_FRAME, onLoop);
  3. function onLoop(evt:Event):void {
  4.        graphics.clear()
  5.        graphics.lineStyle(0,0);
  6.        var time:Number = (stage.stageWidth/2 - mouseX)/10
  7.        for (var j:int = 0; j<100; j++){
  8.                var offset:Number = j/10;
  9.                var t:Number = 0;
  10.                var wh:Number = j * 4;
  11.                for (var i:int = 0; i<300; i++){
  12.                        wave = cos(10-offset, t + time + offset)
  13.                               + sin(time, t/2 + time - offset) + wh
  14.                               + cos(3, t * 4);
  15.                        t += 0.1;
  16.                        var xoff:Number = i * 2;
  17.                        if (i == 0){
  18.                                graphics.moveTo(100 + xoff, 100 + wave);
  19.                        }else{
  20.                                graphics.lineTo(100 + xoff, 100 + wave);
  21.                        }
  22.  
  23.                }
  24.        }
  25. }
  26.  
  27. function cos(radius:Number, theta:Number):Number{
  28.        return radius * Math.cos(theta);
  29. }
  30. function sin(radius:Number, theta:Number):Number{
  31.        return radius * Math.sin(theta);
  32. }


Have a look at the swf...

This entry was posted in Uncategorized. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

4 Comments

  1. Posted April 7, 2010 at 3:41 pm | Permalink

    “Last night I wanted to play with sine and cosine waves ”
    They have good medecine for that now…

    Thanks, not especially for this one but for all the nights you’re playing with those good stuffs you share….
    Don’t forget to sleep.

  2. laFunk
    Posted April 8, 2010 at 1:15 pm | Permalink

    Hey! As always you bring some cool stuff, man!
    I’ve been playin’ with the code for a while and i came with this (dirty code, watch out!):

    var wave:Number = 0;
    var time:Number = 0;
    var wait:Number = 5000;
    var dt:Number = 0;
    var v:Number = 0;
    var inercia:Number = 0;

    var py:Number = 2;
    var dy:Number = 2;

    public function Main():void
    {
    if (stage) init();
    else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void
    {
    removeEventListener(Event.ADDED_TO_STAGE, init);
    addEventListener(Event.ENTER_FRAME, onLoop);

    }
    function onLoop(evt:Event):void {
    graphics.clear()
    graphics.lineStyle(0, 0);
    wait++;
    if (wait > 200) {
    dt = dt + Math.random() * 20 - 10;
    dy = dy + Math.random() * 1 - 0.5;
    wait = 0;
    inercia += (inercia-(v-dt));
    }

    var idx = -time + dt;
    v = v * 0.999 + idx*0.0001;
    time += v ;

    py += (dy - py) / 100;
    inercia = inercia*0.98;

    for (var j:int = 0; j<50; j++){
    var offset:Number = j/10;
    var t:Number = 0;
    var wh:Number = j * 4;

    var alph:int = j * 10 / 5;

    graphics.lineStyle(0, 0 , alph/100);
    for (var i:int = 0; i < 150; i++) {

    wave = cos(10-offset, t + time + offset)
    + sin(time, t/2 + time - offset) + wh
    + cos(3, t * (2+j/10-3));
    t += 0.1;
    var xoff:Number = i * 4;
    if (i == 0){
    graphics.moveTo(100 + xoff, (100 + wave)*py-py*40);
    }else{
    graphics.lineTo(100 + xoff, (100 + wave)*py-py*40);
    }
    }
    }
    }
    function cos(radius:Number, theta:Number):Number{
    return radius * Math.cos(theta);
    }
    function sin(radius:Number, theta:Number):Number{
    return radius * Math.sin(theta);
    }

  3. Posted April 8, 2010 at 7:01 pm | Permalink

    very nice variation

  4. Posted April 30, 2010 at 6:39 am | Permalink

    Have you thought of using sin and cos or a single noise() function to create a Perlin noise like texture? I am trying to generate that on C64 in assembler ;-)

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*