plotter_experiments/faces.ipynb

209 lines
280 KiB
Plaintext
Raw Normal View History

2022-12-11 15:39:07 +01:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from lib import plot\n",
"import IPython\n",
"from random import randint, randrange\n",
"\n",
"paper = plot.A6_PORTRAIT"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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=\"surface1\">\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(30%,15%,0%);stroke-opacity:0.5;stroke-miterlimit:10;\" d=\"M 11.731239 11.462522 L 11.731239 15.60077 L 15.869488 15.60077 L 15.869488 11.462522 L 11.731239 11.462522 \" 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(30%,15%,0%);stroke-opacity:0.5;stroke-miterlimit:10;\" d=\"M 13.129948 14.699533 L 13.19196 14.625119 L 13.26224 14.557595 L 13.340788 14.49834 L 13.424848 14.44873 L 13.513043 14.410145 L 13.606749 14.381207 L 13.703212 14.36467 L 13.799674 14.359158 L 13.897515 14.36467 L 13.9926 14.381207 L 14.086306 14.410145 L 14.175879 14.44873 L 14.259939 14.49834 L 14.337109 14.557595 L 14.407389 14.625119 L 14.469401 14.699533 \" 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(30%,15%,0%);stroke-opacity:0.5;stroke-miterlimit:10;\" d=\"M 12.972852 13.117546 L 13.386263 13.117546 \" 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(30%,15%,0%);stroke-opacity:0.5;stroke-miterlimit:10;\" d=\"M 14.627875 13.117546 L 14.214464 13.117546 \" 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(30%,15%,0%);stroke-opacity:0.5;stroke-miterlimit:10;\" d=\"M 16.736274 21.593859 L 15.268663 24.136339 L 12.332064 24.136339 L 10.863075 21.593859 L 12.332064 19.051378 L 15.268663 19.051378 L 16.736274 21.593859 \" 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(30%,15%,0%);stroke-opacity:0.5;stroke-miterlimit:10;\" d=\"M 13.008681 22.659082 L 13.081717 22.747276 L 13.164399 22.827203 L 13.256727 22.897483 L 13.355946 22.95536 L 13.460677 23.000836 L 13.57092 23.035286 L 13.685297 23.054579 L 13.799674 23.061469 L 13.91543 23.054579 L 14.028429 23.035286 L 14.138672 23.000836 L 14.244781 22.95536 L 14.344 22.897483 L 14.436328 22.827203 L 14.51901 22.747276 L 14.592046 22.659082 \" 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(30%,15%,0%);stroke-opacity:0.5;stroke-miterlimit:10;\" d=\"M 13.677029 21.104655 L 13.497884 21.537359 L 13.066558 21.716504 L 12.633854 21.537359 L 12.454709 21.104655 L 12.633854 20.671951 L 13.066558 20.492806 L 13.497884 20.671951 L 13.677029 21.104655 \" 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(30%,15%,0%);stroke-opacity:0.5;stroke-miterlimit:10;\" d=\"M 15.146018 21.104655 L 14.966873 21.537359 L 14.534169 21.716504 L 14.101465 21.537359 L 13.92232 21.104655 L 14.101465 20.671951 L 14.534169 20.492806 L 14.966873 20.671951 L 15.146018 21.104655 \" 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(30%,15%,0%);stroke-opacity:0.5;stroke-miterlimit:10;\" d=\"M 15.512576 21.104655 L 15.146018 21.104655 \" 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(30%,15%,0%);stroke-opacity:0.5;stroke-miterlimit:10;\" d=\"M 12.
"text/plain": [
"<IPython.core.display.SVG object>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p_svg = plot.SVGPlotter('plots/choices.svg', paper)\n",
"p_hpgl = plot.HPGLPlotter(paper, 'plots/choices_{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",
"plotter.add_layer([0.3, 0.15, 0, 0.5])\n",
"\n",
"def face(head=0, mouth=0, eyes=0):\n",
" yield None\n",
" if head == 0:\n",
" yield (-1, -1)\n",
" yield (-1, 1)\n",
" yield (1, 1)\n",
" yield (1, -1)\n",
" yield (-1, -1)\n",
" elif head == 1:\n",
" for i in np.linspace(0, 2 * np.pi, 7):\n",
" yield(1.2 * np.cos(i), 1.2 * np.sin(i))\n",
" elif head == 2:\n",
" for i in np.linspace(0, 2 * np.pi, 7):\n",
" yield(1.2 * np.sin(i), 1.2 * np.cos(i))\n",
" elif head == 3:\n",
" for i in np.linspace(0, 2 * np.pi, 17):\n",
" yield(1.1 * np.cos(i), 1.1 * np.sin(i))\n",
" elif head == 4:\n",
" yield (-1.3, 1.1)\n",
" yield (1.3, 1.1)\n",
" yield (0, -1.3)\n",
" yield (-1.3, 1.1)\n",
" elif head == 5:\n",
" yield (-1.2, -1.0)\n",
" yield (1.2, -1.0)\n",
" yield (0.5, 1.1)\n",
" yield (-0.5, 1.1)\n",
" yield (-1.2, -1.0)\n",
"\n",
" yield None\n",
" if mouth == 0:\n",
" yield (-0.3, 0.6)\n",
" yield (0.3, 0.6)\n",
" elif mouth == 1:\n",
" yield (-0.45, 0.4)\n",
" yield (0.45, 0.6)\n",
" elif mouth == 2:\n",
" for i in np.linspace(-0.3 * np.pi, 0.3 * np.pi, 17):\n",
" yield(0.4 * np.sin(i), 0.4 * np.cos(i) + 0.2)\n",
" elif mouth == 3:\n",
" for i in np.linspace(-0.3 * np.pi, 0.3 * np.pi, 17):\n",
" yield(0.4 * np.sin(i), -0.4 * np.cos(i) + 0.8)\n",
"\n",
" if eyes == 0:\n",
" yield None\n",
" yield (-0.4, -0.2)\n",
" yield (-0.2, -0.2)\n",
" yield None\n",
" yield (0.4, -0.2)\n",
" yield (0.2, -0.2)\n",
" elif eyes == 1:\n",
" yield None\n",
" yield (-0.4, -0.3)\n",
" yield (-0.2, -0.1)\n",
" yield None\n",
" yield (-0.4, -0.1)\n",
" yield (-0.2, -0.3)\n",
" yield None\n",
" yield (0.4, -0.3)\n",
" yield (0.2, -0.1)\n",
" yield None\n",
" yield (0.4, -0.1)\n",
" yield (0.2, -0.3)\n",
" elif eyes == 2:\n",
" yield None\n",
" for i in np.linspace(0, 2 * np.pi, 9):\n",
" yield(0.25 * np.cos(i) - 0.3, 0.25 * np.sin(i) - 0.2)\n",
" yield None\n",
" for i in np.linspace(0, 2 * np.pi, 9):\n",
" yield(0.25 * np.cos(i) + 0.3, 0.25 * np.sin(i) - 0.2)\n",
" yield None\n",
" yield (0.7, -0.2)\n",
" yield (0.55, -0.2)\n",
" yield None\n",
" yield (-0.55, -0.2)\n",
" yield (-0.7, -0.2)\n",
" elif eyes == 3:\n",
" yield None\n",
" for i in np.linspace(0, 2 * np.pi, 9):\n",
" yield(0.1 * np.cos(i) - 0.2, 0.1 * np.sin(i) - 0.2)\n",
" yield None\n",
" for i in np.linspace(0, 2 * np.pi, 9):\n",
" yield(0.1 * np.cos(i) + 0.2, 0.1 * np.sin(i) - 0.2)\n",
" elif eyes == 4:\n",
" yield None\n",
" for i in np.linspace(0, 2 * np.pi, 9):\n",
" yield(0.1 * np.cos(i) - 0.3, 0.1 * np.sin(i) - 0.2)\n",
" yield None\n",
" for i in np.linspace(-0.3 * np.pi, 0.3 * np.pi, 17):\n",
" yield(0.2 * np.sin(i) + 0.3, 0.1 * np.cos(i) - 0.25)\n",
" elif eyes == 5:\n",
" yield None\n",
" for i in np.linspace(-0.3 * np.pi, 0.3 * np.pi, 17):\n",
" yield(0.2 * np.sin(i) - 0.3, -0.2 * np.cos(i) - 0.2)\n",
" yield None\n",
" for i in np.linspace(-0.3 * np.pi, 0.3 * np.pi, 17):\n",
" yield(0.2 * np.sin(i) + 0.3, -0.2 * np.cos(i) - 0.2)\n",
"\n",
"size = 5\n",
"dist = 3\n",
"count_h = (paper.content_width - dist) // (size + dist)\n",
"count_v = (paper.content_height - dist) // (size + dist)\n",
"dist_h = (paper.content_width - dist) / count_h\n",
"dist_v = (paper.content_height - dist) / count_v\n",
"\n",
"for xi in range(count_h):\n",
" x = paper.centre()[0] + (xi - (count_h - 1) / 2) * dist_h\n",
" for yi in range(count_v):\n",
" y = paper.centre()[1] + (yi - (count_v - 1) / 2) * dist_v\n",
"\n",
" up = False\n",
"\n",
" head = randrange(6)\n",
" mouth = randrange(4)\n",
" eyes = randrange(6)\n",
" scale = 1 + (0.5 - np.random.rand()) * 0.4\n",
" for p in face(head, mouth, eyes):\n",
" if p is None:\n",
" up = True\n",
" continue\n",
" p = np.array(p) * size * scale / 2 + [x, y]\n",
" if up:\n",
" plotter.move_to(p)\n",
" up = False\n",
" else:\n",
" plotter.line_to(p)\n",
"\n",
"plotter.finalise()\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
}