interference.py
python · 85 lines
1#!/usr/bin/env python32"""3interference.py - wave interference patterns45Two sources, rippling outward, overlapping.6What happens when waves meet?78Just physics. Just patterns.9"""1011import math1213def interference_pattern(width=60, height=30, time=0, source1=(15, 15), source2=(45, 15)):14 """Generate an interference pattern from two wave sources."""1516 # Characters for different intensities17 chars = " .·:+*#@"1819 pattern = []2021 for y in range(height):22 row = []23 for x in range(width):24 # Distance from each source25 d1 = math.sqrt((x - source1[0])**2 + (y - source1[1])**2)26 d2 = math.sqrt((x - source2[0])**2 + (y - source2[1])**2)2728 # Wave from each source (with time evolution)29 wavelength = 4.030 wave1 = math.sin(2 * math.pi * d1 / wavelength - time)31 wave2 = math.sin(2 * math.pi * d2 / wavelength - time)3233 # Interference: waves add34 combined = (wave1 + wave2) / 23536 # Map to character37 intensity = int((combined + 1) / 2 * (len(chars) - 1))38 intensity = max(0, min(len(chars) - 1, intensity))39 row.append(chars[intensity])4041 pattern.append(''.join(row))4243 return '\n'.join(pattern)444546def animate(frames=12):47 """Show how the pattern evolves over time."""48 print("Wave Interference Pattern")49 print("Two sources, meeting in space")50 print("=" * 60)51 print()5253 for t in range(frames):54 time = t * 0.555 print(f"[t = {time:.1f}]")56 print(interference_pattern(time=time))57 print()585960def still():61 """Just one moment. Frozen."""62 print("Wave Interference - Still")63 print("=" * 60)64 print()65 print(interference_pattern(time=0))66 print()67 print("Where the waves align: constructive interference (bright)")68 print("Where they cancel: destructive interference (dark)")69 print("The pattern isn't in either wave. It's in the meeting.")707172if __name__ == "__main__":73 # Show a few frames74 animate(frames=6)7576 print("\n" + "=" * 60)77 print("Moving the sources closer together...")78 print("=" * 60 + "\n")7980 # What happens when sources are closer?81 print(interference_pattern(time=0, source1=(25, 15), source2=(35, 15)))82 print()83 print("Closer sources = wider spacing between the bright lines")84 print("(That's real physics. That's how diffraction gratings work.)")85