wavez.py

Created by chick-chicky

Created on October 01, 2022

3.08 KB

A small script for graphing 2d standing waves nodes and anti-nodes

https://www.desmos.com/calculator/a7puiruynb for playing around with mode 0 more easily


# libs
from kandinsky import *
from math import *

# constants
WIDTH,HEIGHT = 320,240
scalex,scaley = 100,100
precision = 1

# default preset
mode = 0
n    = 3
m    = 4
k    = 0.01
g    = 100

# preset loading
preset = input('preset: ')
if preset != '':
  v = ''
  i = 0
  for c in preset:
    if preset[0] == ',':
      if c == ',':
        if v:
          v = float(v)
          if i==1: mode=int(v)
          if i==2: n=v
          if i==3: m=v
          if i==4: k=v
          if i==5: g=v
        v = ''
        i += 1
      else:
        v += c
    else:
      if c in 'Mnmkg':
        v = float(v)
        if c=='M':mode=int(v)
        if c=='n':n=v
        if c=='m':m=v
        if c=='k':k=v
        if c=='g':g=v
        v = ''
      else:
        v += c

preset = bool(len(preset))

if not preset:
  print('''
modes
 0: anti-node graph
 1: node magnitude graph
 2: both
  ''')
  
  mode = input('mode: ') or mode
  
mode = str(mode)

if mode == '0':
  if not preset:
    # values descriptions
    print(''' n: n parameter
 m: m parameter
 k: threshold''')
    
    # settings
    n = float(input('n: ') or n)
    m = float(input('m: ') or m)
    k = float(input('k: ') or k)
  
  # fn
  def fn(x,y):
    return abs(sin(n*pi*x)*sin(m*pi*y) - sin(m*pi*x)*sin(n*pi*y)) < k
    
  # grpahing
  rx = -WIDTH/2
  while rx < WIDTH/2:
    ry = -HEIGHT/2
    while ry < HEIGHT/2:
      x,y = rx/scalex,ry/scaley*-1    
      if fn(x,y):
        set_pixel(round(WIDTH/2+rx),round(HEIGHT/2+ry),(255,0,0))
      ry += precision
    rx += precision
elif mode == '1':
  if not preset:
    # values descriptions
    print(''' n: n parameter
 m: m parameter
 g: gain''')
  
    # settings
    n = float(input('n: ') or n)
    m = float(input('m: ') or m)
    g = float(input('g: ') or g)
    
  gain = g
    
  # fn
  def fn(x,y):
    return sin(n*pi*x)*sin(m*pi*y) - sin(m*pi*x)*sin(n*pi*y) *gain
    
  # graphing
  rx = -WIDTH/2
  while rx < WIDTH/2:
    ry = -HEIGHT/2
    while ry < HEIGHT/2:
      x,y = rx/scalex,ry/scaley*-1    
      r = 128-fn(x,y)*2
      b = 128+fn(x,y)*2
      set_pixel(round(WIDTH/2+rx),round(HEIGHT/2+ry),(r,0,b))
      ry += precision
    rx += precision
elif mode == "2":
  if not preset:
    # values descriptions
    print(''' n: n parameter
 m: m parameter
 k: threshold
 g: gain''')
  
    # settings
    n = float(input('n: ') or n)
    m = float(input('m: ') or m)
    k = float(input('k: ') or k)
    g = float(input('g: ') or g)
    
  gain = g
  
  # fn
  def fn(x,y):
    return sin(n*pi*x)*sin(m*pi*y) - sin(m*pi*x)*sin(n*pi*y) *gain
  def fn2(x,y):
    return abs(sin(n*pi*x)*sin(m*pi*y) - sin(m*pi*x)*sin(n*pi*y))<k
    
  # graphing
  rx = -WIDTH/2
  while rx < WIDTH/2:
    ry = -HEIGHT/2
    while ry < HEIGHT/2:
      x,y = rx/scalex,ry/scaley*-1    
      if fn2(x,y):
        set_pixel(round(WIDTH/2+rx),round(HEIGHT/2+ry),((abs(fn(x,y))-k)/k**4,255,(abs(fn(x,y))-k)/k**4))
      else:
        r = 128-fn(x,y)*2
        b = 128+fn(x,y)*2
        set_pixel(round(WIDTH/2+rx),round(HEIGHT/2+ry),(r,0,b))
      ry += precision
    rx += precision
else:
  print('Invalid mode \'{}\''.format(mode))

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.