wavez_.py

Created by chick-chicky

Created on October 01, 2022

1.24 KB

A small script for graphing 2d standing waves


from kandinsky import *
from math import *

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

print('''n (3): n parameter
m (4): m parameter
mode: 
  0: boolean graph
    k (0.05): threshold
  1: magnitude graph
    g (100): gain''')

n = float(input('n: ') or 3)
m = float(input('m: ') or 4)
mode = input('mode: ') or '0'

if mode == '0':
  # settings
  k = float(input('k: ') or 0.05)
  
  # 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
if mode == '1':
  # settings
  gain = float(input('g: ') or 100)
  
  # 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
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.