sin

.sin()

Returns the sine of this number.

This number is presumed to be a radian.

val = angle.sin()

.sin( min, max )

It returns the sine of the current angle, multiplied in a way so that it will fit into the range between min and max.

This is useful as a simple way to generate sine waves. For example you might want to have a wave between 0.0 and 1.0 for animating an alpha value, or between 100 and 200 for animating a location.

/* 
 * Draws a square and a Circle,
 * both animated using a sine wave.
 */

angle = 0
onEachFrame() do |delta|
    fill( :black )
    
    angle = angle + delta/25.0
    
    x = angle.sin( 375, 425 )
    y = angle.sin( 125, 275 )
    fillCircle( x, y, 50, true )
    
    setAlpha( angle.sin(0, 1) ) do
        fillRect( 150, 150, 100, 100 )
    end
end

Advanced Example

This demo draws sine waves across the screen.

x = 0
angle = 90.toRadians()
size = 100

lastX = x
lastY = getScreenHeight()/2 + size*angle.sin()

onEachFrame() do |delta|
    angle = angle + delta/22
    
    x = x + delta * 5
    if x > getScreenWidth()
        x = 0
        lastX = 0
    end
    
    y = getScreenHeight()/2 + size*angle.sin()
    
    setColor( :blue )
    setBlend( :add )
    drawLine( lastX, lastY, x, y )
    
    lastX = x
    lastY = y
end

See also

asin - cos - tan