' ******************************************************************
' Plotting routines : initializations and general functions
' ******************************************************************

' ------------------------------------------------------------------
' Global variables
' ------------------------------------------------------------------

COMMON SHARED AS INTEGER CharWidth, CharHeight
' Character Width and Height in pixels

COMMON SHARED AS INTEGER XminPixel, YminPixel, XmaxPixel, YmaxPixel
' Window coordinates in pixels

COMMON SHARED AS DOUBLE Xmin, Ymin, Xmax, Ymax
' Window coordinates in user units

COMMON SHARED AS DOUBLE FactX, FactY
' Scale factors

COMMON SHARED AS INTEGER Clip
' Clipping

COMMON SHARED AS INTEGER FgCol, BgCol
' Save foreground and background colors

' ******************************************************************

FUNCTION InitScreen(Mode    AS INTEGER = 12, _
                    FgColor AS INTEGER = 0, _
                    BgColor AS INTEGER = 15) AS INTEGER
' ------------------------------------------------------------------
' Initializes the graphic mode
' ------------------------------------------------------------------

  SCREEN Mode
  IF SCREENPTR = 0 THEN InitScreen = 0 : EXIT FUNCTION

  ' Obtain info about current mode
  SCREENINFO XmaxPixel, YmaxPixel

  XminPixel = 0
  YminPixel = 0

  CharWidth = 8

  SELECT CASE Mode
    CASE 1, 2, 7, 8, 13, 14, 15
      CharHeight = 8
    CASE 9
      CharHeight = 14
    CASE ELSE
      CharHeight = 16
  END SELECT

  COLOR FgColor, BgColor

  CLS

  FgCol = FgColor
  BgCol = BgColor

  InitScreen = -1
END FUNCTION

SUB InitGraph(X1     AS INTEGER = 15, _
              X2     AS INTEGER = 85, _
              Y1     AS INTEGER = 15, _ 
              Y2     AS INTEGER = 85, _
              Border AS INTEGER = -1)
' ------------------------------------------------------------------
' Initializes the graphic window
' ------------------------------------------------------------------
' Input parameters : 
'
' X1, X2, Y1, Y2 : window coordinates in % of maximum
' Border         : -1 to plot a border around the graphic window,
'                   0 otherwise
' ------------------------------------------------------------------

  DIM AS DOUBLE R

  IF X1 >= 0 AND X2 <= 100 AND X1 < X2 THEN
    R = 0.01 * XmaxPixel
    XminPixel = X1 * R
    XmaxPixel = X2 * R
  END IF

  IF Y1 >= 0 AND Y2 <= 100 AND Y1 < Y2 THEN
    R = 0.01 * YmaxPixel
    YminPixel = Y1 * R
    YmaxPixel = Y2 * R
  END IF

  IF Border THEN
    LINE (XminPixel, YminPixel)-(XmaxPixel, YmaxPixel), , B
  END IF
END SUB

FUNCTION Xpixel(X AS DOUBLE) AS INTEGER
' ------------------------------------------------------------------
' Converts user abscissa X to screen coordinate
' ------------------------------------------------------------------

  DIM AS INTEGER Xp

  Xp = (X - Xmin) * FactX
  IF Clip = 0 THEN Xp = Xp + XminPixel
  Xpixel = Xp
END FUNCTION

FUNCTION Ypixel(Y AS DOUBLE) AS INTEGER
' ------------------------------------------------------------------
' Converts user ordinate Y to screen coordinate
' ------------------------------------------------------------------

  DIM AS INTEGER Yp

  Yp = (Ymax - Y) * FactY
  IF Clip = 0 THEN Yp = Yp + YminPixel
  Ypixel = Yp
END FUNCTION

SUB SetClipping
' ------------------------------------------------------------------
' Limits the plots at the current viewport boundaries
' ------------------------------------------------------------------

  VIEW (XminPixel, YminPixel)-(XmaxPixel, YmaxPixel)
  Clip = -1
END SUB

SUB CloseGraph
' ------------------------------------------------------------------
' Quits the graphic mode
' ------------------------------------------------------------------

  Clip = 0
  SLEEP
  SCREEN 0
END SUB

