Skip to content

Instantly share code, notes, and snippets.

@brv00
Last active March 23, 2020 04:52
Show Gist options
  • Save brv00/118ed8ecfed471fb41c7ff57b99878e2 to your computer and use it in GitHub Desktop.
Save brv00/118ed8ecfed471fb41c7ff57b99878e2 to your computer and use it in GitHub Desktop.
programs/julia/dragon_curve.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"scrolled": true,
"trusted": false
},
"cell_type": "code",
"source": "function duplicate!(xs, ys, last, mat)\n center_x = xs[last]; center_y = ys[last]\n for i in 1:last-1\n dx = xs[last-i] - center_x\n dy = ys[last-i] - center_y\n xs[last+i] = mat[1,1]*dx + mat[1,2]*dy + center_x\n ys[last+i] = mat[2,1]*dx + mat[2,2]*dy + center_y\n end\nend",
"execution_count": 1,
"outputs": [
{
"data": {
"text/plain": "duplicate! (generic function with 1 method)"
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "function make_sin_table(n)\n (k->sin(k*π/2n)).(0:n)\nend",
"execution_count": 2,
"outputs": [
{
"data": {
"text/plain": "make_sin_table (generic function with 1 method)"
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "using Plots\ngr(\n legend=false\n , xlims=(-100,50), ylims=(-50,100)\n , xticks=nothing, yticks=nothing\n , size=(500,500)\n)\nENV[\"PLOTS_TEST\"]=\"true\"\n\nmax_order = 10\nxs = Array{Float64, 1}(undef, 2 * 2^max_order + 1)\nys = Array{Float64, 1}(undef, 2 * 2^max_order + 1)\nxs[1] = 0; xs[2] = 1\nys[1] = 0; ys[2] = 0\n\nfp90 = 16\nsintbl = make_sin_table(fp90)\n\ndragon_unfolding = foldr(\n append!,\n (odr->(i->begin\n len = 2^odr\n duplicate!(xs, ys, len + 1,[\n sintbl[fp90-i+1] -sintbl[i+1]\n sintbl[i+1] sintbl[fp90-i+1]])\n plot(getindex(xs, 1:2*len+1), getindex(ys, 1:2*len+1))\n end).(1:fp90)).(0:max_order))\n\ndragon_folding = reverse(dragon_unfolding)\n@time @gif for plt = append!(\n dragon_unfolding,\n append!(\n [\n dragon_folding[1]\n dragon_folding[1]\n dragon_folding[1]],\n dragon_folding))\n plot(plt)\nend",
"execution_count": 3,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": " 86"
},
{
"name": "stderr",
"output_type": "stream",
"text": "┌ Info: Saved animation to \n│ fn = https://user-images.githubusercontent.com/35671444/77270986-ca49b380-6cf0-11ea-991c-8a1c4ff34ed4.gif\n└ @ Plots /home/brv00/.julia/packages/Plots/vTdnV/src/animation.jl:98\n"
},
{
"name": "stdout",
"output_type": "stream",
"text": ".628516 seconds (6.22 M allocations: 322.864 MiB, 0.91% gc time)\n"
},
{
"data": {
"text/html": "<img src=\"https://user-images.githubusercontent.com/35671444/77270986-ca49b380-6cf0-11ea-991c-8a1c4ff34ed4.gif\" />",
"text/plain": "Plots.AnimatedGif(\"https://user-images.githubusercontent.com/35671444/77270986-ca49b380-6cf0-11ea-991c-8a1c4ff34ed4.gif\")"
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
]
}
],
"metadata": {
"_draft": {
"nbviewer_url": "https://gist.github.com/118ed8ecfed471fb41c7ff57b99878e2"
},
"gist": {
"id": "118ed8ecfed471fb41c7ff57b99878e2",
"data": {
"description": "programs/julia/dragon_curve.ipynb",
"public": true
}
},
"kernelspec": {
"name": "julia-1.3",
"display_name": "Julia 1.3.1",
"language": "julia"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.3.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment