plotter_experiments/perlin_landscape.ipynb

115 lines
52 KiB
Plaintext
Raw Permalink Normal View History

2022-12-11 16:48:54 +01:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
2022-12-11 17:37:59 +01:00
"from lib import plot\n",
2022-12-11 16:48:54 +01:00
"import IPython\n",
"import noise"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"297.637795pt\" height=\"419.527559pt\" viewBox=\"0 0 297.637795 419.527559\" version=\"1.1\">\n<g id=\"surface21\">\n<path style=\"fill:none;stroke-width:0.5;stroke-linecap:round;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 7.999512 7.999512 L 97.000109 7.999512 L 97.000109 140.000412 L 7.999512 140.000412 L 7.999512 7.999512 \" transform=\"matrix(2.834646,0,0,2.834646,0,0)\"/>\n<path style=\"fill:none;stroke-width:0.5;stroke-linecap:round;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 13.124436 28.423416 L 13.920942 28.423416 L 14.71607 27.936968 L 15.511198 27.498752 L 16.306326 27.253461 L 17.102832 27.254839 L 17.89796 27.599349 L 18.693088 28.544683 L 19.488216 28.821669 L 20.284722 29.317763 L 21.07985 30.341645 L 21.874978 30.953494 L 22.670106 31.76378 L 23.465234 33.145953 L 24.26174 35.115169 L 25.056868 37.231836 L 25.851997 38.456912 L 26.647125 39.360905 L 27.443631 40.47436 L 28.238759 41.84413 L 29.033887 43.252485 L 29.829015 44.706315 L 30.625521 45.920367 L 31.420649 45.935525 L 32.215777 46.158767 L 33.010905 46.74719 L 33.807411 47.473416 L 34.602539 48.002582 L 35.397667 48.35536 L 36.192795 48.625456 L 36.989301 48.810113 L 37.784429 48.792198 L 38.579557 48.517969 L 39.374685 48.18724 L 40.169813 47.855132 L 40.966319 47.387977 L 41.761447 46.705849 L 42.556576 46.084353 L 43.351704 45.252018 L 44.14821 44.17577 L 44.943338 43.117437 L 45.738466 42.494564 L 46.533594 42.143164 L 47.3301 41.992958 L 48.125228 42.01225 L 48.920356 42.220334 L 49.715484 42.482161 L 50.51199 42.608941 L 51.307118 42.432552 L 52.102246 42.114225 L 52.897374 41.888227 L 53.692502 41.729753 L 54.489008 41.444499 L 55.284136 41.021441 L 56.079264 40.664529 L 56.874392 40.218045 L 57.670898 39.478038 L 58.466026 38.529948 L 59.261155 37.692101 L 60.056283 36.912131 L 60.852789 36.181771 L 61.647917 35.450033 L 62.443045 34.676953 L 63.238173 33.657205 L 64.034679 32.467958 L 64.829807 31.535026 L 65.624935 30.771593 L 66.420063 29.665028 L 67.216569 28.281478 L 68.011697 26.969586 L 68.806825 25.699034 L 69.601953 24.489117 L 70.397081 23.684342 L 71.193587 23.309516 L 71.988715 23.199273 L 72.783843 23.298492 L 73.578971 23.473503 L 74.375477 23.652648 L 75.170605 23.964084 L 75.965734 24.414703 L 76.760862 25.09821 L 77.557368 26.195128 L 78.352496 27.581434 L 79.147624 28.918132 L 79.942752 30.059147 L 80.739258 30.771593 L 81.534386 31.149175 L 82.329514 31.456478 L 83.124642 32.009071 L 83.91977 32.714627 L 84.716276 33.527669 L 85.511404 34.328309 L 86.306532 34.8685 L 87.10166 35.302582 L 87.898166 35.834505 L 88.693294 36.320953 L 89.488422 36.777083 L 90.28355 37.422005 L 91.080056 37.890538 L 91.875184 37.948416 \" transform=\"matrix(2.834646,0,0,2.834646,0,0)\"/>\n<path style=\"fill:none;stroke-width:0.5;stroke-linecap:round;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 13.124436 28.423416 L 13.920942 28.423416 L 14.71607 27.936968 L 15.511198 27.498752 L 16.306326 27.253461 L 17.102832 27.254839 L 17.89796 27.599349 L 18.693088 28.544683 L 19.488216 28.821669 L 20.284722 29.317763 L 21.07985 30.341645 L 21.874978 30.953494 L 22.670106 31.76378 L 23.465234 33.145953 L 24.26174 35.115169 L 25.056868 37.231836 L 25.851997 38.456912 L 26.647125 39.360905 L 27.443631 40.47436 L 28.238759 41.84413 L 29.033887 43.252485 L 29.829015 44.706315 L 30.625521 46.337912 L 31.420649 47.279112 L 32.215777 47.607086 L 33.010905 48.165191 L 33.807411 48.850076 L 34.602539 49.522559 L 35.397667 50.201931 L 36.192795 50.929536 L 36.989301 51.454568 L 37.784429 51.38291 L 38.579557 51.180339 L 39.374685 51.141753 L 40.169813 50.911621 L 40.966319 50.328711 L 41.761447 49.483974 L 42.556576 48.293349 L 43.351704 46.996615 L 44.14821 45.901074 L 44.943338 45.02602 L 45.738466 44.251562 L 46.533594 43.716884 L 47.3301 43.519824 L 48.125228 43.689323 L 48.920356 43.927724 L 49.715484 44.30
"text/plain": [
"<IPython.core.display.SVG object>"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"paper = plot.A6_PORTRAIT\n",
"p_svg = plot.SVGPlotter('plots/landscape.svg', paper)\n",
"p_hpgl = plot.HPGLPlotter(paper, 'plots/landscape_{index}.hpgl')\n",
"plotter = plot.MultiPlotter()\n",
"plotter.register_plotter(p_svg)\n",
"plotter.register_plotter(p_hpgl)\n",
"\n",
"plotter.move_to(paper.bottom_left())\n",
"plotter.line_to(paper.bottom_right())\n",
"plotter.line_to(paper.top_right())\n",
"plotter.line_to(paper.top_left())\n",
"plotter.line_to(paper.bottom_left())\n",
"\n",
"centre = paper.centre()\n",
"\n",
"shape = (100, 21)\n",
"scale = .6\n",
"octaves = 6\n",
"persistence = 0.4\n",
"lacunarity = 1.8\n",
"seed = np.random.randint(0, 100)\n",
"\n",
"world = np.zeros(shape)\n",
"\n",
"x_idx = np.linspace(0, 1, shape[0])\n",
"y_idx = np.linspace(0, 1, shape[1])\n",
"world_x, world_y = np.meshgrid(x_idx, y_idx)\n",
"\n",
"world = np.vectorize(noise.pnoise2)(world_x / scale,\n",
" world_y / scale,\n",
" octaves=octaves,\n",
" persistence=persistence,\n",
" lacunarity=lacunarity,\n",
" repeatx=1024,\n",
" repeaty=1024,\n",
" base=seed)\n",
"\n",
"def tf(coord):\n",
" return paper.width * 0.75 * (np.array(coord) - [0.5, 0.5]) + centre\n",
"\n",
"for i in reversed(range(len(y_idx) - 1)):\n",
" world[i, :] = np.min([world[i, :], world[i + 1, :] - (y_idx[0] - y_idx[1])], axis=0)\n",
"\n",
"for i, y in enumerate(y_idx):\n",
" plotter.move_to(tf((0, y + world[i, 0])))\n",
" for j, x in enumerate(x_idx[1:]):\n",
" plotter.line_to(tf((x, y + world[i, j])))\n",
"\n",
"plotter.finalise()\n",
"\n",
"IPython.display.SVG(filename=p_svg.file_name)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.15 64-bit",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.15"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "1dc4d77b1edb83bef89f833b7ed5251134c6a4899ef5e2c90c44e9927b4ae63a"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}