<?php

  ///////////////////////////////////////////
  //
  // jsp.php
  // a set of wrapper functions for the java sketch pad app.
  // these allow the use of variables to track references,
  // which should allow for cleaner developmet by hand.
  // eventually, error check and diagnostics may be added.
  // the functions map pretty closely to the jsp opersations
  // described at http://www.keypress.com/sketchpad/java_gsp/dr_grammar.html
  //
  ///////////////////////////////////////////
  // Copyright (c) 2003-2004 Friedrich G. Knauss
  //
  // This program is free software; you can redistribute it and/or
  // modify it under the terms of the GNU General Public License
  // as published by the Free Software Foundation; either version 2
  // of the License, or (at your option) any later version.
  // 
  // This program is distributed in the hope that it will be useful,
  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  // GNU General Public License for more details.
  //
  // The GNU General Public License is available at http://www.gnu.org/copyleft/gpl.html
  ///////////////////////////////////////////

  //Set functions must be called before JspStart();
  // aside from "align", they correspond to the
  // available PARAMs, aside from "construction"

  $JspSet["Align"]="left";

  function JspSetFrame($p) { global $JspSet; $JspSet["Frame"]=$p; }
  
  function JspSetAlign ( $p="left") { global $JspSet; $JspSet["Align"]=$p; }
  
  function JspSetDoublebuf($p=1) { global $JspSet; $JspSet["Offcreen"]=$p; }

  function JspSetMeasureInDegrees($p=0) { global $JspSet; $JspSet["MeasureInDegrees"]=$p; }

  function JspSetDirectedAngles($p=0) { global $JspSet; $JspSet["DirectedAngles"]=$p; }

  function JspSetBg($r=200,$g=200,$b=200)
  {
    global $JspSet;
    $JspSet["BackRed"]=$r;
    $JspSet["BackGreen"]=$g;
    $JspSet["BackBlue"]=$b;
  }
  function JspSetLabel($f="Helvetica",$s=12,$b=0,$i=0)
  {
    global $JspSet;
    $JspSet["LabelFont"]=$f;
    $JspSet["LabelSize"]=$s;
    $JspSet["LabelBold"]=$b;
    $JspSet["LabelItalic"]=$i;
  }

  function JspStart( $width, $height, $codebase= "jsp")
  {
    global $JspSet;
    $align=$JspSet["Align"];
    echo "<APPLET CODE=\"GSP.class\" CODEBASE=\"$codebase\" ";
    echo "ARCHIVE=\"jsp4.JAR\"\n";
    echo "WIDTH=\"$width\" HEIGHT=\"$height\" ALIGN=\"$align\">\n";

    foreach ($JspSet as $key => $val)
    {
       if ($key != "Align")
       {
         if (is_string($val))
         {
           echo "<PARAM NAME=$key VALUE=\"$val\">\n";
         }else{
           echo "<PARAM NAME=$key VALUE=$val>\n";
         }
      }
    }
    echo "\n<PARAM NAME=Construction VALUE=\"\n";
  }
 
  function JspEnd ()
  {
    echo "\">\n</APPLET>\n";
  }

  // support functions

  $JspObjectCount = 0;
  function JspNextObjectId()
  {
    global $JspObjectCount;
    printf("; {%d}\n", ++ $JspObjectCount);
    return $JspObjectCount;
  }

  function JspOptions($options)
  {
    if ($options != NULL)
      return "[$options]";
    else
      return NULL;
  }

  function JspArrayAsList($p)
  {
    foreach ($p as $param)
    {
      $v .= $param.", ";
    }
    return substr($v, 0, -2);
  }

  // point objects;
  function JspPoint($x,$y,$opt=NULL)
  {
    echo "Point($x, $y)",JspOptions($opt);
    return JspNextObjectId();
  }

  function JspFixedPoint($x,$y,$opt=NULL)
  {
    echo "FixedPoint($x, $y)",JspOptions($opt);
    return JspNextObjectId();
  }

  function JspMidPoint($o,$opt=NULL)
  {
    echo "Midpoint($o)",JspOptions($opt);
    return JspNextObjectId();
  }

  function JspPointOnObject($o, $f,$opt=NULL)
  {
    echo "Point on object($o, $f)",JspOptions($opt);
    return JspNextObjectId();
  }

  function JspIntersect($a, $b,$opt=NULL)
  {
    echo "Intersect($a, $b)",JspOptions($opt);
    return JspNextObjectId();
  }

  function JspIntersect1($a, $b,$opt=NULL)
  {
    echo "Intersect1($a, $b)",JspOptions($opt);
    return JspNextObjectId();
  }

  function JspIntersect2($a, $b,$opt=NULL)
  {
    echo "Intersect2($a, $b)",JspOptions($opt);
    return JspNextObjectId();
  }
  
  //note the weirdness that the coordinate system is the *second* parameter
  function JspPlotXY($m1,$c,$m2,$opt=NULL)
  {
    echo "PlotXY($m1, $c, $m2)",JspOptions($opt);
    return JspNextObjectId();
  }

  function JspUnitPoint($p,$d,$opt=NULL)
  {
    echo "UnitPoint($p, $d)",JspOptions($opt);
    return JspNextObjectId();
  }

  function JspDriverPoint($x,$y,$opt=NULL)
  {
    echo "DriverPoint($x, $y)",JspOptions($opt);
    return JspNextObjectId();
  }

  // line objects
  function JspSegment($p1,$p2, $opt=NULL)
  {
    echo "Segment($p1, $p2)",JspOptions($opt);
    return JspNextObjectId();
  }
  
  function JspRay($p1,$p2, $opt=NULL)
  {
    echo "Ray($p1, $p2)",JspOptions($opt);
    return JspNextObjectId();
  }
  
  function JspLine($p1,$p2, $opt=NULL)
  {
    echo "Line($p1, $p2)",JspOptions($opt);
    return JspNextObjectId();
  }
 
  function JspPerpendicular($p,$l, $opt=NULL)
  {
    echo "Perpendicular($p, $l)",JspOptions($opt);
    return JspNextObjectId();
  }
 
  function JspAxisX($c, $opt=NULL)
  {
    echo "AxisX($c)",JspOptions($opt);
    return JspNextObjectId();
  }
 
  function JspAxisY($c, $opt=NULL)
  {
    echo "AxisY($c)",JspOptions($opt);
    return JspNextObjectId();
  }
 
  // Interiors
  function JspPolygon($a, $opt=NULL)
  {
    echo "Polygon(";
    echo JspArrayAsList($a);
    echo ")", JspOptions($opt);
    return JspNextObjectId();
  }
  // circles
  function JspCircle($c,$e, $opt=NULL)
  {
    echo "Circle($c, $e)",JspOptions($opt);
    return JspNextObjectId();
  }

  function JspCircleByRadius($c,$r, $opt=NULL)
  {
    echo "Circle by radius($c, $r)",JspOptions($opt);
    return JspNextObjectId();
  }

  // transformations
  function JspReflection($o,$l, $opt=NULL)
  {
    echo "Reflection($o, $l)",JspOptions($opt);
    return JspNextObjectId();
  }

  function JspDilation($o,$p, $n, $opt=NULL)
  {
    echo "Dilation($o, $p, $n)",JspOptions($opt);
    return JspNextObjectId();
  }

  function JspRotation($o,$p, $a, $opt=NULL)
  {
    echo "Rotation($o, $p, $a)",JspOptions($opt);
    return JspNextObjectId();
  }

  function JspTranslation($o, $x, $y, $opt=NULL)
  {
    echo "Translation($o, $x, $y)",JspOptions($opt);
    return JspNextObjectId();
  }

  function JspVecTranslation($o, $s, $e, $opt=NULL)
  {
    echo "VectorTranslation($o, $s, $e)",JspOptions($opt);
    return JspNextObjectId();
  }



  // images
  // measurements
  function JspRatioSegments($l1, $l2,$x,$y,$t, $opt=NULL)
  {
    echo "Ratio/Segments($l1, $l2, $x, $y, '$t')",JspOptions($opt);
    return JspNextObjectId();
  }

  function JspSlope($l, $x, $y, $t, $opt=NULL)
  {
    echo "Slope($l, $x, $y, '$t')",JspOptions($opt);
    return JspNextObjectId();
  }

  function JspAngle($a, $b, $c, $x, $y, $t, $opt=NULL)
  {
    echo "Angle($a, $b, $c, $x, $y, '$t')",JspOptions($opt);
    return JspNextObjectId();
  }

  function JspCoordinates($p, $c, $x, $y, $t, $opt=NULL)
  {
    echo "Coordinates($p, $c, $x, $y, '$t')",JspOptions($opt);
    return JspNextObjectId();
  }

  function JspCalculate($x, $y, $t, $f, $p, $opt=NULL)
  {
    echo "Calculate($x, $y, '$t', '$f')(";
    echo JspArrayAsList($p);
    echo ")", JspOptions($opt);
    return JspNextObjectId();
  }

  //buttons
  // coordinate systems
  function JspOriginAndUnit($o, $u, $opt=NULL)
  {
    echo "Origin&Unit($o, $u)",JspOptions($opt);
    return JspNextObjectId();
  }

  //loci
  // captions
  function JspConcatText($x, $y, $p, $opt=NULL)
  {
    echo "ConcatText($x, $y)(";
    echo JspArrayAsList($p);
    echo ")", JspOptions($opt);
    return JspNextObjectId();

  }

?>