Skip to content

Instantly share code, notes, and snippets.

@halfdan
Created April 23, 2021 16:59
Show Gist options
  • Save halfdan/6d17338b96a0d808c93dc98c85c5718b to your computer and use it in GitHub Desktop.
Save halfdan/6d17338b96a0d808c93dc98c85c5718b to your computer and use it in GitHub Desktop.
Noisy movement of 2D objects in Julia (Pluto.jl notebook)
### A Pluto.jl notebook ###
# v0.14.1
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing
el
end
end
# ╔═╡ 68bed1a4-a40d-11eb-0bf5-8d29818bcdb9
begin
using Pkg
Pkg.activate(mktempdir())
end
# ╔═╡ bca9cc6c-2500-4cf3-ac19-4db294c99321
begin
Pkg.add("Luxor")
Pkg.add("Plots")
Pkg.add("PlutoUI")
Pkg.add("Javis")
Pkg.add("Images")
Pkg.add("PlotlyJS")
using Luxor: noise
using Plots
using PlutoUI
using Javis
using Images: load
plotlyjs()
end
# ╔═╡ 113cf35d-cae4-4d6a-a30b-847cc143f5e7
@bind amplitude Slider(1:120, default=80, show_value=true)
# ╔═╡ 1c95bb41-4fdf-445e-9266-64a1884ad030
md"""
Frequency on the x axis
$(@bind frequency1 Slider(1:10, default=3, show_value=true))
"""
# ╔═╡ 21b3674c-35be-4f53-a104-ccded850d0e7
md"""
Frequency on the y axis
$(@bind frequency2 Slider(1:10, default=3, show_value=true))
"""
# ╔═╡ 40930ffe-e621-441b-9a1d-c1a3f68260fd
begin
xs = -5:0.01:5
ys = [noise(x * frequency1, 0) * amplitude for x in xs]
ws = [noise(x * frequency2, 0) * amplitude for x in xs]
end
# ╔═╡ 183696ee-9541-4d39-b9ae-f1195162bf86
plot(xs, ys)
# ╔═╡ a21a793a-2f1a-4544-8488-25cb738763cd
begin
zs = (x, y) -> noise(x * frequency1, y * frequency2)
plot(xs, ys, zs, st=:surface, camera=(45,65))
end
# ╔═╡ 483ac5c7-5038-4952-a26e-1e830d562c8e
begin
function ground(args...)
background("white") # canvas background
sethue("black") # pen color
end
function object(p=O, color="black")
sethue(color)
circle(p, 25, :fill)
return p
end
myvideo = Video(250, 250)
frames = 1:length(xs)
Background(frames, ground)
red_ball = Object(frames, (args...) -> object(O, "red"), Point(0, 0))
points = [Point(x - (amplitude/2), y - (amplitude/2)) for (x,y) in zip(ys, ws)]
act!(red_ball, Action(frames, follow_path(points)))
filename = "meow.gif"
render(myvideo; framerate=15, pathname=filename)
load(filename)
end
# ╔═╡ Cell order:
# ╟─68bed1a4-a40d-11eb-0bf5-8d29818bcdb9
# ╠═bca9cc6c-2500-4cf3-ac19-4db294c99321
# ╟─113cf35d-cae4-4d6a-a30b-847cc143f5e7
# ╟─1c95bb41-4fdf-445e-9266-64a1884ad030
# ╟─21b3674c-35be-4f53-a104-ccded850d0e7
# ╠═40930ffe-e621-441b-9a1d-c1a3f68260fd
# ╟─183696ee-9541-4d39-b9ae-f1195162bf86
# ╠═a21a793a-2f1a-4544-8488-25cb738763cd
# ╠═483ac5c7-5038-4952-a26e-1e830d562c8e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment