{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Pixel Art of ALIEN MONSTERS {#pixel_art_example}\n\nHere we use `pyvista.Box`{.interpreted-text role=\"func\"} to make [pixel\nart](https://en.wikipedia.org/wiki/Pixel_art). Pixel string\n[source](https://commons.wikimedia.org/wiki/File:Noto_Emoji_Pie_1f47e.svg)\nand\n[license](https://github.com/googlefonts/noto-emoji/blob/main/LICENSE).\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pyvista as pv\nfrom pyvista.demos import logo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Convert pixel art to an array\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "alien_str = \"\"\"\n % %\n % %\n % % % % % %\n % % % % % %\n% % % % % % % % % %\n% % % % % % % %\n% % % %\n% % % % % %\n % %\n % %\n\"\"\"\n\n\nalien = []\nfor line in alien_str.splitlines()[1:]: # skip first linebreak\n if not line:\n continue\n if len(line) < 20:\n line += (20 - len(line)) * ' '\n alien.append([line[i : i + 2] == '% ' for i in range(0, len(line), 2)])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Define function to draw pixels\n\nDefine a helper function to add pixel boxes to plotter.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def draw_pixels(plotter, pixels, center, color):\n bounds = [\n center[0] - 1.0,\n center[0] + 1.0,\n center[1] - 1.0,\n center[1] + 1.0,\n -10.0,\n +10.0,\n ]\n for rows in pixels:\n for pixel in rows:\n if pixel:\n box = pv.Box(bounds=bounds)\n plotter.add_mesh(box, color=color)\n bounds[0] += 2.0\n bounds[1] += 2.0\n bounds[0] = center[0] - 1.0\n bounds[1] = center[0] + 1.0\n bounds[2] += -2.0\n bounds[3] += -2.0\n return plotter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now you can plot a pixel art of ALIEN MONSTERS.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Display MONSTERS\np = pv.Plotter()\np = draw_pixels(p, alien, [-22.0, 22.0], \"green\")\np = draw_pixels(p, alien, [0.0, 22.0], \"green\")\np = draw_pixels(p, alien, [22.0, 22.0], \"green\")\np = draw_pixels(p, alien, [-22.0, 0.0], \"blue\")\np = draw_pixels(p, alien, [0.0, 0.0], \"blue\")\np = draw_pixels(p, alien, [22.0, 0.0], \"blue\")\np = draw_pixels(p, alien, [-22.0, -22.0], \"red\")\np = draw_pixels(p, alien, [0.0, -22.0], \"red\")\np = draw_pixels(p, alien, [22.0, -22.0], \"red\")\n\ntext = logo.text_3d(\"ALIEN MONSTERS\", depth=10.0)\ntext.points *= 4.0\ntext.translate([-20.0, 24.0, 0.0], inplace=True)\n\np.add_mesh(text, color=\"yellow\")\np.show(cpos=\"xy\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }