Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import math
- import sys
- from time import sleep
- """
- Prints the input string modulated along a wave.
- sin and cos for now.
- The string characters are placed at the wave positions nearest aproximation.
- FUNCTIONS
- acos
- acosh
- asin
- asinh
- atan
- atan2
- atanh
- comb
- cos
- cosh
- """
- def text_wave(
- text: str,
- amplitude: int = 5,
- period_chars: int = 20,
- center_char: str = " ",
- empty_char: str = " ",
- *,
- compact: bool = False,
- period_sep: str = " ", # inserted between each wave period (chunk)
- drop_empty_rows: bool = True, # in compact mode, skip rows with no emitted chars
- ) -> None:
- if not text:
- return
- if period_chars <= 0:
- raise ValueError("period_chars must be >= 1")
- if amplitude < 0:
- raise ValueError("amplitude must be >= 0")
- # Compute vertical offsets per character
- positions = []
- for i in range(len(text)):
- phase = 2 * math.pi * i / period_chars
- #offset = round(amplitude * math.cos(phase))
- #offset = round(amplitude * math.sin(phase))
- #offset = round(amplitude * math.tan(phase)) # tan is out for now.
- offset = round(amplitude * math.acosh(phase))
- positions.append(offset)
- min_row = min(positions)
- max_row = max(positions)
- total_rows = max_row - min_row + 1
- center_row = -min_row # row index where offset=0 lives
- if not compact:
- # Original "aesthetic" grid mode
- grid = [[empty_char] * len(text) for _ in range(total_rows)]
- for col in range(len(text)):
- grid[center_row][col] = center_char
- for col, ch in enumerate(text):
- row = center_row + positions[col]
- grid[row][col] = ch
- for row in grid:
- print("".join(row))
- return
- # --- Compact mode ---
- # Build output as chunked "period blocks": each chunk is period_chars wide in *time*,
- # but we emit only characters that land on each row (no filler).
- out_rows = [""] * total_rows
- n = len(text)
- first_chunk = True
- for start in range(0, n, period_chars):
- end = min(start + period_chars, n)
- # Collect emitted chars per row for this chunk
- chunk_rows = [""] * total_rows
- for i in range(start, end):
- r = center_row + positions[i]
- chunk_rows[r] += text[i] # keep original spaces if they occur in the input
- # Append chunk to overall output, inserting period_sep between chunks
- if not first_chunk:
- for r in range(total_rows):
- out_rows[r] += period_sep
- for r in range(total_rows):
- out_rows[r] += chunk_rows[r]
- first_chunk = False
- # Print (optionally skipping rows that had no emitted characters at all)
- for s in out_rows:
- if drop_empty_rows and s == "":
- continue
- print(s)
- #value = "Merry Christmas and Happy New Year"
- value = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghiklmnopqrstuvwxyz"
- #text_wave(value, amplitude=6, period_chars=24)
- if len(sys.argv) != 2:
- print("Usage:", sys.argv[0], "<filename>")
- print("Encoding example where: \nAmplitide=5 \nPeriod=45 \ncompact=True\n")
- print(f"value:{value}")
- line = value
- text_wave(line, amplitude=5, period_chars=45, compact=True, period_sep=" ")
- print("\nEncoding example where: \nAmplitide=5 \nPeriod=45 \ncompact=Flase")
- text_wave(line, amplitude=5, period_chars=45, compact=False, period_sep=" ")
- sys.exit(1)
- try:
- with open(sys.argv[1], encoding='utf-8') as f:
- for line in f:
- line = line.rstrip()
- if line:
- text_wave(line, amplitude=5, period_chars=45, compact=True, period_sep=" ")
- #text_wave(line, amplitude=5, period_chars=45)
- print()
- #text_wave(value, amplitude=6, period_chars=60)
- except FileNotFoundError:
- print(f"File not found: {sys.argv[1]}")
- sys.exit(1)
- '''
- For a string, apply trigonometric functions using text as the line.
- define length or N reps
- sine() cos() Tan()
- amplitude in N chars above a start line.
- start line is Amplitude N chars.
- Nwaves = wave-number
- K=2*pi/Nwaves
- Wave W= Amplitude * sin(2*pi/wave-length)
- Wave W= Amplitude * sin(k*x)
- lets see, what issues will we be havin....
- chars as pixels are rectangular, which wraps space time in character space by the ratio of h/w
- in order to print a string, you need to know the amplitude in characters.
- The mapping of the characts to line up nicely must offset at not less then the previous offset.
- or in other words, if you treat the string as characters across lines, you must also track position.
- ah but I dont want to do that. N char should itterate accross the space.
- dead simple, in a sawtooth wave.
- H_point1 = A-0,N-char
- H_point2 = A-1,N-char ...
- until A=0
- '''
Advertisement
Add Comment
Please, Sign In to add comment