Author Topic: Sine Wave  (Read 4486 times)

0 Members and 1 Guest are viewing this topic.

FellippeHeitor

  • Guest
Sine Wave
« on: May 04, 2018, 01:51:54 pm »
Translated and adapted from javascript/p5.js: https://p5js.org/examples/math-sine-wave.html

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

Code: QB64: [Select]
  1. DIM SHARED xspacing, w, theta, amplitude, period, dx
  2. xspacing = 16
  3. theta = 0.0
  4. amplitude = 75.0
  5. period = 500.0
  6.  
  7.     y AS SINGLE
  8.     a AS SINGLE
  9.  
  10. SCREEN _NEWIMAGE(710, 400, 32)
  11. w = _WIDTH + 8
  12. dx = (_PI(2) / period) * xspacing
  13. DIM SHARED yvalues(INT(w / xspacing)) AS t
  14.  
  15. FOR i = 1 TO UBOUND(yvalues)
  16.     yvalues(i).a = yvalues(i - 1).a + 1
  17.  
  18.     CLS
  19.     calcWave
  20.     renderWave
  21.     _DISPLAY
  22.     _LIMIT 60
  23.  
  24. SUB calcWave
  25.     '// Increment theta (try different values for
  26.     '// 'angular velocity' here)
  27.     theta = theta + 0.02
  28.  
  29.     '// For every x value, calculate a y value with sine function
  30.     x = theta
  31.     FOR i = 0 TO UBOUND(yvalues)
  32.         yvalues(i).y = _HEIGHT / 2 + SIN(x) * amplitude
  33.         yvalues(i).a = yvalues(i).a + .1
  34.         x = x + dx
  35.     NEXT
  36.  
  37. SUB renderWave
  38.     '// A simple way to draw the wave with an ellipse at each location
  39.     FOR x = 0 TO UBOUND(yvalues)
  40.         c~& = hsb(yvalues(x).a MOD 360, 127, 127, 127)
  41.         CircleFill x * xspacing, height / 2 + yvalues(x).y, 50, c~&
  42.     NEXT
  43.  
  44. FUNCTION hsb~& (__H AS _FLOAT, __S AS _FLOAT, __B AS _FLOAT, A AS _FLOAT)
  45.     DIM H AS _FLOAT, S AS _FLOAT, B AS _FLOAT
  46.  
  47.     H = map(__H, 0, 255, 0, 360)
  48.     S = map(__S, 0, 255, 0, 1)
  49.     B = map(__B, 0, 255, 0, 1)
  50.  
  51.     IF S = 0 THEN
  52.         hsb~& = _RGBA32(B * 255, B * 255, B * 255, A)
  53.         EXIT FUNCTION
  54.     END IF
  55.  
  56.     DIM fmx AS _FLOAT, fmn AS _FLOAT
  57.     DIM fmd AS _FLOAT, iSextant AS INTEGER
  58.     DIM imx AS INTEGER, imd AS INTEGER, imn AS INTEGER
  59.  
  60.     IF B > .5 THEN
  61.         fmx = B - (B * S) + S
  62.         fmn = B + (B * S) - S
  63.     ELSE
  64.         fmx = B + (B * S)
  65.         fmn = B - (B * S)
  66.     END IF
  67.  
  68.     iSextant = INT(H / 60)
  69.  
  70.     IF H >= 300 THEN
  71.         H = H - 360
  72.     END IF
  73.  
  74.     H = H / 60
  75.     H = H - (2 * INT(((iSextant + 1) MOD 6) / 2))
  76.  
  77.     IF iSextant MOD 2 = 0 THEN
  78.         fmd = (H * (fmx - fmn)) + fmn
  79.     ELSE
  80.         fmd = fmn - (H * (fmx - fmn))
  81.     END IF
  82.  
  83.     imx = _ROUND(fmx * 255)
  84.     imd = _ROUND(fmd * 255)
  85.     imn = _ROUND(fmn * 255)
  86.  
  87.     SELECT CASE INT(iSextant)
  88.         CASE 1
  89.             hsb~& = _RGBA32(imd, imx, imn, A)
  90.         CASE 2
  91.             hsb~& = _RGBA32(imn, imx, imd, A)
  92.         CASE 3
  93.             hsb~& = _RGBA32(imn, imd, imx, A)
  94.         CASE 4
  95.             hsb~& = _RGBA32(imd, imn, imx, A)
  96.         CASE 5
  97.             hsb~& = _RGBA32(imx, imn, imd, A)
  98.         CASE ELSE
  99.             hsb~& = _RGBA32(imx, imd, imn, A)
  100.     END SELECT
  101.  
  102.  
  103. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  104.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  105.  
  106. SUB CircleFill (x AS LONG, y AS LONG, R AS LONG, C AS _UNSIGNED LONG)
  107.     x0 = R
  108.     y0 = 0
  109.     e = 0
  110.     DO WHILE y0 < x0
  111.         IF e <= 0 THEN
  112.             y0 = y0 + 1
  113.             LINE (x - x0, y + y0)-(x + x0, y + y0), C, BF
  114.             LINE (x - x0, y - y0)-(x + x0, y - y0), C, BF
  115.             e = e + 2 * y0
  116.         ELSE
  117.             LINE (x - y0, y - x0)-(x + y0, y - x0), C, BF
  118.             LINE (x - y0, y + x0)-(x + y0, y + x0), C, BF
  119.             x0 = x0 - 1
  120.             e = e - 2 * x0
  121.         END IF
  122.     LOOP
  123.     LINE (x - R, y)-(x + R, y), C, BF
  124.  
sinwave.png
* sinwave.png (Filesize: 62.94 KB, Dimensions: 716x428, Views: 426)
« Last Edit: May 04, 2018, 01:59:04 pm by FellippeHeitor »