User:Thierry Dugnolle/Python/Stationary wave in a square

Stationary wave in a square

main.py

edit
# 2023, September 21. Version 230921.

# The painter paints an animation of a stationary wave in a square.

# The drawer has a canvas which is defined by a matrix width*height of pixels.
# The canvas is projected on a plane: the vector image.
# The image center is the position of the center of the canvas in the vector image.
# The length unit is the number of pixels of a unit of length in the vector image.


import math
from Scalar2DfieldPainter import aScalar2DfieldPainter
from Scalar1Dfield import aNewGaussian1Dfield, aNewSinus1Dfield
from Scalar2Dfield import productField
from Vector2D import aNew2Dvector

print ("Mathematical painter")

# The drawer
TheDrawer = aScalar2DfieldPainter()

# The canvas:
TheCanvasStyle= "color" # "color", "black on white" or "white on black"
TheWidth = 600 # number of pixels
TheHeight = TheWidth # number of pixels
TheLengthUnit = 100 # number of pixels
TheRealWidth = TheWidth/TheLengthUnit
TheRealHeight = TheHeight/TheLengthUnit
TheCanvasColor = (0, 0, 0) # (red, green, blue) here black
TheDrawer.takesAnewCanvas(TheWidth, TheHeight, TheLengthUnit, TheCanvasStyle, TheCanvasColor)
TheDrawer.canvas.imageCenter = aNew2Dvector( 0.5*TheRealWidth, 0.5*TheRealHeight)

# The palette:
def ThePalette(shade):
    return TheDrawer.palette.depthHeat(shade)

# The field:
ThePixelNumberOfPoints = 1 # number of points in the field in the width of a pixel
TheNumberOfPoints = TheWidth*ThePixelNumberOfPoints
kx = 2.0
ky = 2.0
k = math.sqrt(kx*kx + ky*ky)
c = 1.0 # The speed of sound
omega = k*c # Pythagore's equation
omega0 = c*math.sqrt(2)
TheXfield = aNewSinus1Dfield(TheNumberOfPoints, math.pi, kx, 0)
TheYfield = aNewSinus1Dfield(TheNumberOfPoints, math.pi, ky, 0)
TheField = productField(TheXfield, TheYfield)

# The animation:
TheNumberOfImages = math.floor(300*omega0/omega)
dt = 2*math.pi/omega/TheNumberOfImages
for i in range(TheNumberOfImages):
    print("image", i)
    TheDrawer.takesAnewCanvas(TheWidth, TheHeight, TheLengthUnit, TheCanvasStyle, TheCanvasColor)
    t = i*dt
    TheDrawer.paintsAreal2Dfield(TheField.times(math.cos(omega*t)), ThePixelNumberOfPoints, ThePalette)
    TheDrawer.givesApainting("Animation/Sound" + str(100 + i) + ".png")

print("Good bye")

Other files

edit

This program requires the following additional files: Painter.py, Vector2D.py, Scalar2DfieldPainter.py, Scalar2Dfield.py and Palette.py. They are all on this page : User:Thierry Dugnolle/Python/Mathematical painter.