
#include "bits/bit-paper-fold.h"

#include "aux0/swap.h"

#include "demo/nextarg.h"
#include "fxttypes.h"  // ulong


//% TeX picture of the dragon curve.


ulong style = 0;

void draw_turn(double x, double y, double dx, double dy, bool f)
{
    double nx = x+dx,  ny = y + dy;  // next (x,y)
    double ndx=dy,  ndy=dx;          // next (dx,dy)
    if ( f )  ndx = -ndx;
    else      ndy = -ndy;
//    double nnx = nx+ndx,  nny = ny + ndy;  // next next (x,y)
    double m = 0.25,  m2 = 0.75;
    double x1 = x+m*dx,  y1 = y+m*dy;
    double x2 = x+m2*dx,  y2 = y+m2*dy;
//    double x3 = nx+m*ndx,  y3 = ny+m*ndy;
//    double x4 = nx+m2*ndx,  y4 = ny+m2*dy;


// For \line(dx,dy){length} and \vector(dx,dy}{length}
// NOTE: The argument 'length' is the VERTICAL coordinate for
// vertical line segments, else the HORIZONTAL coordinate.

    switch ( style )
    {
    case 0:  // as in fxtbook (roundish corners, default)
    // LINE  orig=(x1,y1)  dir=(dx,dy)         len=0.50
    cout << "\\put(" << x1 << "," << y1 << ")"
         << "{\\line(" << dx <<","<< dy << ")"
         << "{" << 0.5 << "}}" << endl;
    // LINE  orig=(x2,y2)  dir=(dx+ndx,dy+ndy) len=0.25
    cout << "\\put(" << x2 << "," << y2 << ")"
         << "{\\line(" << dx+ndx <<","<< dy+ndy << ")"
         << "{" << 0.25 << "}}" << endl;
    // LINE  orig=(x3,y3)  dir=(ndx,ndy )      len=0.50
//    cout << "\\put(" << x3 << "," << y3 << ")"
//         << "{\\line(" << ndx <<","<< ndy << ")"
//         << "{" << 0.5 << "}}" << endl;
    break;


    case 1:  // edgy (second figure in fxtbook)
    // LINE  orig=( 0.5*(x2+x1), 0.5*(y2+y1) )  dir=(dx+ndx, dy+ndy )   len=0.50
    cout << "\\put(" << 0.5*(x2+x1) << "," << 0.5*(y2+y1) << ")"
         << "{\\line(" << dx+ndx <<","<< dy+ndy << ")"
         << "{" << 0.5 << "}}" << endl;
    break;


    case 2:  // disks
    cout << "\\put(" << x << "," << y << ")" << "{\\circle{0.5}}" << endl;

    case 3:  // unrounded corners
    // LINE  orig=(x,y)    dir=(dx,dy)         len=1.0
    cout << "\\put(" << x << "," << y << ")"
         << "{\\line(" << dx <<","<< dy << ")"
         << "{" << 1.0 << "}}" << endl;
    // LINE  orig=(nx,ny)  dir=(ndx,ndy)       len=1.0
    cout << "\\put(" << nx << "," << ny << ")"
         << "{\\line(" << ndx <<","<< ndy << ")"
         << "{" << 1.0 << "}}" << endl;


    default: // playground

//    cout << "\\put(" << x << "," << y << ")" << "{\\circle{0.02}}" << endl;

    break;
    }
}
// -------------------------


int
main(int argc, char **argv)
{
    ulong n = 64;
    NXARG(n,"Number of moves (fxtbook uses n=1024)");

    NXARG(style,"Drawing style (fxtbook uses style=0)");
//    cout << "%% style=" << style << endl;

    ulong w = 0;
    NXARG(w,"If !=0 then draw curve from paper-folding sequence with control world w");
//    cout << "%% style=" << style << endl;
    if ( 0==w )  w = ~0UL;


    double x = 0, y = 0;
    cout << "\\put(" << x << "," << y << ")" << "{\\circle{0.5}}" << endl;
    cout << "\\put(" << x << "," << y << ")" << "{\\circle{0.25}}" << endl;

    double dx = 1,  dy = 0;  // initial direction

    double ix=0, ax=0, iy=0, ay=0;  //  min/max  x/y
    for (ulong t=1; t<=n; ++t)  // note: start with k=1
    {
//        bool f;
//        if ( !alt )  f = bit_paper_fold(t);
//        else         f = bit_paper_fold_alt(t);
        const bool f = bit_paper_fold_general(t, w);

//        { ulong m = bit_dragon_rot(t);
//        switch ( m )
//        {
//        case 5:
//            cout << "\\put(" << x << "," << y << ")" << "{\\circle{0.5}}" << endl;
//            break;
//        case 6:
//            cout << "\\put(" << x << "," << y << ")" << "{\\circle*{0.5}}" << endl;
//            break;
//        default:
//            draw_turn(x,y, dx,dy, f);
//        }
//
//        }

        draw_turn(x,y, dx,dy, f);

        // update max/min:
        if ( x<ix )  ix = x;  if ( x>ax )  ax = x;
        if ( y<iy )  iy = y;  if ( y>ay )  ay = y;


        x += dx;  y += dy;  // move
        // turn:
        swap2(dx, dy);
        if ( f )  dx = -dx;
        else      dy = -dy;
    }

//    cout << "% ix = " << ix << endl;
//    cout << "% ax = " << ax << endl;
//    cout << "% iy = " << iy << endl;
//    cout << "% ay = " << ay << endl;
    double wd = ax-ix, hg = ay-iy;
    double xo = ix, yo = iy;
//    cout << "% width = " << wd << endl;
//    cout << "% height = " << hg << endl;
//    cout << "% xoff = " << xo << endl;
//    cout << "% yoff = " << yo << endl;

    cout << "%% Put next line (un-commented) on top of pic:" << endl;
    cout << "%\\begin{picture}("
         << wd+1 <<"," << hg+1 << ")"
         <<"("<< xo <<".5," << yo <<".5)" << endl;

    return 0;
}
// -------------------------

/*
Embedding LaTeX code is:

\begin{figure}
\begin{center}
//\setlength{\unitlength}{4.5mm} % fxtbook, 1024 moves
\setlength{\unitlength}{10.0mm}
%%\begin{picture}(width,height)(x offset,y offset)
\begin{picture}(8,12)(-5.5,-9.5) % 64 moves
%\begin{picture}(16,13)(-5.5,-10.5) % 128 moves
%\begin{picture}(24,16)(-5.5,-10.5) % 256 moves
%\begin{picture}(27,32)(-5.5,-10.5) % 512 moves
%\begin{picture}(32,48)(-10.5,-10.5) % 1024 moves (fxtbook)
  \thicklines
 [OUTPUT of the program HERE] <--= there!
\end{picture}
\end{center}
\caption{\jjlabel{fig:dragon-curve}
The dragon curve.}
\end{figure}
*/


/// Emacs:
/// Local Variables:
/// MyRelDir: "demo/bits"
/// makefile-dir: "../../"
/// make-target: "1demo DSRC=demo/bits/dragon-curve-texpic-demo.cc"
/// make-target2: "1demo DSRC=demo/bits/dragon-curve-texpic-demo.cc DEMOFLAGS=-DTIMING"
/// End:

