/* PatchWork   allow creation of posters with MathVision 12-Feb-90 dh

  This program lets you make posters with MathVision, by plotting a big
picture in segments, which you can print out and tape together.

  To use this program, load MathVision with the picture you want to blow up. 
It is a good idea to have this picture saved on disk for future reference.

+---+---+---+    Units per Side determines how big your poster will be.
|1,1|2,1|3,1|  For example, with three units per side, it will contain
+---+---+---+  3 * 3 screens, or 9.
|1,2|2,2|3,2|
+---+---+---+    Overlap determines how much extra is printed on the sides
|1,3|3,2|3,3|  to simplify the trimming and matching processes.  Since the
+---+---+---+  segments are printed in vertical strips, it is useful to 
               have no overlap on the Y axis if your printer prints
successive screens with no gaps between.

  There are two basic ways to use this program.  You can Print while 
Plotting, which is a quick and dirty way to test your setup.  Or, you 
can have the pictures saved on disk, to be printed later with the Print
from Disk option.

  If you could not complete the calculations in one session, you may change
the starting coordinates.  Load the original picture, and give the next 
frame to compute, as shown in the above diagram.

TIPS FOR PRINTING:
- If you are using a dot-matrix printer, use the tractor feed if possible.
This gives much more accurate registration when printing long vertical
strips.

============================================================================ */

ADDRESS "MathVision"
OPTIONS RESULTS
NUMERIC DIGITS 14
OPTIONS FAILAT 1
SIGNAL ON ERROR

ClrScr = D2C(12)
StopSign "F"

Action = "T"

Patches = 2
XOverlap = 2
YOverlap = 0
BaseFileName = "Patches:Pic"

DO WHILE command ~= "X"
  command = MENU()

  SELECT
    WHEN command ='P' THEN
      DO
        CALL Generate_Pictures('P')
      END
    WHEN command ='D' THEN
      DO
        CALL Generate_Pictures('D')
      END
    WHEN command ='S' THEN
      DO
        CALL Generate_Pictures('S')
      END
    WHEN command ='O' THEN
      DO
        Xoverlap = GetAns("Percent Overlap on X Axis",XOverlap )
        Yoverlap = GetAns("Percent Overlap on Y Axis",YOverlap )
      END
    WHEN command ='F' THEN
      DO
        BaseFilename = GetAns("Base Filename", BaseFilename )
      END
    WHEN command ='U' THEN
      DO
        Patches = GetAns("Units on a Side", Patches )
        if (Patches<2) THEN Patches = 2
      END
    OTHERWISE say command;
  END /*select*/
END

EXIT

/*================================== MENU ================================= */

MENU:
  SAY ClrScr
  SAY "U - Units per side   ("Patches", Total: "patches*patches")"
  SAY "O - Overlap Percent  (X "Xoverlap"%    Y "Yoverlap"%)"
  SAY "F - Filename         ("BaseFileName")"
  SAY "P --  Print while Plotting"
  SAY "S --  Save to Disk while Plotting"
  SAY "D --  Print from Disk"
  SAY "X - Exit"
  SAY ""
  OPTIONS PROMPT "--------------> "
  PULL Command

  RETURN command

/*------------------------------- Generate_Pictures ------------------------ */
Generate_pictures:

Arg Action 

StartX = GetAns("Starting X Coordinate",1)-1
StartY = GetAns("Starting Y Coordinate",1)-1

Get XMin; LeftX   = RESULT
Get XMax; RightX  = RESULT
Get YMin; BottomY = RESULT
Get YMax; TopY    = RESULT

Xspan = (RightX-LeftX)/Patches
Yspan = (TopY-BottomY)/Patches
divr = Patches-1
Stopit = "F"
ReachedStart = "F"

DO across = 0 to divr
  DO down = 0 to divr
    IF ((ReachedStart = "F") & ((StartX=Across)&(StartY=Down))) THEN ReachedStart = "T";
    IF (ReachedStart = "T")
    THEN
      DO
        XMin LeftX+ (across  )*XSpan - XOverlap/100*XSpan
        XMax LeftX+ (across+1)*XSpan + XOverlap/100*XSpan
        YMax TopY - (down    )*YSpan + YOverlap/100*YSpan
        YMin TopY - (down+1  )*YSpan - YOverlap/100*YSpan

        Filename = BaseFileName||".X"Right(across+1,2,"0")".Y"Right(down+1,2,"0")

        SAY "X"Across+1" Y"Down+1

        SELECT
          WHEN Action = "P" THEN
            DO
              PlotContour
              if Stopit() THEN BREAK;
              PrintScreen
            END
          WHEN Action = "S" THEN
            DO
              PlotContour
              if Stopit() THEN BREAK;
              Pathname Filename
              SavePicture
            END
          WHEN Action = "D" THEN
            DO
              Pathname Filename
              LoadPicture
              PrintScreen
            END
        END
        if Stopit() THEN BREAK;
    END
  END
  if Stopit() THEN BREAK;
END 

StopSign "F"   /* reset to normalcy */

XMin LeftX
XMax RightX
YMin BottomY
YMax TopY

RETURN 

/* --------------------------------- GetAns -------------------------------- */
/* result = GetAns( prompt, default) */
/* prompt for input, displaying default. Accept one item. If only <return> */
/* was pressed, return the default answer */

GetAns:
  parse arg prompter, default
  Options Prompt Prompter "(" Default "): "
  pull response
  if (response = "") THEN response = default
  return(response)

/*------------------------------- StopIt -------------------------------- */
/* Ask MathVision if we should stop, return boolean result */
StopIt:

  ret = 0;
  Get StopSign
  if (Result = "T") THEN ret = 1;
  RETURN(ret);

/*-------------------------------- ERROR -------------------------------- */

ERROR:         /* Error Diagnostic for return codes */
  Get Diagnosis RC
  SAY RESULT" on line "SIGL
  DO i = 1 to 5000
  END
  EXIT
