1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| const getSin = (deg) => { return Math.sin(deg * Math.PI / 180) } const getCos = (deg) => { return Math.cos(deg * Math.PI / 180) } const getPoints = (c, n, l) => { if (n < 3) return; const res = []; const θ = 360 / n; for(let i = 1;i < n + 1;i++) { let deg = (n%2 == 0 ? θ/2 : 0) + (i - 1)*θ; res.push([c[0] + l * getSin(deg), c[1] - l * getCos(deg)]) } return res; }
const res = getPoints([50, 50], 5, 30);
const fs = require("fs"); const buildSVG = (list) => { let path = `M ${list[0][0]} ${list[0][1]}`; for(let i = 1; i < list.length; i ++) { const it = list[i]; path += ` L ${it[0]} ${it[1]}` } path += `L ${list[0][0]} ${list[0][1]}`; const svg = `<svg width="100" height="100"> <path d="${path}" fill="#fff" stroke="#000" stroke-width="2" > </svg>`; fs.writeFileSync("index.html", svg) }
buildSVG(res);
|