User:Thierry Dugnolle/Python/Pythagore and the origin of harmony: the perfect fifth

From Wikipedia, the free encyclopedia

The perfect fifth with two strings

main.py[edit]

# 2023, September 21. Version 230921.

# 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 Scalar1DfieldDrawer import aScalar1DfieldDrawer
from Scalar1Dfield import aNewGaussian1Dfield, aNewSinus1Dfield
from Scalar2Dfield import productField
from Vector2D import aNew2Dvector

print ("Mathematical painter")

# The drawer
TheDrawer = aScalar1DfieldDrawer()

# The canvas:
TheCanvasStyle= "black on white" # "color", "black on white" or "white on black"
TheWidth = 628 # number of pixels
TheHeight = 300 # number of pixels
TheLengthUnit = 200 # 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.0*TheRealWidth, 0.5*TheRealHeight)

# The paintbrush:
TheDrawer.paintbrush.haloHalfWidth = 0.02 # (The halo half width * the length unit) is the
# number of pixels in the half width of the halo.
TheDrawer.paintbrush.powerOfSquaredCos = 1
TheDrawer.paintbrush.totalHalfWidth = TheDrawer.paintbrush.haloHalfWidth

# The field:
ThePixelNumberOfPoints = 1 # number of points in the field in the width of a pixel
TheNumberOfPoints = TheWidth*ThePixelNumberOfPoints
k = 1
c = 1.0 # The speed of sound
omega = k*c # Pythagore's equation
TheField = aNewSinus1Dfield(TheNumberOfPoints, math.pi, k, 0).times(0.4)

# The animation:
TheNumberOfImages = math.floor(400/omega)
dt = 2*2*math.pi/omega/TheNumberOfImages
interval = 3/2
for i in range(TheNumberOfImages):
    print("image", i)
    TheDrawer.takesAnewCanvas(TheWidth, TheHeight, TheLengthUnit, TheCanvasStyle, TheCanvasColor)
    t = i*dt
    TheDrawer.drawsAreal1Dfield(TheField.times(math.cos(omega*t)).verticallyShifted(1.02), ThePixelNumberOfPoints)
    for j in range(math.floor(TheField.width/ThePixelNumberOfPoints - 1)):
        xShift = TheDrawer.canvas.imageCenter.x + 2/3*TheDrawer.canvas.halfRealWidth
        TheFirstPoint = aNew2Dvector((j + 0.5)*ThePixelNumberOfPoints*TheField.dx/interval - xShift,
                                     TheField.value[j*ThePixelNumberOfPoints]*math.cos(interval*omega*t)/interval + 0.32)
        TheSecondPoint = aNew2Dvector((j + 1.5)*ThePixelNumberOfPoints*TheField.dx/interval - xShift,
                                      TheField.value[(j + 1)*ThePixelNumberOfPoints]*math.cos(interval*omega*t)/interval + 0.32)
        TheDrawer.drawsAsegment(TheFirstPoint, TheSecondPoint)
    TheDrawer.givesApainting("PerfectFifth" + str(100 + i) + ".png")

print("Good bye")

Other files[edit]

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