Created
December 15, 2025 06:28
-
-
Save singularitti/d92fbf74a3e9177a3d2321d6ad4d0987 to your computer and use it in GitHub Desktop.
Plot polygon meshes #Python #plot #mesh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| from scipy.spatial import Delaunay | |
| import svgwrite | |
| from pathlib import Path | |
| # cairosvg might not be pre-imported after reset; import explicitly | |
| import cairosvg | |
| def polygon_mesh_svg( | |
| out_svg, | |
| width=1920, | |
| height=1200, | |
| step=160, | |
| jitter=0.55, | |
| stroke="#9CADB7", | |
| stroke_width=2.0, | |
| stroke_opacity=0.35, | |
| bg=None, | |
| seed=7, | |
| ): | |
| rng = np.random.default_rng(seed) | |
| xs = np.arange(0, width + step, step) | |
| ys = np.arange(0, height + step, step) | |
| pts = [] | |
| for y in ys: | |
| for x in xs: | |
| jx = (rng.random() - 0.5) * step * jitter | |
| jy = (rng.random() - 0.5) * step * jitter | |
| pts.append([x + jx, y + jy]) | |
| pts = np.asarray(pts, dtype=float) | |
| frame = np.array( | |
| [ | |
| [0, 0], | |
| [width, 0], | |
| [width, height], | |
| [0, height], | |
| [width / 2, 0], | |
| [width, height / 2], | |
| [width / 2, height], | |
| [0, height / 2], | |
| ], | |
| dtype=float, | |
| ) | |
| pts = np.vstack([pts, frame]) | |
| tri = Delaunay(pts) | |
| dwg = svgwrite.Drawing(str(out_svg), size=(width, height), profile="full") | |
| dwg.attribs["viewBox"] = f"0 0 {width} {height}" | |
| if bg is not None: | |
| dwg.add(dwg.rect(insert=(0, 0), size=(width, height), fill=bg)) | |
| for simplex in tri.simplices: | |
| p = pts[simplex] | |
| dwg.add( | |
| dwg.polygon( | |
| points=[(p[0, 0], p[0, 1]), (p[1, 0], p[1, 1]), (p[2, 0], p[2, 1])], | |
| fill="none", | |
| stroke=stroke, | |
| stroke_width=stroke_width, | |
| stroke_opacity=stroke_opacity, | |
| stroke_linejoin="round", | |
| ) | |
| ) | |
| dwg.save() | |
| def svg_to_png(in_svg, out_png, width=1920, height=1200): | |
| cairosvg.svg2png( | |
| url=str(in_svg), write_to=str(out_png), output_width=width, output_height=height | |
| ) | |
| out_dir = Path("/mnt/data") | |
| variants = [ | |
| ("gullgray", "#9CADB7", 0.40, 2.0), # visible on white | |
| ("bigstone", "#333F48", 0.22, 2.0), # subtle dark | |
| ] | |
| outputs = [] | |
| for name, stroke, op, w in variants: | |
| svg_path = out_dir / f"ut_polygon_mesh_16x10_{name}_transparent.svg" | |
| png_path = out_dir / f"ut_polygon_mesh_16x10_{name}_transparent.png" | |
| polygon_mesh_svg( | |
| svg_path, | |
| width=1920, | |
| height=1200, | |
| step=160, | |
| jitter=0.55, | |
| stroke=stroke, | |
| stroke_width=w, | |
| stroke_opacity=op, | |
| bg=None, | |
| seed=7, | |
| ) | |
| svg_to_png(svg_path, png_path, width=1920, height=1200) | |
| outputs.append((str(svg_path), str(png_path))) | |
| outputs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment