{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Polygons" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`scikit-geometry` can handle polygons, as well as polygons with holes. Polygons can be created from lists of points." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import skgeom as sg\n", "from skgeom.draw import draw" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "poly = sg.Polygon([sg.Point2(0, 0), sg.Point2(0, 3), sg.Point2(3, 3)])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "draw(poly)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You may also use lists of coordinates or numpy arrays to construct polygons:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sg.Polygon([[0, 0], [0, 3], [3, 3]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Likewise, you can access a Polygon's vertices as either a list of `Point2`s or as a numpy array:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list(poly.vertices)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "poly.coords" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can check wether a given point is _inside_ the polygon (a negative sign means outside, positive means inside):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "poly.oriented_side(sg.Point2(1, 2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "poly.oriented_side(sg.Point2(2, 1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate the bounding box" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "poly.bbox()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compute the area of the polygon. Note: the sign depends on the order of the polygon (clockwise → negative, counter-clockwise → positive)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "poly.area()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "poly.orientation() == sg.Sign.CLOCKWISE" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "poly.is_convex()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "poly.is_simple()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Polygon with Holes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can cut out an area of a Polygon by adding one or multiple holes." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hole = sg.Polygon([\n", " sg.Point2(1.0, 2.0),\n", " sg.Point2(1.0, 2.5),\n", " sg.Point2(0.5, 2.5),\n", " sg.Point2(0.5, 2.0)]\n", ")\n", "poly_with_hole = sg.PolygonWithHoles(poly, [hole])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "draw(poly_with_hole)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Boolean operations on Polygons\n", "\n", "*scikit-geometry* supports boolean operations on polygons, such as computing the union, difference and intersection between two polygons (or polygons with holes)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "poly1 = sg.Polygon([sg.Point2(3, 0), sg.Point2(3, 3), sg.Point2(0, 3), sg.Point2(0, 0)])\n", "poly2 = sg.Polygon([sg.Point2(5, 2), sg.Point2(5, 5), sg.Point2(2, 5), sg.Point2(2, 2)])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "draw(poly1, facecolor='red')\n", "draw(poly2, facecolor='blue')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from skgeom import boolean_set" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "draw(boolean_set.join(poly1, poly2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "draw(boolean_set.difference(poly1, poly2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "draw(boolean_set.symmetric_difference(poly1, poly2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "draw(boolean_set.intersect(poly1, poly2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Minkowski Sum of 2 Polygons\n", "\n", "We can compute the minkowski sum of two polygons easily (i.e. offsetting a polygon with another polygon)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from skgeom import minkowski" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p1 = sg.Polygon([sg.Point2(-1, -1), sg.Point2(1, -1), sg.Point2(0, 1)])\n", "p2 = sg.Polygon([sg.Point2(3, -1), sg.Point2(5, -1), sg.Point2(5, 1), sg.Point2(3, 1)])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "draw(p1, facecolor='red')\n", "draw(p2, facecolor='blue')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result = minkowski.minkowski_sum(p1, p2)\n", "draw(result)" ] } ], "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": 2 }