{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Polygonal skeletons (straight skeletons)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "scikit-geometry contains functions to work with straight skeletons of polygons. The straight skeleton can be used to create inset or offset polygons from a polygon, or a polygon with holes." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import skgeom as sg\n", "from skgeom.draw import draw\n", "\n", "import matplotlib.pyplot as plt\n", "\n", "def draw_skeleton(polygon, skeleton, show_time=False):\n", " draw(polygon)\n", "\n", " for h in skeleton.halfedges:\n", " if h.is_bisector:\n", " p1 = h.vertex.point\n", " p2 = h.opposite.vertex.point\n", " plt.plot([p1.x(), p2.x()], [p1.y(), p2.y()], 'r-', lw=2)\n", " \n", " if show_time:\n", " for v in skeleton.vertices:\n", " plt.gcf().gca().add_artist(plt.Circle(\n", " (v.point.x(), v.point.y()),\n", " v.time, color='blue', fill=False))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "poly = sg.random_polygon(seed=1)\n", "skel = sg.skeleton.create_interior_straight_skeleton(poly)\n", "draw_skeleton(poly, skel)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "draw_skeleton(poly, skel, show_time=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "skel = sg.skeleton.create_exterior_straight_skeleton(poly, 0.1)\n", "draw_skeleton(poly, skel)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "skel = sg.skeleton.create_interior_straight_skeleton(poly)\n", "draw(poly)\n", "for offset_poly in skel.offset_polygons(0.1):\n", " draw(offset_poly, facecolor=\"red\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from skgeom import boolean_set\n", "import functools\n", "\n", "poly_with_holes = functools.reduce(\n", " lambda a, b: boolean_set.difference(a, b)[0], skel.offset_polygons(0.1), poly)\n", "draw(poly_with_holes)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "draw_skeleton(poly_with_holes, sg.skeleton.create_interior_straight_skeleton(poly_with_holes))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }