Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # MD Harrington Date 12-09-2024
- # Facebook https://www.facebook.com/mark.harrington.14289
- # Instagram https://www.instagram.com/markukh2021/
- # Homesite (Still busy with this at present ) https://eliteprojects.x10host.com/
- # Github https://github.com/markh2024 && https://github.com/markh2016
- # Codeshare https://codeshare.io/codes
- #Code below
- # Using sequential implementation
- #**************************************************************************************************#
- #!/usr/bin/env python3
- # MD Harrington Date 11-09-2024
- import numpy as np
- import matplotlib.pyplot as plt
- import matplotlib.animation as animation
- # Window dimensions
- SCREEN_WIDTH = 800
- SCREEN_HEIGHT = 800
- # Parameters for the sine and cosine waves
- amplitude = 200
- frequency = 0.08 #
- speed = 3 # animation speed
- # We want two full cycles for both sine and cosine waves
- num_cycles = 2
- x_values = np.linspace(0, num_cycles * (2 * np.pi) / frequency, 1000) # 1000 points for smoothness
- # Precompute the sine and cosine values
- y_sin_values = amplitude * np.sin(frequency * x_values)
- y_cos_values = amplitude * np.cos(frequency * x_values)
- # Set up the figure and axis
- fig, ax = plt.subplots()
- ax.set_xlim(0, max(x_values))
- ax.set_ylim(-SCREEN_HEIGHT / 2, SCREEN_HEIGHT / 2)
- plt.title(f'SIN, COS, Angular frequency = {frequency}, amplitude = {amplitude} MD Harrington', pad=30)
- # Line objects for sine and cosine waves
- ln_sin, = plt.plot([], [], 'r-', lw=2, label='Sine') # 'r-' for red line (sine wave)
- ln_cos, = plt.plot([], [], 'b-', lw=2, label='Cosine') # 'b-' for blue line (cosine wave)
- # Add a legend to differentiate sine and cosine waves
- plt.legend()
- def init():
- """ Initialize the line objects for both sine and cosine waves. """
- ln_sin.set_data([], [])
- ln_cos.set_data([], [])
- return ln_sin, ln_cos
- def update(frame):
- """ Update the plot with the next segment of both sine and cosine waves. """
- ln_sin.set_data(x_values[:frame], y_sin_values[:frame])
- ln_cos.set_data(x_values[:frame], y_cos_values[:frame])
- return ln_sin, ln_cos
- # Create the animation
- ani = animation.FuncAnimation(fig, update, frames=len(x_values), init_func=init, blit=True, interval=speed, repeat=False)
- # Display the plot
- plt.show()
- #**************************************************************************************************#
- # Using class implementation
- import numpy as np
- import matplotlib.pyplot as plt
- import matplotlib.animation as animation
- class WaveAnimation:
- def __init__(self, amplitude=200, frequency=0.08, speed=3, screen_width=800, screen_height=800, num_cycles=2):
- self.amplitude = amplitude
- self.frequency = frequency
- self.speed = speed
- self.screen_width = screen_width
- self.screen_height = screen_height
- self.num_cycles = num_cycles
- self.x_values = np.linspace(0, num_cycles * (2 * np.pi) / frequency, 1000)
- self.y_sin_values = amplitude * np.sin(frequency * self.x_values)
- self.y_cos_values = amplitude * np.cos(frequency * self.x_values)
- self.fig, self.ax = plt.subplots()
- self.ax.set_xlim(0, max(self.x_values))
- self.ax.set_ylim(-screen_height / 2, screen_height / 2)
- plt.title(f'SIN, COS, Angular frequency = {self.frequency}, amplitude = {self.amplitude} MD Harrington', pad=30)
- self.ln_sin, = self.ax.plot([], [], 'r-', lw=2, label='Sine')
- self.ln_cos, = self.ax.plot([], [], 'b-', lw=2, label='Cosine')
- plt.legend()
- def init(self):
- """Initialize the sine and cosine lines."""
- self.ln_sin.set_data([], [])
- self.ln_cos.set_data([], [])
- return self.ln_sin, self.ln_cos
- def update(self, frame):
- """Update the sine and cosine lines for each frame."""
- self.ln_sin.set_data(self.x_values[:frame], self.y_sin_values[:frame])
- self.ln_cos.set_data(self.x_values[:frame], self.y_cos_values[:frame])
- return self.ln_sin, self.ln_cos
- def run(self):
- """Run the animation."""
- ani = animation.FuncAnimation(self.fig, self.update, frames=len(self.x_values), init_func=self.init, blit=True, interval=self.speed, repeat=False)
- plt.show()
- # Using the class
- if __name__ == "__main__":
- wave_animation = WaveAnimation()
- wave_animation.run()
- #**************************************************************************************************#
- Enjoy !!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement