isundil 3 years ago
parent
commit
068cb3d6c6
1 changed files with 73 additions and 0 deletions
  1. 73 0
      d12/render.js

+ 73 - 0
d12/render.js

@@ -0,0 +1,73 @@
+
+const fs = require('fs');
+const readline = require('readline');
+
+const MARGIN = 20;
+const GRID_STROKE_BASE_LENGTH = 15;
+let ISO_FACTOR = 1.85;
+const ALTI_FACTOR = 8;
+
+function getPos(px, py, alt) {
+    let result = [ GRID_STROKE_BASE_LENGTH * px - GRID_STROKE_BASE_LENGTH * py, GRID_STROKE_BASE_LENGTH * (py +px) ];
+    result[0] *= ISO_FACTOR;
+    result[1] += ALTI_FACTOR * alt;
+    return result;
+}
+
+function getColor() {
+    return '#11a5f3';
+}
+
+async function main() {
+    let map = [];
+    for await (let line of readline.createInterface({ input: process.stdin })) {
+        map.push(line.split('').map((i, pos) => {
+            if (i === 'S') i = 'a';
+            if (i === 'E') i = 'z';
+            return i.charCodeAt(0) - 'a'.charCodeAt(0);}));
+    }
+    lines = [];
+    for (let i =1; i < map.length; ++i) {
+        let prev = getPos(0, i -1, map[i -1][0]);
+        let next = getPos(0, i, map[i][0]);
+        lines.push([prev[0], prev[1], next[0], next[1], getColor(map[i -1][0]), getColor(map[i][0])]);
+    }
+    for (let i =1; i < map[0].length; ++i) {
+        let prev = getPos(i-1, 0, map[0][i-1]);
+        let next = getPos(i, 0, map[0][i]);
+        lines.push([prev[0], prev[1], next[0], next[1], getColor(map[0][i-1]), getColor(map[0][i])]);
+    }
+    for (let i =1; i < map.length; ++i) {
+        for (let j=1; j < map[i].length; ++j) {
+            let prev = getPos(j-1, i, map[i][j-1]);
+            let next = getPos(j, i, map[i][j]);
+            lines.push([prev[0], prev[1], next[0], next[1], getColor(map[i][j-1]), getColor(map[i][j])]);
+            prev = getPos(j, i -1, map[i -1][j]);
+            lines.push([prev[0], prev[1], next[0], next[1], getColor(map[i-1][j]), getColor(map[i][j])]);
+        }
+    }
+    let maxX = lines.map(x => Math.max(x[0], x[2])).reduce((a, b) => a ? Math.max(a, b) : b);
+    let minX = lines.map(x => Math.min(x[0], x[2])).reduce((a, b) => a ? Math.min(a, b) : b);
+    let maxY = lines.map(x => Math.max(x[1], x[3])).reduce((a, b) => a ? Math.max(a, b) : b);
+    let minY = lines.map(x => Math.min(x[1], x[3])).reduce((a, b) => a ? Math.min(a, b) : b);
+    const height = maxY - minY;
+    let draw = require('svg-builder').width(maxX - minX + 2* MARGIN).height(height + 4* MARGIN);
+    lines.forEach(l => {
+        //img.stroke(l[4], 3);
+        draw.line({
+            x1: l[0]- minX + MARGIN,
+            y1: height - (l[1] -minY) + MARGIN,
+            x2: l[2] - minX + MARGIN,
+            y2: height - (l[3] -minY) + MARGIN,
+            stroke: l[4],
+            'stroke-width': 3
+        });
+    });
+    draw.render();
+    console.log(draw.root + draw.elements.join('')+'</svg>');
+};
+
+(main());
+
+module.exports = {};
+