{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Vertex Sampling\n", "Vertex sampling is designed for polygon simplification.\n", "There is a choice of two implementation algorithms: **Douglas-Peucker** and **Visvalingam-Whyatt**." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:27:38.865456Z", "iopub.status.busy": "2024-04-17T07:27:38.865332Z", "iopub.status.idle": "2024-04-17T07:27:39.190358Z", "shell.execute_reply": "2024-04-17T07:27:39.189998Z" } }, "outputs": [], "source": [ "import numpy as np\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:27:39.191938Z", "iopub.status.busy": "2024-04-17T07:27:39.191757Z", "iopub.status.idle": "2024-04-17T07:27:39.193733Z", "shell.execute_reply": "2024-04-17T07:27:39.193562Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:27:39.207770Z", "iopub.status.busy": "2024-04-17T07:27:39.207594Z", "iopub.status.idle": "2024-04-17T07:27:39.209940Z", "shell.execute_reply": "2024-04-17T07:27:39.209760Z" } }, "outputs": [], "source": [ "def f(x, y, n):\n", " z = np.sin(x * 3 * np.pi / n)\n", " z += np.sin(y * 3 * np.pi / n)\n", " z += x * 3 / n\n", " z += y * 5 / n\n", " return z\n", "\n", "n = 50\n", "x = np.arange(n)\n", "y = np.arange(n)\n", "X, Y = np.meshgrid(x, y)\n", "Z = f(X, Y, n)\n", "data = dict(\n", " x=X.reshape(-1),\n", " y=Y.reshape(-1),\n", " z=Z.reshape(-1))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:27:39.210967Z", "iopub.status.busy": "2024-04-17T07:27:39.210852Z", "iopub.status.idle": "2024-04-17T07:27:39.257704Z", "shell.execute_reply": "2024-04-17T07:27:39.257273Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(data, aes('x','y', z='z')) \\\n", "+ theme_void() + ggsize(200, 200)\n", "p + geom_contour()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:27:39.258966Z", "iopub.status.busy": "2024-04-17T07:27:39.258873Z", "iopub.status.idle": "2024-04-17T07:27:39.274968Z", "shell.execute_reply": "2024-04-17T07:27:39.274706Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Douglas-Peucker algorithm in this case works better than others. \n", "# The lines become ragged but the shape remained unchanged.\n", "p + geom_contour(sampling=sampling_vertex_dp(200))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:27:39.276064Z", "iopub.status.busy": "2024-04-17T07:27:39.275936Z", "iopub.status.idle": "2024-04-17T07:27:39.291608Z", "shell.execute_reply": "2024-04-17T07:27:39.291425Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Visvalingam-Whyatt algorithm is less accurate with rings: they became unclosed.\n", "p + geom_contour(sampling=sampling_vertex_vw(200))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.13" } }, "nbformat": 4, "nbformat_minor": 2 }