C  SUBROUTINE VECTOR:
C
C  13 JAN 77 (007; PDH) ADD EDIT NUMBER TO PROGRAM NAME
C  20 MAY 76 (PDH) RE-TYPE FROM LISTING, AS RK CARTRIDGE GOT CLOBBERED
C  14 MAY 76 (PDH( TIDY UP STATEMENT NUMBERS AND EXTRA VARIABLES
C  13 MAY 76 (PDH) CONVERT FOR USE WITH THE VT15
C   5 DEC 72 - MOD FOR WATRAN COMPATIBLITY: M.K.HOLMBERG
C   2 SEP 71 EDITED AND DEBUGGED BY JIM STORM
C  SOMETIME, 1969 - ORIGINALLY CODED BY J.A. FIELD FROM AN
C      EARLIER PROGRAM (VECGEN, VECEDG), WHICH WAS
C      THE CREATION OF J.A. FIELD AND STAN MIDDLESTADT.
C
C
C        THIS SUBROUTINE WILL PLOT POINTS, STRAIGHT LINES,
C        OR CURVES. IT PREVENTS LINES  BEING DRAWN OUTSIDE
C        THE SCREEN LIMITS, AND AS AN OPTION IT WILL TRIM
C        LINES TO FIT WITHIN A GIVEN WINDOW.
C
C
C        THE CALLING SEQUENCE IS:
C
C
C  CALL VECTOR(MODE,ARRAY,BEGIN,END,IVECT,FIRST,LAST,INT,ISPACE,LTPEN)
C
C        WHERE: ARRAY IS REAL
C               LTPEN IS LOGICAL
C               ALL OTHERS ARE INTEGER
C
C
C
C
C
C
C        THE DISPLAY FILE IS GENERATED STARTING AT IVECT(FIRST)
C        AND AFTER GENERATION, THE PROGRAM SETS 'LAST' SUCH
C        THAT 'IVECT(LAST)' IS THE LAST ELEMENT IN THE DISPLAY
C        FILE. 'INT' SPECIFIES THE DISPLAY INTENSITY (0-7),
C        'ISPACE' SPECIFIES POINT SPACING ON THE SCREEN (1,2,4,8)
C        RASTER UNIT. IF 'LTPEN=.TRUE.' THEN LIGHT PEN INTERRUPTS
C        ARE ENABLED.
C
C
C
C        WHEN (MODE=1) OR (MODE=2), 'ARRAY' IS A (4 BY N)
C        ARRAY SPECIFYING A SET AT LINES (X1,Y1,  X2,Y2),
C        AND ALL LINES BETWEEN 'ARRAY(1,BEGIN)' AND 'ARRAY(4,END)'
C        WILL BE PLOTTED.
C        FOR (MODE=2) ONLY THAT PART OF A LINE INSIDE A
C        WINDOW IS PLOTTED.
C
C
C
C
C        WHEN (MODE=3) OR (MODE=4) 'ARRAY' IS A (2 BY N)
C        ARRAY SPECIFYING THE (X,Y) PAIRS OF A CURVE. THE
C        CURVE BEGINS AT 'ARRAY(1,BEGIN)' AND ENDS AT
C        ARRAY(2,END).  FOR MODE=4, ONLY THAT PART OF THE
C        CURVE INSIDE THE WINDOW IS PLOTTED.
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C        WHEN (MODE=5) OR (MODE=6), 'ARRAY' IS A (2 BY N)
C        ARRAY SPECIFYING (X,Y) POINTS TO BE PLOTTED.
C        THE FIRST POINT IS AT 'ARRAY(1,FIRST),ARRAY(2,FIRST)
C        AND THE LAST IS AT 'ARRAY(1,END),ARRAY(2,END).
C         FOR MODE =6, ONLY THOSE POINTS INSIDE THE WINDOW
C        ARE PLOTTED.
C
C
C
C
C
C
C  WINDOW:
C
C        TO SPECIFY A WINDOW REQUIRES THE STATEMENT:
C
C        COMMON/FRAME/A,B,C,D
C
C        IN THE MAIN PROGRAM. THE WINDOW KEEPS-
C                                        A.LE.X.LE.B
C                                        C.LE.Y.LE.D
C
C        NOTE: THESE LIMITS ARE INDEPENDENT OF 'XMAX' ETC.,
C              THAT SPECIFY THE SCREEN SIZE. THERE IS NO NEED
C              FOR THE WINDOW TO BE COMPLETELY ON THE SCREEN.
C
C
C
C
C
C
C
C
      SUBROUTINE VECTOR (MODE,PICT,FIRST,LAST,IVECT,II,JJ,INT,ISPACE,
     *LTPEN)
      LOGICAL LTPEN,SWITCH
      INTEGER FIRST,OLDX,OLDY
      DIMENSION PICT(2,5000),IVECT(5000)
      COMMON /LIMITS/ XMIN,XMAX,YMIN,YMAX
      COMMON /FRAME/ A,B,C,D
      COMMON /LINEND/ XBEGIN,YBEGIN,XFINAL,YFINAL
  10  L = 1
      SWITCH = .FALSE.
      IF (MODE .LT. 1) STOP 12345
      IF (MODE .GT. 6) STOP 12345
      GO TO (101,102,103,104,105,106),MODE
  101 SWITCH = .TRUE.
  102 IFIRST = FIRST*2 - 1
      ILAST = LAST*2
      INCRE = 2
      GO TO 21
  105 SWITCH = .TRUE.
  106 L = 0
      ILAST = LAST
      GO TO 20
  103 SWITCH = .TRUE.
  104 ILAST = LAST - 1
  20  IFIRST = FIRST
      INCRE = 1
  21  OLDX = -1
      J = II + 1
C
C  PARAMETER 1: INTENSITY ENABLE BIT, SCALE 0 (202020 -> 66576);
C               THEN SET USER SPECIFIED INTENSITY LEVEL.
C
      INTX = INT
      IF (INT .LT. 0) INTX = 7
      IF (INT .GT. 7) INTX = 7
      IVECT(J) = 66576 + INTX*128
      J = J+1
C
C  PARAMETER 2: BLINK OFF (FOR INITIAL IMPLEMENTATION, EDGE
C               INTERRUPTS DISABLED, ROTATE HORIZONTAL, LIGHT
C               PEN ENABLE BIT, OFFSET OFF (211252 -> 70314);
C               THEN SET LIGHT PEN ON OR OFF, AS SPECIFIED.
C
      IVECT(J) = 70314
      IF (LTPEN) IVECT(J) = 70318
      J = J+1
C
C  PARAMETER 3: (ONLY PRESENT IF LIGHT PEN ON) LIGHT PEN INTERRUPT
C               ENABLE (220044 -> 73764).
C
      IF (.NOT. LTPEN) GO TO 30
      IVECT(J) = 73764
      J = J+1
  30  XLOW  = AMIN1 (XMIN,XMAX)
      XHIGH = AMAX1 (XMIN,XMAX)
      YLOW  = AMIN1 (YMIN,YMAX)
      YHIGH = AMAX1 (YMIN,YMAX)
      IF (SWITCH) GO TO 31
      XLOW  = AMAX1 (XLOW, AMIN1(AMIN1(A,B),XHIGH))
      XHIGH = AMIN1 (XHIGH,AMAX1(AMAX1(A,B), XLOW))
      YLOW  = AMAX1 (YLOW, AMIN1(AMIN1(C,D),YHIGH))
      YHIGH = AMIN1 (YHIGH,AMAX1(AMAX1(C,D), YLOW))
  31  IF (XLOW.EQ.XHIGH .OR. YLOW.EQ.YHIGH) GO TO 91
      XDIFF = 1023./(XMAX-XMIN)
      YDIFF = 1023./(YMAX-YMIN)
C
C  NOW THAT WE HAVE THE INITIALIZATION OUT OF THE WAY, WE CAN
C  PROCEED TO MARCH THROUGH THE 'PICT' ARRAY, GENERATING THE
C  SPECIFIED VECTORS OR POINTS.
C
      DO 90 I = IFIRST,ILAST,INCRE
C
C  GET END POINTS OF LINE
C
      XBEGIN = PICT(1,I)
      YBEGIN = PICT(2,I)
      K = I+L
      XFINAL = PICT(1,K)
      YFINAL = PICT(2,K)
C
C  TRIM LINE SO THAT IT WILL FIT ON THE SCREEN
C
      SWITCH = ABS(XFINAL-XBEGIN) .GT. ABS(YFINAL-YBEGIN)
      IF (SWITCH) GO TO 45
  40  IF (XBEGIN.GT.XFINAL) CALL SWAP
      IF (XBEGIN.GT.XHIGH.OR.XFINAL.LT.XLOW) GO TO 90
      IF (XBEGIN.LT.XLOW) CALL TRIM(XBEGIN,YBEGIN,XLOW, XFINAL,YFINAL)
      IF (XFINAL.GT.XHIGH)CALL TRIM(XFINAL,YFINAL,XHIGH,XBEGIN,YBEGIN)
      IF (SWITCH) GO TO 50
  45  IF (YBEGIN.GT.YFINAL) CALL SWAP
      IF (YBEGIN.GT.YHIGH.OR.YFINAL.LT.YLOW) GO TO 90
      IF (YBEGIN.LT.YLOW) CALL TRIM(YBEGIN,XBEGIN,YLOW, YFINAL,XFINAL)
      IF (YFINAL.GT.YHIGH)CALL TRIM(YFINAL,XFINAL,YHIGH,YBEGIN,XBEGIN)
      IF (SWITCH) GO TO 40
C
C  CONVERT END POINTS TO SCREEN COORDINATES
C
  50  IXX1 = (XBEGIN - XMIN)*XDIFF + 0.5
      IYY1 = (YBEGIN - YMIN)*YDIFF + 0.5
      IXX2 = (XFINAL - XMIN)*XDIFF + 0.5
      IYY2 = (YFINAL - YMIN)*YDIFF + 0.5
C
C  CHECK IF BEAM REPOSITIONING IS REQUIRED
C
      IF (IXX1 .NE. OLDX) GO TO 51
      IF (IYY1 .EQ. OLDY) GO TO 55
  51  IF (IXX2 .NE. OLDX) GO TO 52
      IF (IYY2 .NE. OLDY) GO TO 52
      IXX2 = IXX1
      IYY2 = IYY1
      IXX1 = OLDX
      IYY1 = OLDY
      GO TO 55
C
C  BEAM POSITIONING IS REQUIRED.
C  SET THE NEW X-COORDINATE (144000+X -> 51200+X),
C  AND THE NEW Y-COORDINATE (160000+Y -> 57344+Y).
C
  52  IVECT(J) = 51200 + IXX1
      J = J+1
      IVECT(J) = 57344 + IYY1
      J = J+1
C
C  REMEMBER WHERE THE END OF LINE NOW IS
C
  55  OLDX = IXX2
      OLDY = IYY2
C
C  GENERATE THE REQUIRED VECTOR.  INITIALLY WE WILL USE ONLY
C  ARBITRARY LONG VECTORS, WITH LIGHT PEN ENABLED:
C  IXWORD = 130000+DX -> 45056+DX
C  IYWORD = 100000+DY -> 32768+DY
C
      IXWORD = 45056
      IYWORD = 32768
C
C  BUILD THE X-WORD OF THE VECTOR, WITH APPROPRIATE SIGN
C  (4000 -> 2048) AND MAGNITUDE.
C
  60  IXINC = IXX2 - IXX1
      IF (IXINC) 61,70,70
  61  IXWORD = IXWORD + 2048
      IXINC  = -IXINC
C
C  AND BUILD THE Y-WORD, WITH APPROPRIATE SIGN AND MAGNITUDE.
C
  70  IYINC = IYY2 - IYY1
      IF (IYINC) 71,80,80
  71  IYWORD = IYWORD + 2048
      IYINC  = -IYINC
C
C  NOW CHECK TO SEE IF WE HAVE A LINE OR A POINT
C
  80  IF (IXINC + IYINC) 81,90,81
  81  IVECT(J) = IXWORD + IXINC
      J = J+1
      IVECT(J) = IYWORD + IYINC
      J = J+1
  90  CONTINUE
  91  JJ = J+1
      RETURN
      END
