Variations sur un puzzle

En utilisant les directives de compilation il est possible, à partir du même fichier, de produire des images différentes.

Animation flash

puzzle.cc [ source ]
#include "epix2.h"
using namespace ePiX2;

const int n(8);

int sign(const int i)
{
  return (i%2 == 0) ? 1 : -1;
}

int rot(int i, int j)
{
#ifdef P1
  return gcd(i, j); // nearly random
#endif
#ifdef P2
  return i+2*j;   // herringbone
#endif
#ifdef P3
  return (i+j)%2; // fish scales
#endif
#ifdef P4
  return 0;       // saw teeth
#endif
#ifdef P5
  return sign(i+j); // candystripes
#endif
#ifdef P6
  return sign(i);   // parallelograms
#endif
#ifdef P7
  return (i+j)%4; // envelopes
#endif
#ifdef P8
  return 2+(i+j)%3; // stairs
#endif
#ifdef P9
  return (i+2*j)%3; // rug
#endif
#ifdef P10
  return (int) floor(0.5*i-0.5*j)%3; // ice cube tray
#endif
}

World world;
Point mm(-1,-1), pm(1,-1), pp(1,1), mp(-1,1);

Polygon T1(mm, pm, pp), T2(mm, pp, mp);

void piece(const Vector& dX, const int n)
{
  Polygon t1(T1), t2(T2);
  world << t1.rotate(n*M_PI_2, E_3()).move_by(dX)
	<< t2.rotate(n*M_PI_2, E_3()).move_by(dX);
}

int main() {

  Picture picture(Pair(0,0), Pair(2*n, 2*n), "6x6in");
  Camera camera;

  // colors
  T1.paint(Blue(1.5)), T2.paint(Blue(0.6));

  // place pieces
  for (int i=0; i<n; ++i)
    for (int j=0; j<n; ++j)
      piece(Vector(Origin, 2*i+1, 2*j+1, 0), rot(i,j));

  world.photo(picture, camera);
  picture.print_to("puzzle.eepic", PSTricks());
}