Last active
January 29, 2025 15:11
-
-
Save Hermann-SW/c47291d47c1c5d14933707fa83da46fb to your computer and use it in GitHub Desktop.
K_{3,3} embedded on a torus
This file contains 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
/*jslint node*/ | |
"use strict"; | |
const jscad = require("@jscad/modeling"); | |
const {vec2, vec3} = jscad.maths; | |
const {colorize} = jscad.colors; | |
const {cylinder, sphere, torus} = jscad.primitives; | |
const {rotate, scale, translate} = jscad.transforms; | |
let edgeCylinder = cylinder({height: 1, radius: 0.01}); // cached | |
function edge(v, w) { | |
let d = vec3.subtract(vec3.create(), w, v); | |
let m = vec3.lerp(vec3.create(), v, w, 0.5); | |
let R = [0, Math.acos(d[2] / vec3.length(d)), Math.atan2(d[1], d[0])]; | |
let S = [1, 1, vec3.length(d)]; | |
return translate(m, rotate(R, scale(S, edgeCylinder))); | |
} | |
function vertex(c) { | |
return translate(c, sphere({radius: 0.08})); | |
} | |
function xyz(a) { | |
let M = [0, Math.sin(a[1]) / 2 - 1, Math.cos(a[1]) / 2]; | |
return vec3.rotateZ(vec3.create(), M, [0, 0, 0], a[0]); | |
} | |
function torus_edge(v, w) { | |
const n = 100; | |
let ret = []; | |
let from = v; | |
let i = 1; | |
while (i <= n) { | |
let to = vec2.lerp(vec2.create(), v, w, i / n); | |
ret.push(edge(xyz(from), xyz(to))); | |
from = to; | |
i += 1; | |
} | |
return ret; | |
} | |
function main() { | |
let Vs = [ | |
[-Math.PI / 5, -1.2 * Math.PI / 4], | |
[-Math.PI / 5, -0.2 * Math.PI / 4], | |
[0, -1.2 * Math.PI / 4], | |
[0, -0.2 * Math.PI / 4], | |
[Math.PI / 5, -1.2 * Math.PI / 4], | |
[Math.PI / 5, -0.2 * Math.PI / 4], | |
[2 * Math.PI - Math.PI / 5, -1.2 * Math.PI / 4], // 0 | |
[0, 2 * Math.PI - 1.2 * Math.PI / 4], // 2 | |
[Math.PI / 5 - 2 * Math.PI, 2 * Math.PI - 1.2 * Math.PI / 4] // 4 | |
]; | |
let Es = [[7, 1], [2, 3], [7, 5], [0, 1], [0, 3], [4, 3], [4, 5], [6, 5]]; | |
Es.push([8, 1]); | |
let ret = [ | |
colorize([0.66, 0.0, 0.66], torus({innerRadius: 0.5, outerRadius: 1})) | |
]; | |
Vs.forEach(function (v) { | |
ret.push(colorize([1, 0.5, 0], vertex(xyz(v)))); | |
}); | |
Es.forEach(function (e) { | |
ret.push(colorize([1, 1, 0], torus_edge(Vs[e[0]], Vs[e[1]]))); | |
}); | |
return ret; | |
} | |
module.exports = {main}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No warnings on jslint.com
JSCAD app link to play with. Below view when clicking on edge between FRONT and TOP.