Skip to content

Instantly share code, notes, and snippets.

@tommy-mor
Created November 26, 2019 19:37
Show Gist options
  • Save tommy-mor/da6e14637d5cb1cc9c8cd1d451541f9c to your computer and use it in GitHub Desktop.
Save tommy-mor/da6e14637d5cb1cc9c8cd1d451541f9c to your computer and use it in GitHub Desktop.
using Luxor
using ColorSchemes
#this. OR we could multiply amt by something based on orientation passed in. try that next
function flipDiagonalBottomRight(og, pt)
dx = pt.x - og.x
dy = pt.y - og.y
return Point(og.x + dy, og.y + dx)
end
function flipDiagonalBottomLeft(og, pt)
dx = pt.x - og.x
dy = pt.y - og.y
return Point(og.x - dy, og.y - dx)
end
function hilbert(n, origin, amt)
#move to our point, rotate at our point, then translate
#gsave maybe?
if(n == 0)
return [origin]
else
blog = origin + (0, 0)
bl = map(x->flipDiagonalBottomLeft(blog, x), hilbert(n-1, blog, amt)) #bottom left
tlog = last(bl) + (0, -amt)
tl = hilbert(n-1, tlog, amt)
trog = last(tl) + (amt, 0)
tr = hilbert(n-1, trog, amt)
brog = last(tr) + (0, amt)
br = map(x->flipDiagonalBottomRight(brog, x), hilbert(n-1, brog, amt)) #bottom left
return [bl;tl;tr;br] #all the paths concatd together
end
end
function colorgradedpathfollow(l)
setlinecap("round")
for (n,(a,b)) in enumerate(zip(1:length(l), 2:length(l)))
sethue(get(ColorSchemes.plasma, rescale(n, 0, length(l), 0, 1)))
setline(5)
#circle(l[a],2,:fill)
#circle(l[b],2,:fill)
line(l[a],l[b], :stroke)
end
end
function rgb(r,g,b)
setcolor(r/255.0,g/255.0,b/255.0)
end
function frame2(scene, t)
background("black")
setline(1)
#sethue(get(ColorSchemes.plasma, 0))
orp = O
#circle(orp, 3, :fill)
#prettypoly(hilbert(3,orp, 20),:stroke) #vertexlabels = (n, l) -> (text(string(n, " of ", l), halign=:center)))
#prettypoly(offsetpoly(h, 10), :stroke)
function shape(x1,y1,w,h,s, lines=false)::Tuple{Array{Point,1}, Array{Point,2}}
pts = [Point(x1 + x*s, y1 + y*s) for x in 0:w for y in 0:h]
l = polyfit([Point(x1, y1 + y*s) for y in 0:h], 1000)
if lines
print("\n ", t)
for xp in 0:w
a = polyfit([Point(x1 + xp*s, y1 + y*s) for y in 0:h], 1000)
l = [l a]
end
for yp in 0:h
b = polyfit([Point(x1 + x*s, y1 + yp*s) for x in 0:w], 1000)
l = [l b]
end
end
return pts, l
end
function renderlines(arr::Array{Point, 2})
_,s = size(arr)
for i in 1:s
poly(arr[:,i], :stroke)
end
end
function multpt(pt::Point)::Point
range = 14 * 25
x,y = pt
return Point(x + t*sin(x*2pi/range) - 30*cos(y*2pi/range), y + (t/100) * 30*cos((t/50)*y*x*2pi/range^2))
end
function transform(dx, dy)::Function
function app(pt::Point)::Point
x,y = pt
return Point(x + dx, y + dy)
end
return app
end
pts, lines = shape(-400,-200, 40, 40, 10, true)
#prettypoly(pts, :none, () -> circle(O, 1, :fill))
transformation = transform(10,20) multpt
switched = map(transformation, lines)
rgb(225,130,111)
renderlines(switched)
rgb(118,162,165)
h = hilbert(5, orp, 12)
rgb(118,162,165)
prettypoly(map(transformation, h), :stroke, () -> circle(O, 1, :stroke))
#prettypoly(map(transformation, offsetpoly(h, 3)), :stroke, () -> circle(O, 1, :stroke))
end
simpleanimation = Movie(1000, 1000, "animation")
animate(simpleanimation,
[Scene(simpleanimation, frame2, 1:200, easingfunction=easeinexpo),
Scene(simpleanimation, frame2, 200:1, easingfunction=easeinexpo)],
creategif=true, pathname="image.gif")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment