teaching machines

spiegedj – HW4

May 12, 2015 by . Filed under cs455, postmortems, spring 2015.

So I like math things so I decided to start with a Möbius Strip. Luckily, there is a relatively simple parametrization that gives you exactly the coordinates you need for the moveto command!

mobiusmobius-wire

R = 10 w = 2 for s in -w .. w for t to 361 x = (R + s * cos ( 0.5 * t)) * cos (t) y = (R + s * cos ( 0.5 * t)) * sin (t) z = s * sin ( 0.5 * t) moveto x, y, z end end surface 361, w * 2 + 1

Next I wanted to do a Klein bottle. This was more challenging because the parametric equation is rather large and you need to be careful how you compute it else it will never finish. A trouble I ran into was that at first my Klein bottle was all twisted up. I was about to give up on it I stumbled upon the solution: StepV needs to twice stepU.

KleinKlein-wire

stepU = 5 stepV = 10 for u in 0,stepU..(180) cu = cos(u) su = sin(u) for v in 0,stepV..(360) cv = cos(v) sv = sin(v) x = (-2//15) * cu * (3 * cv - 30 * su + 90 * cu ^ 4 * su - 60 * cu ^ 6 * su + 5 * cu * cv * su) A = 3 * (cos v) - 3 * cu ^ 2 * cv - 48 * cu ^ 4 * cv + 48 * cu ^ 6 * cv - 60 * su + 5 * cu * cv * su B = -5 * cu ^ 3 * cv * su - 80 * cu^5 * cv * su + 80 * cu ^ 7 * cv * su y = (-1//15) * su * (A + B) z = (2//15) * (3 + 5 * cu * su) * sv moveto x, y, z end end surface (180 / stepU) + 1, (360 / stepV) + 1

I thing I noticed is that the parenthesis in cos(v) does not do what I would expect. Instead you have to do this: (cos u). This is part of the reason I pulled out the cos and sin evaluations into their own variables.

And finally a “Bagel” Klein just for fun:
BagelBagel-wire

r = 3 step = 5 for v in 0, step.. 360 sv = sin(v) sdv = sin(2 * v) for t in 0, step..360 cht = cos (t // 2) sht = sin (t // 2) ct = cos(t) st = sin(t) a = (r + cht * sv - sht * sdv) x = a * ct y = a * st z = sht * sv + cht * sdv moveto x, y, z end end surface 360 / step + 1, 360 / step + 1