Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import math
- from typing import List, Tuple
- '''
- Ok i gotta think about this more or something.
- https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
- '''
- def swta(
- raw_text: str,
- amplitude: int = 5,
- period: int = 48,
- center_char: str = " ",
- empty: str = " ",
- oversample: int = 8,
- compact: bool = False,
- show_text_spaces_as: str | None = " ", # set None to keep literal spaces (but compact will remove them)
- ) -> None:
- import math
- if not raw_text:
- print("(empty string)")
- return
- # If we plan to remove ALL background spaces, make literal spaces in the text visible first
- if compact and show_text_spaces_as is not None:
- raw_text = raw_text.replace(" ", show_text_spaces_as)
- n = len(raw_text)
- if n == 0:
- return
- k = 2 * math.pi / (period * oversample)
- sub_steps = max(n * oversample * 2, period * oversample * 2)
- y_sub = [amplitude * math.sin(k * i) for i in range(sub_steps + 1)]
- s = [0.0]
- for i in range(sub_steps):
- dy = y_sub[i + 1] - y_sub[i]
- s.append(s[-1] + math.sqrt(1.0 + dy * dy))
- total_len = s[-1]
- if total_len == 0:
- print(raw_text)
- return
- targets = [total_len * (i / (n - 1)) if n > 1 else 0.0 for i in range(n)]
- idxs = []
- j = 0
- for t in targets:
- while j < len(s) - 1 and s[j] < t:
- j += 1
- idxs.append(j)
- cols = []
- last_c = -1
- for sub_i in idxs:
- c = int(round(sub_i / oversample))
- if c <= last_c:
- c = last_c + 1
- cols.append(c)
- last_c = c
- width = cols[-1] + 1
- offsets = [int(round(y_sub[sub_i])) for sub_i in idxs]
- min_y = min(offsets)
- max_y = max(offsets)
- height = max_y - min_y + 1
- zero_row = -min_y
- grid = [[empty for _ in range(width)] for _ in range(height)]
- if center_char != " ":
- for x in range(width):
- grid[zero_row][x] = center_char
- for ch, x, off in zip(raw_text, cols, offsets):
- r = zero_row - off
- if 0 <= r < height and 0 <= x < width:
- grid[r][x] = ch
- for row in grid:
- line = "".join(row)
- if compact:
- # Remove ALL background filler characters
- line = line.replace(empty, "")
- else:
- # Normal: keep spacing, but don't print trailing background
- line = line.rstrip(empty)
- if line:
- print(line)
- print()
- if __name__ == "__main__":
- text1 = r"Its a shame, I have nothing to say, when I just want to warp my words"
- swta(text1, amplitude=5, period=48, center_char=" ")
- swta(text1, amplitude=5, period=48, center_char=" ", compact=True)
Advertisement
Add Comment
Please, Sign In to add comment