Dev.to · 2 min read

Emit an isometric cube as three SVG polygons and test its shared edges

Emit an isometric cube as three SVG polygons and test its shared edges

First published on Homingo Learn. An isometric cube is three polygons that have to meet along identical edges, and a vertex that is off by even one user unit can leave a gap or an overlap. This script emits the three faces with integer coordinates and checks the edges before you trust the SVG. Name the points once All geometry hangs off a center (100, 100) and two offsets, set in cx, cy, dx, dy = 100, 100, round(50 * sqrt(3)), 50. The program names seven points: C, T, B, UR, LR, LL, UL. Each face is a list of those names. For a 30 degree edge with a vertical rise of 50, the exact horizontal run is 50 * sqrt(3), which is 86.603. The code rounds it to 87 so every coordinate is an integer and the printed points are exact. The price is that the cube is only approximately isometric: the slanted edges are very slightly off true 30 degrees. from collections import Counter from math import sqrt cx, cy, dx, dy = 100, 100, round(50 * sqrt(3)), 50 print(f"dx = round(50*sqrt(3)) = {dx}; exact value {50 * sqrt(3):.3f}") C, T, B = (cx, cy), (cx, cy - 2 * dy), (cx, cy + 2 * dy) UR, LR = (cx + dx, cy - dy), (cx + dx, cy + dy) LL, UL = (cx - dx, cy + dy), (cx - dx, cy - dy) faces = {"top": [T, UR, C, UL], "right": [C, UR, LR, B], "left": [C, B, LL, UL]} hexagon = [T, UR, LR, B, LL, UL] def ring(poly): return list(zip(poly, poly[1:] + poly[:1])) def edges(poly): return [frozenset(e) for e in ring(poly)] def area2(poly): return abs(sum(x1 * y2 - x2 * y1 for (x1, y1), (x2, y2) in ring(poly))) def check(faces): all_edges = [e for f in faces.values() for e in edges(f)] assert max(all_edges.count(e) for e in set(all_edges))

This is a summary aggregated from Dev.to. Read the complete article on the original site:

Read full article at Dev.to

More AI & Machine Learning News