• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Rolling a square in a square

Matthen Jan 14th, 2012 630 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. RollSub[p_, dir_, \[Theta]_] := Module[{sq, rp},
2.    sq = {p, p + dir, p + dir + RotationMatrix[Pi/2].dir,
3.      p + RotationMatrix[Pi/2].dir};
4.    sq = Map[RotationMatrix[-\[Theta]].(# - (p + dir)) + (p + dir) &,
5.      sq];
6.    sq = Map[# &, sq]
7.    ];
8. Roll[p_, dir_, \[Theta]_] :=
9.   Module[{n = Floor[2 \[Theta]/Pi], sq0, sq},
10.    sq0 = RollSub[p, dir, 0];
11.    sq = Nest[RotateLeft[RollSub[#[[1]], dir, Pi/2]] &, sq0, n];
12.    RollSub[sq[[1]], dir, \[Theta] - n Pi/2]
13.    ];
14. RollAround[\[Theta]_] := Module[{p, dir, th, n, m},
15.   n = Mod[Floor[\[Theta]/Pi], 4] + 1;
16.   p = {{0, 0}, {3, 0}, {3, 3}, {0, 3}}[[n]];
17.   dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}[[n]];
18.   th = \[Theta] - Pi (n - 1);
19.   m = Mod[Floor[2 \[Theta]/Pi], 4] + 1;
20.   Nest[RotateRight, Roll[p, dir, th],
21.    1/4 (-3 - (-1)^m + 6 m) + 2 Floor[(n + 1)/2]]
22.   ];
23. Manipulate[
24.  Show[Graphics[{
25.     EdgeForm[Directive[Thick, RGBColor[0.1, 0.2, 0.1]]], FaceForm[],
26.     Rectangle[{0, 0}, {3, 3}],
27.     EdgeForm[Thick],
28.     Polygon[RollAround[Mod[\[Theta], 4 Pi]]]
29.     }, PlotRange -> {{-0.1, 3.1}, {-0.1, 3.1}}],
30.
31.   ParametricPlot[
32.    RollAround[t], {t, If[\[Theta] < 4 Pi, 0, Mod[\[Theta], 4 Pi]],
33.     If[\[Theta] < 4 Pi, Max[\[Theta], 0.01], 4 Pi - 0.01]},
34.    PlotStyle -> RGBColor[0.450385`, 0.157961`, 0.217975`]]
35.   , ImageSize -> 320]
36.  , {\[Theta], 0, 8 Pi - 0.01}]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top