j0h

wavetext2

j0h
Jan 10th, 2026
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import math
  3. import sys
  4. from time import sleep
  5. """
  6. Prints the input string modulated along a wave.
  7. sin and cos for now.
  8. The string characters are placed at the wave positions nearest aproximation.
  9.  
  10. FUNCTIONS
  11. acos
  12. acosh
  13. asin
  14. asinh
  15. atan
  16. atan2
  17. atanh
  18. comb
  19. cos
  20. cosh
  21. """
  22.  
  23. def text_wave(
  24. text: str,
  25. amplitude: int = 5,
  26. period_chars: int = 20,
  27. center_char: str = " ",
  28. empty_char: str = " ",
  29. *,
  30. compact: bool = False,
  31. period_sep: str = " ", # inserted between each wave period (chunk)
  32. drop_empty_rows: bool = True, # in compact mode, skip rows with no emitted chars
  33. ) -> None:
  34. if not text:
  35. return
  36. if period_chars <= 0:
  37. raise ValueError("period_chars must be >= 1")
  38. if amplitude < 0:
  39. raise ValueError("amplitude must be >= 0")
  40.  
  41. # Compute vertical offsets per character
  42. positions = []
  43. for i in range(len(text)):
  44. phase = 2 * math.pi * i / period_chars
  45. #offset = round(amplitude * math.cos(phase))
  46. #offset = round(amplitude * math.sin(phase))
  47. #offset = round(amplitude * math.tan(phase)) # tan is out for now.
  48. offset = round(amplitude * math.acosh(phase))
  49. positions.append(offset)
  50.  
  51. min_row = min(positions)
  52. max_row = max(positions)
  53. total_rows = max_row - min_row + 1
  54. center_row = -min_row # row index where offset=0 lives
  55.  
  56. if not compact:
  57. # Original "aesthetic" grid mode
  58. grid = [[empty_char] * len(text) for _ in range(total_rows)]
  59. for col in range(len(text)):
  60. grid[center_row][col] = center_char
  61. for col, ch in enumerate(text):
  62. row = center_row + positions[col]
  63. grid[row][col] = ch
  64. for row in grid:
  65. print("".join(row))
  66. return
  67.  
  68. # --- Compact mode ---
  69. # Build output as chunked "period blocks": each chunk is period_chars wide in *time*,
  70. # but we emit only characters that land on each row (no filler).
  71. out_rows = [""] * total_rows
  72.  
  73. n = len(text)
  74. first_chunk = True
  75. for start in range(0, n, period_chars):
  76. end = min(start + period_chars, n)
  77.  
  78. # Collect emitted chars per row for this chunk
  79. chunk_rows = [""] * total_rows
  80. for i in range(start, end):
  81. r = center_row + positions[i]
  82. chunk_rows[r] += text[i] # keep original spaces if they occur in the input
  83.  
  84. # Append chunk to overall output, inserting period_sep between chunks
  85. if not first_chunk:
  86. for r in range(total_rows):
  87. out_rows[r] += period_sep
  88. for r in range(total_rows):
  89. out_rows[r] += chunk_rows[r]
  90.  
  91. first_chunk = False
  92.  
  93. # Print (optionally skipping rows that had no emitted characters at all)
  94. for s in out_rows:
  95. if drop_empty_rows and s == "":
  96. continue
  97. print(s)
  98.  
  99.  
  100. #value = "Merry Christmas and Happy New Year"
  101. value = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghiklmnopqrstuvwxyz"
  102. #text_wave(value, amplitude=6, period_chars=24)
  103.  
  104. if len(sys.argv) != 2:
  105. print("Usage:", sys.argv[0], "<filename>")
  106. print("Encoding example where: \nAmplitide=5 \nPeriod=45 \ncompact=True\n")
  107. print(f"value:{value}")
  108. line = value
  109. text_wave(line, amplitude=5, period_chars=45, compact=True, period_sep=" ")
  110. print("\nEncoding example where: \nAmplitide=5 \nPeriod=45 \ncompact=Flase")
  111. text_wave(line, amplitude=5, period_chars=45, compact=False, period_sep=" ")
  112. sys.exit(1)
  113.  
  114. try:
  115. with open(sys.argv[1], encoding='utf-8') as f:
  116. for line in f:
  117. line = line.rstrip()
  118. if line:
  119. text_wave(line, amplitude=5, period_chars=45, compact=True, period_sep=" ")
  120. #text_wave(line, amplitude=5, period_chars=45)
  121. print()
  122. #text_wave(value, amplitude=6, period_chars=60)
  123. except FileNotFoundError:
  124. print(f"File not found: {sys.argv[1]}")
  125. sys.exit(1)
  126.  
  127. '''
  128. For a string, apply trigonometric functions using text as the line.
  129. define length or N reps
  130. sine() cos() Tan()
  131. amplitude in N chars above a start line.
  132. start line is Amplitude N chars.
  133.  
  134. Nwaves = wave-number
  135. K=2*pi/Nwaves
  136.  
  137. Wave W= Amplitude * sin(2*pi/wave-length)
  138. Wave W= Amplitude * sin(k*x)
  139.  
  140. lets see, what issues will we be havin....
  141. chars as pixels are rectangular, which wraps space time in character space by the ratio of h/w
  142.  
  143. in order to print a string, you need to know the amplitude in characters.
  144. The mapping of the characts to line up nicely must offset at not less then the previous offset.
  145. or in other words, if you treat the string as characters across lines, you must also track position.
  146. ah but I dont want to do that. N char should itterate accross the space.
  147. dead simple, in a sawtooth wave.
  148. H_point1 = A-0,N-char
  149. H_point2 = A-1,N-char ...
  150. until A=0
  151.  
  152.  
  153.  
  154. '''
  155.  
Advertisement
Add Comment
Please, Sign In to add comment