This wiki is out of date, use the continuation of this wiki instead

Tutorial:Cosine

From FenixWiki

Revision as of 23:28, 29 May 2007 by Sandman (Talk | contribs)
Jump to: navigation, search

Some sine and cosine program. Check it out. More of example code, but useful anyway.

Example code

Program cosine;
Const
    seconds       = 2.3;
    ball_width    = 20;
    ball_height   = 20;
    screen_width  = 320;
    screen_height = 200;
    screen_depth  = 8;
    screen_fps    = 60;
    cos_factor    = 2000;
    sin_factor    = 4000;
    timer_spawn   = 0;
    timer_ball    = 1;
Begin

    // make the movement a bit more smooth
    set_fps(screen_fps,0);

    // spawn a ball process every <seconds> seconds with random colors.
    timer[timer_spawn]= seconds*100;
    Repeat
        if(timer[timer_spawn]>=seconds*100)
            timer[timer_spawn]-=seconds*100;
            x = new_map(ball_width,ball_height,screen_depth);
            map_clear(0,x,rgb(rand(0,255),rand(0,255),rand(0,255)));
            ball(x);
        end
        frame;
    Until(key(_esc))

    // kill all other remaining processes and exit
    let_me_alone();
    exit();

End

Process ball(graph)
Private
    int start_time;
Begin

    // remember starting time
    start_time = timer[timer_ball];

    // position the ball according to the passed time (timer[timer_ball])
    Loop
        x = screen_width /2 + cos((timer[timer_ball]-start_time)*cos_factor)
                              * (screen_width /2-ball_width /2);
        y = screen_height/2 + sin((timer[timer_ball]-start_time)*sin_factor)
                              * (screen_height/2-ball_height/2);
        frame;
    End

End

Used in example: set_fps(), new_map(), map_clear(), rand(), key(), let_me_alone(), cos(), sin(), timer, x, y

This will result in something like:
cosine.PNG

Or with seconds=0.25; cos_factor=6000;sin_factor=2000 something like this:
cosine2.PNG

Personal tools