! Euler path
!
DECLARE EXTERNAL SUB  Euler.Draw_Start, Euler.Draw_Name, Euler.Draw_Path
DECLARE EXTERNAL SUB  Euler.Build_Circuit, Euler.Build_Trail
DECLARE EXTERNAL FUNCTION Euler.Test_Connected, Euler.Ix_OddPoint, Euler.N_OddPoints
DECLARE EXTERNAL NUMERIC Euler.N_Max 
DIM m(25,25)
SET ECHO "OFF"
DO
   INPUT PROMPT "Number of vertices":N_Max 
LOOP UNTIL N_Max>=2 AND N_Max<=25 
MAT m=ZER(N_Max,N_Max)
!
SET WINDOW -1.2,1.2,-1.2,1.2
SET TEXT JUSTIFY "CENTER","HALF"
FOR i=1 TO N_MAX
   CALL Draw_Name(i)
NEXT i
! 
SET LINE COLOR 7 
DO
   INPUT Prompt "Connects two points ( enter 0,0 to finish)": a,b
   IF a>0 AND b>0 THEN
      IF a<>b THEN
         CALL Add(m,a,b)
         CALL Draw_Path(a,b)
      ELSE
         PAUSE "Specify different points"
      END IF
   ELSE
      LET N_Odd=N_OddPoints(m)
      IF N_Odd>2 THEN 
         PAUSE "There are" & STR$(N_Odd) &" odd points"
      ELSEIF Test_Connected(m,1)=1 THEN
         PAUSE "disconnected"
      ELSE 
         EXIT DO
      END IF
   END IF
LOOP
! 
SET LINE COLOR 1
LET a=Ix_OddPoint(m,1)
IF a=0 THEN           ! only evev points
   CALL Draw_Start(1)
   CALL Build_Circuit(m,1)
ELSE                  ! 
   CALL Draw_Start(a)
   LET b=Ix_OddPoint(m,a+1)
   CALL Build_Trail(m,a,b)
END IF 
END
! 
EXTERNAL SUB Add(m(,),a,b)
LET m(a,b)=1
LET m(b,a)=1
END SUB
! 
EXTERNAL SUB Del(m(,),a,b)
LET m(a,b)=0
LET m(b,a)=0
END SUB
! 
MODULE Euler          
PUBLIC NUMERIC N_Max
PUBLIC SUB  Draw_Start, Draw_Name, Draw_Path
PUBLIC SUB  Build_Circuit, Build_Trail
PUBLIC FUNCTION Test_Connected, Ix_OddPoint, N_OddPoints
SHARE FUNCTION degree, Ix_Any_Vertex
! 
EXTERNAL FUNCTION Test_Connected(m(,),a)
!
   DIM p(25,25),q(25,25)
   MAT p=ZER(N_Max,N_Max)
   MAT q=IDN(N_Max)
   FOR i=1 TO N_Max
      MAT q=q*m
      MAT p=p+q
   NEXT i
   LET t=0
   FOR i=2 TO N_Max
      IF p(1,i)=0 THEN 
         LET t=1
      END IF
   NEXT i
   LET  Test_Connected=t 
END FUNCTION
! 
EXTERNAL SUB Build_Trail(m(,),a,b)
! 
   IF m(a,b)=1 THEN
      CALL del(m,a,b)
      CALL Build_Circuit(m,a)
      CALL Draw_Arrow(a,b)
      CALL Build_Circuit(m,b)
   ELSE
      LET c=Ix_Any_Vertex(m,b)
      CALL del(m,b,c)
      CALL Build_Trail(m,a,c)
      CALL Draw_Arrow(c,b)
      CALL Build_Circuit(m,b)
   END IF
END SUB
! 
EXTERNAL SUB build_circuit(m(,),a)
! 
   LET b=Ix_Any_Vertex(m,a)
   IF b>0 THEN
      CALL Draw_Arrow(a,b)
      CALL Del(m,a,b)
      CALL Build_Trail(m,b,a)
   END IF
END SUB
! 
EXTERNAL FUNCTION Ix_Any_Vertex(M(,),a)
!
   LET t=0
   FOR i=1 TO N_Max
      IF m(a,i)=1 THEN 
         LET t=i
         EXIT FOR
      END IF
   NEXT i
   LET Ix_Any_Vertex=t
END FUNCTION
! 
EXTERNAL FUNCTION degree(m(,),n)
   LET t=0
   FOR i=1 TO N_Max
      IF m(n,i)=1 THEN LET t=t+1
   NEXT i
   LET degree=t
END FUNCTION 
! 
EXTERNAL FUNCTION N_OddPoints(m(,))
   LET t=0
   FOR i=1 TO N_Max
      IF MOD(degree(m,i),2)=1 THEN LET t=t+1
   NEXT i
   LET N_OddPoints=t
END FUNCTION
! 
EXTERNAL FUNCTION Ix_OddPoint(m(,),k)
   FOR i=k TO N_Max
      IF MOD(degree(m,i),2)=1 THEN
         LET Ix_OddPoint=i
         EXIT FUNCTION
      END IF
   NEXT i
   LET Ix_OddPoint=0
END FUNCTION
! 
EXTERNAL SUB Draw_Start(a)
   OPTION ANGLE DEGREES
   LET t=a*360/N_Max
   LET x2=COS(t)
   LET y2=SIN(t)
   SET AREA COLOR 4
   DRAW arrowhead WITH ROTATE(ANGLE(-x2,-y2))*SHIFT(x2,y2)
   SET AREA COLOR 1
END SUB
! 
EXTERNAL SUB Draw_Name(a)
   OPTION ANGLE DEGREES
   LET t=a*360/N_Max
   LET x2=1.12*COS(t)
   LET y2=1.12*SIN(t)
   PLOT TEXT ,AT x2,y2: STR$(a)
   SET AREA COLOR 1
END SUB
! 
EXTERNAL SUB Draw_Path(a1,a2)
   OPTION ANGLE DEGREES
   LET t1=a1*360/N_Max
   LET t2=a2*360/N_Max
   PLOT LINES: COS(t1),SIN(t1) ; COS(t2),SIN(t2)
END SUB
! 
EXTERNAL SUB Draw_Arrow(a1,a2)
   OPTION ANGLE DEGREES
   PRINT a1;a2
   LET t1=a1*360/N_Max
   LET t2=a2*360/N_Max
   LET x1=COS(t1)
   LET y1=SIN(t1)
   LET x2=COS(t2)
   LET y2=SIN(t2)
   PLOT LINES: x1,y1 ; x2,y2
   DRAW arrowhead WITH ROTATE(ANGLE(x2-x1,y2-y1))*SHIFT(x2,y2)
END SUB
! 
END MODULE 
! 
EXTERNAL PICTURE arrowhead
PLOT AREA : 0,0; -0.1,0.02; -0.1,-0.02
END PICTURE
