• Print

Author Topic: Sine wave  (Read 92 times)

FellippeHeitor

  • QB64 Partner Site Owner
  • Hero Member
  • *
  • Posts: 1885
  • LET IT = BE
    • QB64.org
Sine wave
« on: May 04, 2018, 10:53:24 am »
Translated and adapted from javascript/p5.js: https://p5js.org/examples/math-sine-wave.html

Contains elements from p5js.bas: bit.ly/p5jsbas

Code: [Select]
DIM SHARED xspacing, w, theta, amplitude, period, dx
xspacing = 16
theta = 0.0
amplitude = 75.0
period = 500.0

TYPE t
    y AS SINGLE
    a AS SINGLE
END TYPE

SCREEN _NEWIMAGE(710, 400, 32)
w = _WIDTH + 8
dx = (_PI(2) / period) * xspacing
DIM SHARED yvalues(INT(w / xspacing)) AS t

FOR i = 1 TO UBOUND(yvalues)
    yvalues(i).a = yvalues(i - 1).a + 1
NEXT

DO
    CLS
    calcWave
    renderWave
    _DISPLAY
    _LIMIT 60
LOOP

SUB calcWave
    'http:// Increment theta (try different values for
    'http:// 'angular velocity' here)
    theta = theta + 0.02

    'http:// For every x value, calculate a y value with sine function
    x = theta
    FOR i = 0 TO UBOUND(yvalues)
        yvalues(i).y = _HEIGHT / 2 + SIN(x) * amplitude
        yvalues(i).a = yvalues(i).a + .1
        x = x + dx
    NEXT
END SUB

SUB renderWave
    'http:// A simple way to draw the wave with an ellipse at each location
    FOR x = 0 TO UBOUND(yvalues)
        c~& = hsb(yvalues(x).a MOD 360, 127, 127, 127)
        CircleFill x * xspacing, height / 2 + yvalues(x).y, 50, c~&
    NEXT
END SUB

FUNCTION hsb~& (__H AS _FLOAT, __S AS _FLOAT, __B AS _FLOAT, A AS _FLOAT)
    DIM H AS _FLOAT, S AS _FLOAT, B AS _FLOAT

    H = map(__H, 0, 255, 0, 360)
    S = map(__S, 0, 255, 0, 1)
    B = map(__B, 0, 255, 0, 1)

    IF S = 0 THEN
        hsb~& = _RGBA32(B * 255, B * 255, B * 255, A)
        EXIT FUNCTION
    END IF

    DIM fmx AS _FLOAT, fmn AS _FLOAT
    DIM fmd AS _FLOAT, iSextant AS INTEGER
    DIM imx AS INTEGER, imd AS INTEGER, imn AS INTEGER

    IF B > .5 THEN
        fmx = B - (B * S) + S
        fmn = B + (B * S) - S
    ELSE
        fmx = B + (B * S)
        fmn = B - (B * S)
    END IF

    iSextant = INT(H / 60)

    IF H >= 300 THEN
        H = H - 360
    END IF

    H = H / 60
    H = H - (2 * INT(((iSextant + 1) MOD 6) / 2))

    IF iSextant MOD 2 = 0 THEN
        fmd = (H * (fmx - fmn)) + fmn
    ELSE
        fmd = fmn - (H * (fmx - fmn))
    END IF

    imx = _ROUND(fmx * 255)
    imd = _ROUND(fmd * 255)
    imn = _ROUND(fmn * 255)

    SELECT CASE INT(iSextant)
        CASE 1
            hsb~& = _RGBA32(imd, imx, imn, A)
        CASE 2
            hsb~& = _RGBA32(imn, imx, imd, A)
        CASE 3
            hsb~& = _RGBA32(imn, imd, imx, A)
        CASE 4
            hsb~& = _RGBA32(imd, imn, imx, A)
        CASE 5
            hsb~& = _RGBA32(imx, imn, imd, A)
        CASE ELSE
            hsb~& = _RGBA32(imx, imd, imn, A)
    END SELECT

END FUNCTION

FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
    map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
END FUNCTION

SUB CircleFill (x AS LONG, y AS LONG, R AS LONG, C AS _UNSIGNED LONG)
    x0 = R
    y0 = 0
    e = 0
    DO WHILE y0 < x0
        IF e <= 0 THEN
            y0 = y0 + 1
            LINE (x - x0, y + y0)-(x + x0, y + y0), C, BF
            LINE (x - x0, y - y0)-(x + x0, y - y0), C, BF
            e = e + 2 * y0
        ELSE
            LINE (x - y0, y - x0)-(x + y0, y - x0), C, BF
            LINE (x - y0, y + x0)-(x + y0, y + x0), C, BF
            x0 = x0 - 1
            e = e - 2 * x0
        END IF
    LOOP
    LINE (x - R, y)-(x + R, y), C, BF
END SUB
« Last Edit: May 04, 2018, 10:59:15 am by FellippeHeitor »
- InForm for QB64 http://qb64.org/inform

- vWATCH64 (debugger for QB64) http://bit.ly/vWATCH64v1-103

- Games: http://bit.ly/2048_qb64 * http://bit.ly/ClonedShades_qb64source * http://bit.ly/2aqK866 * http://bit.ly/SpaceshipQB64 * http://bit.ly/2rD1pPP

Petr

  • Hero Member
  • *****
  • Posts: 656
Re: Sine wave
« Reply #1 on: May 04, 2018, 12:05:53 pm »
Nice job. You got me. I'm looking at the program and wondering: Since when does the MOD have more than one parameter? And then I just realized that it were paramters of the HSB function.  ;D
Coding is relax.

FellippeHeitor

  • QB64 Partner Site Owner
  • Hero Member
  • *
  • Posts: 1885
  • LET IT = BE
    • QB64.org
Re: Sine wave
« Reply #2 on: May 04, 2018, 12:28:55 pm »
 ;)
- InForm for QB64 http://qb64.org/inform

- vWATCH64 (debugger for QB64) http://bit.ly/vWATCH64v1-103

- Games: http://bit.ly/2048_qb64 * http://bit.ly/ClonedShades_qb64source * http://bit.ly/2aqK866 * http://bit.ly/SpaceshipQB64 * http://bit.ly/2rD1pPP

bplus

  • Hero Member
  • *****
  • Posts: 756
  • B = B + _
Re: Sine wave
« Reply #3 on: May 04, 2018, 06:18:20 pm »
Nice! I wonder if we could get Flappy Bird to fly through that?
Will you still love me, will you still need me, when I'm (QB) 64?

Ashish

  • Hero Member
  • *****
  • Posts: 604
  • Eat.... Sleep.... CODE.... Repeat.....
Re: Sine wave
« Reply #4 on: May 05, 2018, 07:41:46 am »
Nice! :)
if (Me.Success) {Me.Improve();} else {Me.TryAgain();}

aKFrameWork - http://bit.ly/aKFrameWork
p5js.bas - http://bit.ly/p5jsbas
Menu System - http://bit.ly/guiMenuBar

OpenGL Demos - http://bit.ly/openGLdemos

  • Print