12
$\begingroup$

I have the following that works good, and computes in one shot,but the code is so long. Can someone please help me to reduce the size of this?

n01=Flatten[{{{5,0,0},{6,0,0},{6,0,1},{6,0,2},{5,0,2},{4,0,2},{4,0,1},{4,0,0},{5,0,0}}},1];
n02={0,0,0.5}+RotationMatrix[45Degree,{0,0,1}].Transpose[%]//Transpose//N;
n03={0,0,0.5}+RotationMatrix[45Degree,{0,0,1}].Transpose[%]//Transpose//N;
n04={0,0,0.5}+RotationMatrix[45Degree,{0,0,1}].Transpose[%]//Transpose//N;
n05={0,0,0.5}+RotationMatrix[45Degree,{0,0,1}].Transpose[%]//Transpose//N;
n06={0,0,0.5}+RotationMatrix[45Degree,{0,0,1}].Transpose[%]//Transpose//N;
n07={0,0,0.5}+RotationMatrix[45Degree,{0,0,1}].Transpose[%]//Transpose//N;
n08={0,0,0.5}+RotationMatrix[45Degree,{0,0,1}].Transpose[%]//Transpose//N;
n09={0,0,0.5}+RotationMatrix[45Degree,{0,0,1}].Transpose[%]//Transpose//N;
n10={0,0,0.5}+RotationMatrix[45Degree,{0,0,1}].Transpose[%]//Transpose//N;
n11={0,0,0.5}+RotationMatrix[45Degree,{0,0,1}].Transpose[%]//Transpose//N;
n12={0,0,0.5}+RotationMatrix[45Degree,{0,0,1}].Transpose[%]//Transpose//N;
n13={0,0,0.5}+RotationMatrix[45Degree,{0,0,1}].Transpose[%]//Transpose//N;
n14={0,0,0.5}+RotationMatrix[45Degree,{0,0,1}].Transpose[%]//Transpose//N;
n15={0,0,0.5}+RotationMatrix[45Degree,{0,0,1}].Transpose[%]//Transpose//N;
n16={0,0,0.5}+RotationMatrix[45Degree,{0,0,1}].Transpose[%]//Transpose//N;
n17={0,0,0.5}+RotationMatrix[45Degree,{0,0,1}].Transpose[%]//Transpose//N;
n18={0,0,0.5}+RotationMatrix[45Degree,{0,0,1}].Transpose[%]//Transpose//N;
n19={0,0,0.5}+RotationMatrix[45Degree,{0,0,1}].Transpose[%]//Transpose//N;
allPts=List[n01,n02,n03,n04,n05,n06,n07,n08,n09,n10,n11,n12,n13,n14,n15,n16,n17,n18,n19];

This generates control points for a B-Spline Surface.

I'm using Mathematica 8.0.4 on Windows 7 Professional.

$\endgroup$
  • $\begingroup$ Look up NestList. $\endgroup$ – John Doty Jul 21 '17 at 16:58
  • 2
    $\begingroup$ A... code-golf... that's a code-review... that's on neither site... +1 $\endgroup$ – HyperNeutrino Jul 22 '17 at 1:29
  • $\begingroup$ @HyperNeutrino I so clicked on that one.. $\endgroup$ – Roman Gräf Jul 22 '17 at 17:34
  • $\begingroup$ Closely related, perhaps duplicate: (146937) $\endgroup$ – Mr.Wizard Jul 23 '17 at 8:38
12
$\begingroup$

since the result of the new computation depends on the previous one we can use NestList

NestList[N@({0, 0, 0.5} + 
  RotationMatrix[45 Degree, {0, 0, 1}].Transpose[#])\[Transpose] &, n01, 18]
|improve this answer|||||
$\endgroup$
  • $\begingroup$ Thanks Ali, and to all who helped. $\endgroup$ – Bill W. Jul 22 '17 at 17:15
7
$\begingroup$

Another one using NestList:

tf = TranslationTransform[{0, 0, 0.5}].RotationTransform[45 Degree, {0, 0, 1}];

allPts = NestList[tf, n01, 18]
|improve this answer|||||
$\endgroup$
5
$\begingroup$

This is concise and I think it will perform a little better than the methods using Transpose.

n01 = 
  N[{{5, 0, 0}, {6, 0, 0}, {6, 0, 1}, {6, 0, 2}, {5, 0, 2}, {4, 0, 2}, {4, 0, 1}, {4, 0, 0}, {5, 0, 0}}];

xform =
  AffineTransform[{RotationMatrix[45. °, {0, 0, 1}], {0, 0, 0.5}}];

allPts = NestList[xform, n01, 18];

Short[allPts, 12]
{{{5., 0., 0.}, {6., 0., 0.}, {6., 0., 1.}, {6., 0., 2.}, {5., 0., 2.}, 
  {4., 0., 2.}, {4., 0., 1.}, {4., 0., 0.}, {5., 0., 0.}},
 <<17>>,
 {{0., 5., 9.}, {0., 6., 9.}, {0., 6., 10.}, {0., 6., 11.}, {0., 5., 11.}, 
  {0., 4., 11.}, {0., 4., 10.}, {0., 4., 9.}, {0., 5., 9.}}}
|improve this answer|||||
$\endgroup$
2
$\begingroup$

You can also use RecurrenceTable:

t = Composition[TranslationTransform[{0, 0, .5}], RotationTransform[45 Degree, {0, 0, 1}]];
RecurrenceTable[{a[n + 1] == t[a[n]], a[1] == n01}, a, {n, 1, 19}] // Short[#, 12] &

{{{5, 0, 0}, {6, 0, 0}, {6, 0, 1}, {6, 0, 2}, {5, 0, 2}, {4, 0, 2}, {4, 0, 1},{4, 0, 0}, {5, 0, 0}},
{{3.53553, 3.53553, 0.5}, {4.24264, 4.24264, 0.5}, {4.24264, 4.24264, 1.5}, {4.24264, 4.24264, 2.5}, {3.53553, 3.53553, 2.5},{2.82843, 2.82843, 2.5}, {2.82843, 2.82843, 1.5}, {2.82843, 2.82843, 0.5},{3.53553, 3.53553, 0.5}},
<<16>>,
{{0., 5., 9.}, {0., 6., 9.}, {0., 6., 10.},{0., 6., 11.}, {0., 5., 11.}, {0., 4., 11.}, {0., 4., 10.}, {0., 4., 9.}, {0., 5., 9.}}}

|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.