On 12/19/2012 9:13 PM, Troy Henderson wrote:
I got this to work. There's probably a better method though ;)
\edef\r{\luaexpr{1/3.8}} \edef\q{1}\doloop{\ctxlua{commands.doifelse(math.abs(\q*\r - math.floor(\q*\r+0.5))<0.000001)}{\exitloop}{\edef\q{\luaexpr{\q+1}}}}
Yes, you need to think more hybrid .. two variants: \startluacode local r = 1 / 3.8 local q = 1 while true do if math.abs(q*r - math.floor(q*r+0.5)) < 0.000001 then break else q = q + 1 end end context.setvalue("MyQ",q) \stopluacode \startluacode userdata = userdata or { } function userdata.CalculateQ(r) local q = 1 while true do if math.abs(q*r - math.floor(q*r+0.5)) < 0.000001 then break else q = q + 1 end end context(q) end \stopluacode \edef\MyOtherQ{\ctxlua{userdata.CalculateQ(1/3.8)}} \starttext q: \MyQ q: \MyOtherQ \stoptext ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
In case anyone cares, here is the "finished" product which is a frame-by-frame animation of a hypocycloid. The following code will produce a 190 page PDF where each page is an individual frame of the animation. Troy \edef\r{\luaexpr{1/3.8}} % Radius of smaller circle (larger circle has radius 1) \edef\N{10} % Number of frames in animation per hypocycloid "branch" \edef\K{1}\doloop{\ctxlua{commands.doifelse(math.abs(\K*\r - math.floor(\K*\r+0.5))<0.000001)}{\exitloop}{\edef\K{\luaexpr{\K+1}}}} \startMPinclusions[+] r:=\r; % Radius of smaller circle (larger circle has radius 1) u:=70; % Scaling factor for the graphics K:=\K; % Number of "branches" per period (computed automatically) M:=8; % Number of points in the Bezier curve for each "branch" N:=\N; % Number of frames in animation per hypocycloid "bran path p; % p is the "first branch" of the hypocycloid p:=(1,0) for m=1 upto (M-1): .. (r*cosd(360*(r-1)/(M-1)*m)-(r-1)*cosd(360*r/(M-1)*m),r*sind(360*(r-1)/(M-1)*m)-(r-1)*sind(360*r/(M-1)*m)) endfor; W:=ceiling(u/32*65); % Width of each figure's bounding box H:=W; % Height of each figure's bounding box path q; \stopMPinclusions \starttext \dorecurse{\luaexpr{\K*\N}}{ \startMPpage m:=floor((\recurselevel-1)/N); n:=\recurselevel-m*N-1; numeric s[]; t:=n/N; q:=(((1-r)*cosd(360*r*t),(1-r)*sind(360*r*t))--((1+r)*cosd(360*(r-1)*t)-(r-1)*cosd(360*r*t),(1+r)*sind(360*(r-1)*t)-(r-1)*sind(360*r*t))); (s1,s2) = p intersectiontimes reverse q; for j=0 upto (m-1): draw p scaled u rotated (360*r*j) withcolor red; endfor; draw subpath (0,s1) of p scaled u rotated (360*r*m) withcolor red; draw fullcircle scaled (2*u); draw fullcircle scaled (2*r*u) shifted ((1-r)*u*cosd(360*r*t),(1-r)*u*sind(360*r*t)) rotated (360*r*m); draw subpath (s2,length q) of (reverse q) scaled u rotated (360*r*m); setbounds currentpicture to (-W/2,-H/2)--(W/2,-H/2)--(W/2,H/2)--(-W/2,H/2)--cycle; \stopMPpage } \stoptext
On 2012–12–19 Troy Henderson wrote:
In case anyone cares, here is the "finished" product which is a frame-by-frame animation of a hypocycloid. The following code will produce a 190 page PDF where each page is an individual frame of the animation.
[…]
Very nice work, indeed. It reminds me of a drawing toy I had when I was a kid. Thanks a lot for sharing. Marco
Very nice work, indeed. It reminds me of a drawing toy I had when I was a kid. Thanks a lot for sharing.
Spirograph? http://en.wikipedia.org/wiki/Spirograph Troy
Here's the animated (video) version. http://www.youtube.com/watch?v=2_OZ-yKMd7g Troy
Here's the animated (video) version.
How did you produce a video out of the PDF?
How did you produce a video out of the PDF?
I wrote a BASH script to automate this process, but essentially it involves converting each "frame"/page of the PDF to JPG and then combining the JPG sequence into a MP4 movie. The hypocycloid example has 191 frames, and so I did something like: let n=0; while [ $n -lt 191 ] do convert -density 720 hypocycloid.pdf[$n] -resize "x720" -flatten -gravity center -extent "1280x720" -quality 100 hypocycloid-$n.jpg done ffmpeg -r 13 -i hypocycloid-%d.jpg -b 5000k -vcodec libx264 -y hypocycloid.mp4
On 2012–12–19 Troy Henderson wrote:
Very nice work, indeed. It reminds me of a drawing toy I had when I was a kid. Thanks a lot for sharing.
Spirograph?
Exactly, that's it. Plenty of beautiful patterns with just small change in parameters. Maybe this can be adapted for (non-circular) ornaments as well, which could be used for text decorations. It would make for a nice module. Marco
participants (4)
-
Andre Caldas
-
Hans Hagen
-
Marco Patzer
-
Troy Henderson