#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""2D geometry plotter using matplotlib (pylab).
:Date: 2019-08-20
.. module:: geometry_plot
:platform: *nix, Windows
:synopsis: Geometry plotting.
.. moduleauthor:: Daniel Weschke <daniel.weschke@directbox.de>
"""
import math
import pylab
from geometry import (
distance, angle, line, interpolate_hermite, rotate_xy, translate_xy
)
pylab.style.use('dark_background')
pylab.rcParams['grid.color'] = 'gray'
# The kwargs are Line2D properties:
# https://matplotlib.org/3.1.1/_modules/matplotlib/lines.html#Line2D
# https://matplotlib.org/devdocs/_modules/matplotlib/lines.html#Line2D
pylab_Line2D_properties = [
'linewidth', 'linestyle', 'color',
'marker', 'markersize', 'markeredgewidth', 'markeredgecolor',
'markerfacecolor', 'markerfacecoloralt',
'fillstyle', 'antialiased',
'dash_capstyle', 'solid_capstyle',
'dash_joinstyle', 'solid_joinstyle',
'pickradius', 'drawstyle', 'markevery',
]
[docs]def plot_lines(lns, **kwargs):
if 'color' not in kwargs:
# Colors from "tab10" colormap
cmap = pylab.get_cmap("tab10")
kwargs['color'] = cmap(0)
# Colors from color cycle
cycle = pylab.rcParams['axes.prop_cycle'].by_key()['color']
kwargs['color'] = cycle[0]
# Colors from CN notation (same as the first 10 colors from the color cycle)
kwargs['color'] = "C0"
for ln in lns:
x, y = line(*ln)
pylab.plot(x, y, **kwargs)
[docs]def plot_cubic_lines(lns, **kwargs):
if 'color' not in kwargs:
# Colors from "tab10" colormap
cmap = pylab.get_cmap("tab10")
kwargs['color'] = cmap(0)
# Colors from color cycle
cycle = pylab.rcParams['axes.prop_cycle'].by_key()['color']
kwargs['color'] = cycle[0]
# Colors from CN notation (same as the first 10 colors from the color cycle)
kwargs['color'] = "C0"
factor = 1
if 'factor' in kwargs:
factor = kwargs['factor']
samples = 10
if 'samples' in kwargs:
samples = kwargs['samples']
kwargs = {k: v for k, v in kwargs.items() if k in pylab_Line2D_properties}
for ln in lns:
l, r, d = ln
L = distance(l, r)
ang = angle(l, r)
x, y = interpolate_hermite(d[1], d[2], d[4], d[5], d[0], d[3],
scale_x=L, scale_y=factor, samples=samples)
x, y = rotate_xy((0, 0), ang, x, y)
x, y = translate_xy(l, x, y)
pylab.plot(x, y, **kwargs)