' Animated wave patterns with sine and cosine functions
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL j  AS LONG
    LOCAL frame  AS LONG
    LOCAL numFrames  AS LONG
    LOCAL w  AS LONG
    LOCAL numWaves  AS LONG
    LOCAL amplitude(10) AS LONG
    LOCAL frequency(10) AS DOUBLE
    LOCAL phase(10) AS DOUBLE
    LOCAL speed(10) AS DOUBLE
    LOCAL waveHeight  AS DOUBLE
    LOCAL x  AS DOUBLE
    LOCAL timeVal  AS DOUBLE
    LOCAL combinedWave  AS DOUBLE
    LOCAL sample  AS DOUBLE
    LOCAL value  AS DOUBLE
    LOCAL peakVal  AS DOUBLE
    LOCAL troughVal  AS DOUBLE
    LOCAL waveSamples(80) AS DOUBLE
    LOCAL waveType AS LONG

    w = 40
    numFrames = 6
    numWaves = 4

    PRINT "=== Animated Wave Patterns ==="
    PRINT "Display width: " & FORMAT$(w) & " units"
    PRINT "Frames: " & FORMAT$(numFrames)
    PRINT "Waves: " & FORMAT$(numWaves)
    PRINT

    amplitude(0) = 10
    frequency(0) = 0.3
    phase(0) = 0.0
    speed(0) = 0.5

    amplitude(1) = 5
    frequency(1) = 0.7
    phase(1) = 1.57
    speed(1) = -0.3

    amplitude(2) = 3
    frequency(2) = 1.2
    phase(2) = 3.14
    speed(2) = 0.7

    amplitude(3) = 2
    frequency(3) = 2.5
    phase(3) = 4.71
    speed(3) = 0.2

    PRINT "--- Wave Components ---"
    FOR i = 0 TO numWaves - 1
        PRINT "  Wave " & FORMAT$(i) & ": amp=" & FORMAT$(amplitude(i)) & " freq=" & FORMAT$(frequency(i)) & " speed=" & FORMAT$(speed(i))
    NEXT i
    PRINT

    FOR frame = 0 TO numFrames - 1
        timeVal = frame * 0.5
        PRINT "--- Frame " & FORMAT$(frame) & " (t=" & FORMAT$(timeVal) & ") ---"

        peakVal = -1000
        troughVal = 1000

        FOR i = 0 TO w - 1
            combinedWave = 0.0
            FOR j = 0 TO numWaves - 1
                x = i
                value = amplitude(j) * SIN(frequency(j) * x + speed(j) * timeVal + phase(j))
                combinedWave = combinedWave + value
            NEXT j
            waveSamples(i) = combinedWave
            IF combinedWave > peakVal THEN peakVal = combinedWave
            IF combinedWave < troughVal THEN troughVal = combinedWave
        NEXT i

        PRINT "  Range: " & FORMAT$(troughVal) & " to " & FORMAT$(peakVal)

        FOR i = 0 TO 7
            sample = waveSamples(i * 5)
            PRINT "  x=" & FORMAT$(i * 5) & ": y=" & FORMAT$(sample)
        NEXT i
        PRINT
    NEXT frame

    PRINT "--- Wave Types ---"
    PRINT "  Sine: y = A * sin(f*x + s*t + p)"
    PRINT "  Cosine: y = A * cos(f*x + s*t + p)"
    PRINT "  Square: y = A * sign(sin(...))"
    PRINT "  Triangle: y = A * asin(sin(...)) * 2/pi"
    PRINT "  Sawtooth: y = A * 2/pi * atan(tan(...))"
    PRINT

    PRINT "--- Interference Patterns ---"
    PRINT "  Constructive: waves add in phase (peaks align)"
    PRINT "  Destructive: waves cancel (peak + trough)"
    PRINT "  Combined: " & FORMAT$(numWaves) & " waves superimposed"
    PRINT "  Max amplitude: sum of all amplitudes = " & FORMAT$(amplitude(0) + amplitude(1) + amplitude(2) + amplitude(3))
    PRINT

    PRINT "--- Standing Wave ---"
    PRINT "  y = sin(f*x) * cos(s*t)"
    PRINT "  Nodes at x = n*pi/f (zero amplitude)"
    PRINT "  Antinodes at x = (n+0.5)*pi/f (max amplitude)"

    PRINT
    PRINT "Wave animation complete."

    FUNCTION = 0
END FUNCTION
