Skip to content

Instantly share code, notes, and snippets.

@chrisws
Created January 12, 2022 09:24
Show Gist options
  • Save chrisws/63c2a6c2ebfd1bea18552e208a3f3383 to your computer and use it in GitHub Desktop.
Save chrisws/63c2a6c2ebfd1bea18552e208a3f3383 to your computer and use it in GitHub Desktop.
Hitomezashi Stitch Patterns - Numberphile (Youtube)
rem see https://youtu.be/JbfhzlMk2eY
const spacing = 18
const lineColor = rgb(120, 121, 122)
txt = "this is my secret message"
alphabet = {}
word_index = 0
bit_index = 0
sub init
local i = 0
local c
for c in " abcdefghijklmnopqrstuvwxzy1234567890'.,!/-:"
alphabet[c] = i
i++
next c
end
func pop_bit
local result = false
local w, v
if (word_index < len(txt)) then
w = mid(txt, word_index + 1, 1)
v = alphabet[w]
result = iff((v & pow(2, bit_index)) > 0, true, false)
endif
bit_index = (bit_index + 1) % 7
word_index = iff(bit_index = 0, word_index + 1, word_index)
return result
end
func do_stich
return pop_bit
end
sub stich_line(x0, y0, x1, y1)
if (x0 > x1) then swap x0, x1
local n
local d = x1 - x0
local m = (y1 - y0) / max(1, x1 - x0)
local lx0 = x0
local ly0 = y0
local lx1 = x0 + spacing
local ly1 = y0 + spacing * m
local stich = do_stich()
for n = 0 to d - spacing step spacing
if (stich) then
line lx0, ly0, lx1, ly1, lineColor
stich = false
else
stich = true
endif
lx0 = lx1
ly0 = ly1
lx1 += spacing
ly1 += spacing * m
next
end
func get_nodes(x0, y0, x1, y1)
local nodes = []
local m = (y1 - y0) / max(1, x1 - x0)
local t = atan(m)
local h = sqr(pow(y1 - y0, 2) + pow(x1 - x0, 2))
local n, x, y
for n = 0 to h step spacing
x = x0 + cos(t) * n
y = y0 + sin(t) * n
nodes << [x, y]
next
return nodes
end
func reverse(a)
local result = []
for el in a
insert result, 0, el
next
return result
end
sub do_triangle(x0, y0, x1, y1, x2, y2)
local a1 = get_nodes(x0, y0, x1, y1)
local a2 = get_nodes(x0, y0, x2, y2)
local a3 = reverse(get_nodes(x1, y1, x2, y2))
local a4 = reverse(get_nodes(x0, y0, x2, y2))
local i
for i = 0 to min(len(a1), len(a2)) - 1
stich_line(a1[i][0], a1[i][1], a2[i][0], a2[i][1])
next
for i = 0 to min(len(a1), len(a3)) - 1
stich_line(a1[i][0], a1[i][1], a3[i][0], a3[i][1])
next
for i = 0 to min(len(a4), len(a3)) - 1
stich_line(a4[i][0], a4[i][1], a3[i][0], a3[i][1])
next
drawpoly a1 color lineColor
drawpoly a2 color lineColor
drawpoly a3 color lineColor
end
sub do_rectangle(x0, y0, x1, y1)
local x, y
local stich
for y = y0 to y1 step spacing
stich = do_stich()
for x = x0 to x1 - spacing step spacing
if (stich) then
line x, y, x + spacing, y, lineColor
stich = false
else
stich = true
endif
next
next
for x = x0 to x1 step spacing
stich = do_stich()
for y = y0 to y1 - spacing step spacing
if (stich) then
line x, y, x, y + spacing, lineColor
stich = false
else
stich = true
endif
next
next
end
init
rem do_rectangle(0, 0, xmax, ymax)
do_triangle(0, ymax, xmax/2, 0, xmax, ymax)
@chrisws
Copy link
Author

chrisws commented Jan 12, 2022

sbasic_dump_11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment