Vertex Sampling¶

Vertex sampling is designed for polygon simplification. There is a choice of two implementation algorithms: Douglas-Peucker and Visvalingam-Whyatt.

In [1]:
import numpy as np
from lets_plot import *

LetsPlot.setup_html()

In [2]:
def f(x, y, n):
z = np.sin(x * 3 * np.pi / n)
z += np.sin(y * 3 * np.pi / n)
z += x * 3 / n
z += y * 5 / n
return z

n = 50
x = np.arange(n)
y = np.arange(n)
X, Y = np.meshgrid(x, y)
Z = f(X, Y, n)
data = dict(
x=X.reshape(-1),
y=Y.reshape(-1),
z=Z.reshape(-1))

In [3]:
p = ggplot(data, aes('x','y', z='z')) \
+ theme_classic() + theme(axis='blank') + ggsize(200, 200)
p + geom_contour()

Out[3]:
In [4]:
# Douglas-Peucker algorithm in this case works better than others.
# The lines become ragged but the shape remained unchanged.
p + geom_contour(sampling=sampling_vertex_dp(200))

Out[4]:
In [5]:
# Visvalingam-Whyatt algorithm is less accurate with rings: they became unclosed.
p + geom_contour(sampling=sampling_vertex_vw(200))

Out[5]: