%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% Begin & End %%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Initializes a few variables, must be called!

vardef BeginAutomata =
  begingroup

% To avoid warnings caused by "large" numbers
    warningcheck := 0;

% Frame
    boolean isGlobalFrameToShow, isGlobalRestricted, isGlobalGrid, isGlobalFrame;
    pair globalLowerLeft, globalUpperRight;
    numeric globalGridDistance;

    isGlobalRestricted  := false; % size of picture restricted?
    globalLowerLeft     := (0,0); % restriction of picture, lower left corner
    globalUpperRight    := (0,0); % restriction of picture, upper right corner
    globalGridDistance  := 1cm;
    isGlobalGrid        := false;
    isGlobalFrame       := false;
    isGlobalFrameToShow := false;

% Rotation & Translation
    boolean globalRigidLabel, globalTraRot;
    numeric globalRotation;
    pair globalTranslation;

    globalRigidLabel  := true;
    globalTranslation := (0,0);
    globalRotation    := 0;
    globalTraRot      := false;

% Style Constants
    numeric solid, dash, dot, none, vlines, hlines, crosshatch;
    solid = 1; dash = 2; dot = 3; none = 4;
    vlines = 2; hlines = 3; crosshatch = 5;

% State Constants
    numeric larslarge, larsmedium, larssmall, larsverysmall, larsvariable;
    larslarge = 1; larsmedium = 2; larssmall = 3; larsverysmall = 4; larsvariable = 5;

% States Part I
    numeric globalStateSize, globalLetterSize;
    boolean globalHideState;
    numeric globalCurrentState;

    globalStateSize := 0.9cm;
    globalLetterSize := 0.6cm;
    globalHideState := false;
    globalCurrentState := larsmedium;

% States Part II
    color globalStateLineColor,  globalStateLineColorCurrent;
    color globalStateLabelColor, globalStateLabelColorCurrent;
    color globalStateFillColor,  globalStateFillColorCurrent;
    numeric globalStateLineStyle,  globalStateLineStyleCurrent;
    numeric globalStateLineWidth,  globalStateLineWidthCurrent;
    numeric globalStateLabelScale, globalStateLabelScaleCurrent;
    numeric globalStateFillStatus, globalStateFillStatusCurrent;

    globalStateLineColor         := black;
    globalStateLineColorCurrent  := black;
    globalStateLabelColor        := black;
    globalStateLabelColorCurrent := black;
    globalStateFillColor         := white;
    globalStateFillColorCurrent  := white;
    globalStateLineStyle         := solid;
    globalStateLineStyleCurrent  := solid;
    globalStateLineWidth         := 1.8pt;
    globalStateLineWidthCurrent  := 1.8pt;
    globalStateLabelScale        := 1.;    % is multiplied with globalLetterSize
    globalStateLabelScaleCurrent := 1.;
    globalStateFillStatus        := solid;
    globalStateFillStatusCurrent := solid;

% Doubled States
    boolean globalStateDoubled;
    numeric globalStateLineDblCoef, globalStateLineDblCoefCurrent;
    numeric globalStateLineDblSep,  globalStateLineDblSepCurrent;

    globalStateDoubled := false;
    globalStateLineDblCoef        := 0.6;
    globalStateLineDblCoefCurrent := 0.6;
    globalStateLineDblSep         := 0.4;
    globalStateLineDblSepCurrent  := 0.4;

% Dimmed States
    boolean globalDimState;
    color globalDimStateLineColor,  globalDimStateLineColorCurrent;
    color globalDimStateLabelColor, globalDimStateLabelColorCurrent;
    color globalDimStateFillColor,  globalDimStateFillColorCurrent;
    numeric globalDimStateLineStyle, globalDimStateLineStyleCurrent;
    numeric globalDimStateLineCoef,  globalDimStateLineCoefCurrent;

    globalDimState := false;
    globalDimStateLineStyle         := solid;
    globalDimStateLineStyleCurrent  := solid;
    globalDimStateLineColor         := (0.7,0.7,0.7);
    globalDimStateLineColorCurrent  := (0.7,0.7,0.7);
    globalDimStateLineCoef          := 1;
    globalDimStateLineCoefCurrent   := 1;
    globalDimStateLabelColor        := (0.7,0.7,0.7);
    globalDimStateLabelColorCurrent := (0.7,0.7,0.7);
    globalDimStateFillColor         := white;
    globalDimStateFillColorCurrent  := white;

% Initial / Final Arrows (coefficients which control the lengths of the arrows)
    numeric globalArrowOnLargeState, globalArrowOnLargeStateCurrent;
    numeric globalArrowOnMediumState, globalArrowOnMediumStateCurrent;
    numeric globalArrowOnSmallState, globalArrowOnSmallStateCurrent;
    numeric globalArrowOnVerySmallState, globalArrowOnVerySmallStateCurrent;

    globalArrowOnLargeState            := 1;
    globalArrowOnLargeStateCurrent     := 1;
    globalArrowOnMediumState           := 1;
    globalArrowOnMediumStateCurrent    := 1;
    globalArrowOnSmallState            := 1;
    globalArrowOnSmallStateCurrent     := 1;
    globalArrowOnVerySmallState        := 1;
    globalArrowOnVerySmallStateCurrent := 1;

% Loops (coefficients which control the size of the arrows)
    numeric globalLoopOnLargeState, globalLoopOnLargeStateCurrent;
    numeric globalLoopOnMediumState, globalLoopOnMediumStateCurrent;
    numeric globalLoopOnSmallState, globalLoopOnSmallStateCurrent;
    numeric globalLoopOnVerySmallState, globalLoopOnVerySmallStateCurrent;

    globalLoopOnLargeState             := 1;
    globalLoopOnLargeStateCurrent      := 1;
    globalLoopOnMediumState            := 1;
    globalLoopOnMediumStateCurrent     := 1;
    globalLoopOnSmallState             := 1;
    globalLoopOnSmallStateCurrent      := 1;
    globalLoopOnVerySmallState         := 1;
    globalLoopOnVerySmallStateCurrent  := 1;

% Loops
    numeric globalLoopAngle, globalLoopAngleCurrent;
    numeric globalCLoopAngle, globalCLoopAngleCurrent;

    globalLoopAngle           := 60;
    globalLoopAngleCurrent    := 60;
    globalCLoopAngle          := 44;
    globalCLoopAngleCurrent   := 44;

% Transitions
    boolean globalEdgeLineDblStatus, globalEdgeLineDblStatusCurrent;
    color globalEdgeLineColor, globalEdgeLineColorCurrent;
    color globalEdgeLabelColor, globalEdgeLabelColorCurrent;
    numeric globalEdgeLineStyle, globalEdgeLineStyleCurrent;
    numeric globalEdgeLineWidth, globalEdgeLineWidthCurrent;
    numeric globalEdgeLabelScale, globalEdgeLabelScaleCurrent;

    globalEdgeLineDblStatus        := false;
    globalEdgeLineDblStatusCurrent := false;
    globalEdgeLineColor            := black;
    globalEdgeLineColorCurrent     := black;
    globalEdgeLabelColor           := black;
    globalEdgeLabelColorCurrent    := black;
    globalEdgeLineStyle            := solid;
    globalEdgeLineStyleCurrent     := solid;
    globalEdgeLineWidth            := 1pt;
    globalEdgeLineWidthCurrent     := 1pt;
    globalEdgeLabelScale           := 1.;
    globalEdgeLabelScaleCurrent    := 1.;

% Angles of Arcs
    numeric globalArcAngle, globalArcAngleCurrent;
    numeric globalLArcAngle, globalLArcAngleCurrent;

    globalArcAngle         := 15;
    globalArcAngleCurrent  := 15;
    globalLArcAngle        := 30;
    globalLArcAngleCurrent := 30;

% Bordered Transitions
    boolean globalEdgeBordered;
    color globalEdgeLineBorderColor, globalEdgeLineBorderColorCurrent;
    numeric globalEdgeLineBorderCoef, globalEdgeLineBorderCoefCurrent;
    
    globalEdgeBordered               := false;
    globalEdgeLineBorderColor        := white;
    globalEdgeLineBorderColorCurrent := white;
    globalEdgeLineBorderCoef         := 2;
    globalEdgeLineBorderCoefCurrent  := 2;

% Doubled Transitions
    numeric globalEdgeLineDblCoef, globalEdgeLineDblCoefCurrent;
    numeric globalEdgeLineDblSep, globalEdgeLineDblSepCurrent;

    globalEdgeLineDblCoef        := 0.5;
    globalEdgeLineDblCoefCurrent := 0.5;
    globalEdgeLineDblSep         := 0.6;
    globalEdgeLineDblSepCurrent  := 0.6;

% Dimmed Transitions
    boolean globalDimEdge;
    color globalDimEdgeLineColor, globalDimEdgeLineColorCurrent;
    color globalDimEdgeLabelColor, globalDimEdgeLabelColorCurrent;
    numeric globalDimEdgeLineStyle, globalDimEdgeLineStyleCurrent;
    numeric globalDimEdgeLineCoef, globalDimEdgeLineCoefCurrent;

    globalDimEdge := false;
    globalDimEdgeLineColor         := (0.7,0.7,0.7);
    globalDimEdgeLineColorCurrent  := (0.7,0.7,0.7);
    globalDimEdgeLabelColor        := (0.7,0.7,0.7);
    globalDimEdgeLabelColorCurrent := (0.7,0.7,0.7);
    globalDimEdgeLineStyle         := solid;
    globalDimEdgeLineStyleCurrent  := solid;
    globalDimEdgeLineCoef          := 1.;
    globalDimEdgeLineCoefCurrent   := 1.;

% Zigzag Transitions & Forth / Back Offset & Reversed Transition
    numeric globalZZSize, globalZZSizeCurrent;
    numeric globalZZLineWidth, globalZZLineWidthCurrent;
    boolean globalForthBackOffset;
    boolean globalReverseArrow;

    globalZZSize             := 0.9cm;
    globalZZSizeCurrent      := 0.9cm;
    globalZZLineWidth        := 1;
    globalZZLineWidthCurrent := 1;
    globalForthBackOffset    := false;
    globalReverseArrow       := false;

% Label Offset & Color
    numeric globalLabelOffset;
    color globalLabelColor;

    globalLabelOffset := labeloffset;  % internal variable (0.10583 cm)
    globalLabelColor  := black;    

% Arrow Parameters
    % the current values are the Metapost parameters aharrow and ahlength
    numeric globalEdgeArrowAngle, globalEdgeArrowLength;

    globalEdgeArrowAngle := 45;
    globalEdgeArrowLength := 4;

  endgroup
enddef;


% Adds frame and grid, if isGlobalFrame and isGlobalGrid are set true; should be called!

vardef EndAutomata =
  begingroup
    save rechts, links, oben, unten, lrc, ulc, picframe, savepic;
    numeric rechts, links, oben, unten, j;
    pair lrc, ulc;
    path picframe;
    picture savepic;

    pickup pencircle scaled 1;

    if(isGlobalFrame):
      lrc := (xpart globalLowerLeft, ypart globalUpperRight);
      ulc := (xpart globalUpperRight, ypart globalLowerLeft);
      picframe := globalLowerLeft--lrc--globalUpperRight--ulc--cycle;
      if(isGlobalFrameToShow): draw picframe; fi
    fi

    if(isGlobalRestricted): clip currentpicture to picframe; fi
    if(isGlobalGrid):
      if(isGlobalFrame):
        links  := xpart globalLowerLeft;
        rechts := xpart globalUpperRight;
        oben   := ypart globalUpperRight;
        unten  := ypart globalLowerLeft;
      else:
        links  := xpart llcorner currentpicture;
        rechts := xpart urcorner currentpicture;
        oben   := ypart urcorner currentpicture;
        unten  := ypart llcorner currentpicture;
      fi
      savepic := currentpicture;
      currentpicture := nullpicture;
      for j=0 step globalGridDistance until rechts: draw (j, unten)--(j, oben) dashed evenly; endfor
      for j=0 step -globalGridDistance until links: draw (j, unten)--(j, oben) dashed evenly; endfor
      for j=0 step globalGridDistance until oben:   draw (links, j)--(rechts, j) dashed evenly; endfor
      for j=0 step -globalGridDistance until unten: draw (links, j)--(rechts, j) dashed evenly; endfor
      draw savepic;
    fi
  endgroup
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% Frame & Grid %%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% "SetFrame" defines a rectangle with lower left corner "llc" and upper right corner "urc"
% The rectangle is drawn, unless you use the command "HideFrame"
% If you want everything hided outside the frame, use the command "RestrictSize"

vardef SetFrame(expr llc, urc) =
  begingroup
    globalLowerLeft := llc;
    globalUpperRight := urc;
    isGlobalFrame := true;
  endgroup
enddef;

vardef RestrictSize = isGlobalRestricted := true; enddef;
vardef ShowFrame = isGlobalFrameToShow := true; enddef;
vardef HideFrame = isGlobalFrameToShow := false; enddef;


% "SetGridDistance" changes the distance between the lines of the grid (default = 1cm)
% Show the grid by calling ShowGrid; if a frame is set, the grid is restricted by the frame

vardef SetGridDistance(expr griddist) = globalGridDistance := griddist; enddef;
vardef ShowGrid = isGlobalGrid := true; enddef;
vardef HideGrid = isGlobalGrid := false; enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%% States %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Defines a state (circle) with name @# and draws it at the coordinate z = (x, y)
% with label Label written inside

vardef State@#(expr z, Label) =
  begingroup
    picture labelpic;
    labelpic := thelabel(Label, (0, -0.5cm));
    DrawState@#(z, labelpic, false, false, nullpicture, false, true);
  endgroup
enddef;


% Defines a state (rectangle) with name @# and draws it at the coordinate z = (x, y)
% with label Label written inside

vardef QState@#(expr z, Label) =
  begingroup
    picture labelpic;
    labelpic := thelabel(Label, (0, -0.5cm));
    DrawState@#(z, labelpic, false, false, nullpicture, false, false);
  endgroup
enddef;


% Defines a state (circle) with name @# and draws it at the coordinate z = (x, y)
% with label Label written inside

vardef StateVar@#(expr z, Label) =
  begingroup
    picture labelpic;
    labelpic := thelabel(Label, (0, -0.5cm));
    DrawState@#(z, labelpic, true, false, nullpicture, false, true);
  endgroup
enddef;


% Defines a state (rectangle) with name @# and draws it at the coordinate z = (x, y)
% with label Label written inside

vardef QStateVar@#(expr z, Label) =
  begingroup
    picture labelpic;
    labelpic := thelabel(Label, (0, -0.5cm));
    DrawState@#(z, labelpic, true, false, nullpicture, false, false);
  endgroup
enddef;


% Defines a very small state (circle) with name @# and draws it at the coordinate z = (x, y)

vardef VSState@#(expr z) = DrawState@#(z, nullpicture, false, true, nullpicture, false, true); enddef;


% Defines a very small state (rectangle) with name @# and draws it at the coordinate z = (x, y)

vardef VSQState@#(expr z) = DrawState@#(z, nullpicture, false, true, nullpicture, false, false); enddef;


% Defines a state (circle) with name @# and draws it at the coordinate z = (x, y)
% with picture Pic and with label Label written inside

vardef StateWithPic@#(expr z, Label, Pic) =
  begingroup
    picture labelpic;
    labelpic := thelabel(Label, (0, -0.5cm));
    DrawState@#(z, labelpic, false, false, Pic, true, true);
  endgroup
enddef;


% Defines a state (rectangle) with name @# and draws it at the coordinate z = (x, y)
% with picture Pic and with label Label written inside

vardef QStateWithPic@#(expr z, Label, Pic) =
  begingroup
    picture labelpic;
    labelpic := thelabel(Label, (0, -0.5cm));
    DrawState@#(z, labelpic, false, false, Pic, true, false);
  endgroup
enddef;


% Defines a state (circle) with name @# and draws it at the coordinate z = (x, y)
% with picture Pic and with label Label written inside

vardef StateVarWithPic@#(expr z, Label, Pic) =
  begingroup
    picture labelpic;
    labelpic := thelabel(Label, (0, -0.5cm));
    DrawState@#(z, labelpic, true, false, Pic, true, true);
  endgroup
enddef;


% Defines a state (rectangle) with name @# and draws it at the coordinate z = (x, y)
% with picture Pic and with label Label written inside

vardef QStateVarWithPic@#(expr z, Label, Pic) =
  begingroup
    picture labelpic;
    labelpic := thelabel(Label, (0, -0.5cm));
    DrawState@#(z, labelpic, true, false, Pic, true, false);
  endgroup
enddef;


% Defines a very small state (circle) with name @# and draws it at the coordinate z = (x, y)
% with picture Pic inside

vardef VSStateWithPic@#(expr z, Pic) = DrawState@#(z, nullpicture, false, true, Pic, true, true); enddef;


% Defines a very small state (rectangle) with name @# and draws it at the coordinate z = (x, y)
% with picture Pic inside

vardef VSQStateWithPic@#(expr z, Pic) = DrawState@#(z, nullpicture, false, true, Pic, true, false); enddef;


% Draws a state

vardef DrawState@#(expr z, Label, isVar, isVS, Pic, isPic, isCircle) =
  begingroup
    save links, rechts, oben, unten, ul, ur, ol, or, halbebreite, halbehoehe;
    save linksInner, untenInner, rechtsInner, obenInner, ulInner, urInner, olInner, orInner;
    save linksOuter, untenOuter, rechtsOuter, obenOuter, ulOuter, urOuter, olOuter, orOuter;
    save radius, radiusplus, bildhoehe, bildradius;
    pair links, unten, rechts, oben, ul, ur, ol, or, halbebreite, halbehoehe;
    pair linksInner, untenInner, rechtsInner, obenInner, ulInner, urInner, olInner, orInner;
    pair linksOuter, untenOuter, rechtsOuter, obenOuter, ulOuter, urOuter, olOuter, orOuter;
    numeric radius, radiusplus, bildhoehe, bildradius;

    numeric @#maxwidth, @#picwidth, @#stateSize, @#letterSize, @#thickness, @#stateType;
    boolean @#visible, @#doubled, @#variable, @#verysmall, @#ispic, @#initial[], @#final[];
    pair @#coord, @#n, @#ne, @#e, @#se, @#s, @#sw, @#w, @#nw;
    path @#frame, @#frameNotRotated, @#frameInner, @#frameOuter;
    picture @#lab, @#bgpic;
    color @#lineColor, @#labelColor, @#fillColor;
    numeric @#lineStyle, @#fillStatus;

    @#visible := not globalHideState;
    @#doubled := globalStateDoubled;
    @#variable := isVar;
    @#verysmall := isVS;
    @#ispic := isPic;
    @#bgpic := Pic;
    for i=0 upto 7: @#initial[i] := false; endfor
    for i=0 upto 7: @#final[i] := false; endfor
    @#lab := Label;
    @#coord := z;
    if(globalTraRot): @#coord := @#coord shifted globalTranslation; fi
    if(@#verysmall): @#stateSize := 0.3cm;
    else: @#stateSize := globalStateSize; fi
    @#stateType := globalCurrentState;
    @#letterSize := globalLetterSize * globalStateLabelScaleCurrent;
    @#thickness := globalStateLineWidthCurrent;

    if(globalDimState):
      @#lineStyle  := globalDimStateLineStyleCurrent;
      @#lineColor  := globalDimStateLineColorCurrent;
      @#labelColor := globalDimStateLabelColorCurrent;
      @#fillStatus := globalStateFillStatusCurrent;
      @#fillColor  := globalDimStateFillColorCurrent;
      @#thickness  := @#thickness * globalDimStateLineCoefCurrent;
    else:
      @#lineStyle  := globalStateLineStyleCurrent;
      @#lineColor  := globalStateLineColorCurrent;
      @#labelColor := globalStateLabelColorCurrent;
      @#fillStatus := globalStateFillStatusCurrent;
      @#fillColor  := globalStateFillColorCurrent;
    fi

    % Preparations for drawing the background
    if(@#ispic):
      halbebreite := (urcorner @#bgpic - ulcorner @#bgpic) / 2;
      halbehoehe  := (ulcorner @#bgpic - llcorner @#bgpic) / 2;
      @#bgpic := @#bgpic shifted (@#coord - llcorner @#bgpic - halbehoehe - halbebreite);
    fi

    % Preparations for drawing the image / label
    bildhoehe := ypart urcorner @#lab - ypart lrcorner @#lab;
    if(bildhoehe = 0): 
      @#lab := nullpicture;
      halbebreite := (0,0);
    else:
      bildradius := @#letterSize / 2;
      @#lab := @#lab scaled (@#letterSize / bildhoehe);
      halbebreite := (urcorner @#lab - ulcorner @#lab) / 2;
      @#lab := @#lab shifted (@#coord - llcorner @#lab - (0,bildradius) - halbebreite);
    fi
    @#picwidth := 2 * xpart halbebreite;
    @#maxwidth := @#picwidth + @#stateSize + 2 * @#thickness;

    % Preparations for drawing the frame
    if(isCircle):
      if(@#doubled):
        radius := @#stateSize / 2 + @#thickness * globalStateLineDblCoefCurrent / 2;
        radiusplus := @#thickness * (globalStateLineDblCoefCurrent + globalStateLineDblSepCurrent);
        radiusplusplus := 2 * @#thickness * globalStateLineDblCoefCurrent;    %%% little space to avoid merging of margin and transition
        if(@#variable):
          linksInner  := @#coord + (-radius, 0) - halbebreite;
          ulInner     := @#coord + (0, -radius) - halbebreite;
          urInner     := @#coord + (0, -radius) + halbebreite;
          rechtsInner := @#coord + (radius, 0) + halbebreite;
          olInner     := @#coord + (0, radius) - halbebreite;
          orInner     := @#coord + (0, radius) + halbebreite;
          linksOuter  := linksInner  + (-radiusplus, 0);
          ulOuter     := ulInner     + (0, -radiusplus);
          urOuter     := urInner     + (0, -radiusplus);
          rechtsOuter := rechtsInner + (radiusplus,  0);
          olOuter     := olInner     + (0,  radiusplus);
          orOuter     := orInner     + (0,  radiusplus);
          @#w := linksOuter  + (-radiusplusplus, 0);
          ul  := ulOuter     + (0, -radiusplusplus);
          ur  := urOuter     + (0, -radiusplusplus);
          @#e := rechtsOuter + (radiusplusplus,  0);
          ol  := olOuter     + (0,  radiusplusplus);
          or  := orOuter     + (0,  radiusplusplus);
          @#n := @#coord + (0,  radius + radiusplus + radiusplusplus);
          @#s := @#coord + (0, -radius - radiusplus - radiusplusplus);
          @#frameInner := linksInner..ulInner--urInner..rechtsInner..orInner--olInner..cycle;
          @#frameOuter := linksOuter..ulOuter--urOuter..rechtsOuter..orOuter--olOuter..cycle;
          @#frame := @#w..ul--ur..@#e..or--ol..cycle;
        else:
          linksInner  := @#coord + (-radius, 0);
          untenInner  := @#coord + (0, -radius);
          rechtsInner := @#coord + (radius,  0);
          obenInner   := @#coord + (0,  radius);
          linksOuter  := linksInner  + (-radiusplus, 0);
          untenOuter  := untenInner  + (0, -radiusplus);
          rechtsOuter := rechtsInner + (radiusplus,  0);
          obenOuter   := obenInner   + (0,  radiusplus);
          @#w := linksOuter  + (-radiusplusplus, 0);
          @#s := untenOuter  + (0, -radiusplusplus);
          @#e := rechtsOuter + (radiusplusplus,  0);
          @#n := obenOuter   + (0,  radiusplusplus);
          @#frameInner := linksInner..untenInner..rechtsInner..obenInner..cycle;
          @#frameOuter := linksOuter..untenOuter..rechtsOuter..obenOuter..cycle;
          @#frame := @#w..@#s..@#e..@#n..cycle;
        fi
      else:
        radius := @#stateSize / 2 + @#thickness / 2;
        radiusplus := @#thickness;    %%% little space (half the thickness) to avoid merging of margin and transition
        if(@#variable):
          linksOuter  := @#coord + (-radius, 0) - halbebreite;
          ulOuter     := @#coord + (0, -radius) - halbebreite;
          urOuter     := @#coord + (0, -radius) + halbebreite;
          rechtsOuter := @#coord + (radius, 0) + halbebreite;
          olOuter     := @#coord + (0, radius) - halbebreite;
          orOuter     := @#coord + (0, radius) + halbebreite;
          @#w := linksOuter  + (-radiusplus, 0);
          ul  := ulOuter     + (0, -radiusplus);
          ur  := urOuter     + (0, -radiusplus);
          @#e := rechtsOuter + (radiusplus, 0);
          ol  := olOuter     + (0, radiusplus);
          or  := orOuter     + (0, radiusplus);
          @#n := @#coord + (0,  radius + radiusplus);
          @#s := @#coord + (0, -radius - radiusplus);
          @#frameOuter := linksOuter..ulOuter--urOuter..rechtsOuter..orOuter--olOuter..cycle;
          @#frame := @#w..ul--ur..@#e..or--ol..cycle;
        else:
          linksOuter  := @#coord + (-radius,0);
          untenOuter  := @#coord + (0,-radius);
          rechtsOuter := @#coord + (radius,0);
          obenOuter   := @#coord + (0,radius);
          @#w := linksOuter  + (-radiusplus,0);
          @#s := untenOuter  + (0,-radiusplus);
          @#e := rechtsOuter + (radiusplus,0);
          @#n := obenOuter   + (0,radiusplus);
          @#frameOuter := linksOuter..untenOuter..rechtsOuter..obenOuter..cycle;
          @#frame := @#w..@#s..@#e..@#n..cycle;
        fi
      fi
    else:
      if(@#doubled):
        radius := @#stateSize / 2 + @#thickness * globalStateLineDblCoefCurrent / 2;
        radiusplus := @#thickness * (globalStateLineDblCoefCurrent + globalStateLineDblSepCurrent);
        radiusplusplus := 2 * @#thickness * globalStateLineDblCoefCurrent;
        if(@#variable):
          ulInner     := @#coord + (-radius, -radius) - halbebreite;
          urInner     := @#coord + ( radius, -radius) + halbebreite;
          olInner     := @#coord + (-radius, radius) - halbebreite;
          orInner     := @#coord + ( radius, radius) + halbebreite;
          ulOuter     := ulInner     + (-radiusplus, -radiusplus);
          urOuter     := urInner     + ( radiusplus, -radiusplus);
          olOuter     := olInner     + (-radiusplus,  radiusplus);
          orOuter     := orInner     + ( radiusplus,  radiusplus);
          @#w := @#coord + (-radius-radiusplus-radiusplusplus, 0);
          ul  := ulOuter + (-radiusplusplus, -radiusplusplus);
          ur  := urOuter + ( radiusplusplus, -radiusplusplus);
          @#e := @#coord + ( radius+radiusplus+radiusplusplus,  0);
          ol  := olOuter + (-radiusplusplus,  radiusplusplus);
          or  := orOuter + ( radiusplusplus,  radiusplusplus);
          @#n := @#coord + (0,  radius + radiusplus + radiusplusplus);
          @#s := @#coord + (0, -radius - radiusplus - radiusplusplus);
          @#frameInner := ulInner--urInner--orInner--olInner--cycle;
          @#frameOuter := ulOuter--urOuter--orOuter--olOuter--cycle;
          @#frame := ul--ur--or--ol--cycle;
        else:
          ulInner  := @#coord + (-radius, -radius);
          urInner  := @#coord + ( radius, -radius);
          olInner  := @#coord + (-radius,  radius);
          orInner  := @#coord + ( radius,  radius);
          ulOuter  := ulInner + (-radiusplus, -radiusplus);
          urOuter  := urInner + ( radiusplus, -radiusplus);
          olOuter  := olInner + (-radiusplus,  radiusplus);
          orOuter  := orInner + ( radiusplus,  radiusplus);
          ul  := ulOuter + (-radiusplusplus, -radiusplusplus);
          ur  := urOuter + ( radiusplusplus, -radiusplusplus);
          ol  := olOuter + (-radiusplusplus,  radiusplusplus);
          or  := orOuter + ( radiusplusplus,  radiusplusplus);
          @#w := @#coord + (-radius-radiusplus-radiusplusplus, 0);
          @#s := @#coord + (0, -radius-radiusplus-radiusplusplus);
          @#e := @#coord + ( radius+radiusplus+radiusplusplus, 0);
          @#n := @#coord + (0,  radius+radiusplus+radiusplusplus);
          @#frameInner := ulInner--urInner--orInner--olInner--cycle;
          @#frameOuter := ulOuter--urOuter--orOuter--olOuter--cycle;
          @#frame := ul--ur--or--ol--cycle;
        fi
      else:
        radius := @#stateSize / 2 + @#thickness / 2;
        radiusplus := @#thickness;
        if(@#variable):
          ulOuter     := @#coord + (-radius, -radius) - halbebreite;
          urOuter     := @#coord + ( radius, -radius) + halbebreite;
          olOuter     := @#coord + (-radius,  radius) - halbebreite;
          orOuter     := @#coord + ( radius,  radius) + halbebreite;
          @#w := @#coord - halbebreite + (-radius-radiusplus, 0);
          ul  := ulOuter     + (-radiusplus, -radiusplus);
          ur  := urOuter     + ( radiusplus, -radiusplus);
          @#e := @#coord + halbebreite + ( radius+radiusplus, 0);
          ol  := olOuter     + (-radiusplus, radiusplus);
          or  := orOuter     + ( radiusplus, radiusplus);
          @#n := @#coord + (0,  radius + radiusplus);
          @#s := @#coord + (0, -radius - radiusplus);
          @#frameOuter := ulOuter--urOuter--orOuter--olOuter--cycle;
          @#frame := ul--ur--or--ol--cycle;
        else:
          ulOuter := @#coord + (-radius,-radius);
          urOuter := @#coord + ( radius,-radius);
          olOuter := @#coord + (-radius, radius);
          orOuter := @#coord + ( radius, radius);
          ul := ulOuter + (-radiusplus,-radiusplus);
          ur := urOuter + ( radiusplus,-radiusplus);
          ol := olOuter + (-radiusplus, radiusplus);
          or := orOuter + ( radiusplus, radiusplus);
          @#w := @#coord + (-radius-radiusplus, 0);
          @#s := @#coord + (0, -radius-radiusplus);
          @#e := @#coord + ( radius+radiusplus, 0);
          @#n := @#coord + (0,  radius+radiusplus);
          @#frameOuter := ulOuter--urOuter--orOuter--olOuter--cycle;
          @#frame := ul--ur--or--ol--cycle;
        fi
      fi
    fi
    eckpunkte@#;

    @#frameNotRotated := @#frame;
    if(globalTraRot): @#frame := @#frame rotatedaround(@#coord, globalRotation); fi

    if(@#visible):
      if(@#ispic):
        clip @#bgpic to @#frameOuter; 
        if(globalTraRot): @#bgpic := @#bgpic rotatedaround(@#coord, globalRotation); fi
        draw @#bgpic;
      else:
        if(@#fillStatus = solid): addto @#bgpic contour @#frameOuter;
        elseif(@#fillStatus = vlines):
          for i = xpart @#w step 5 until xpart @#e:
            addto @#bgpic doublepath (i, ypart @#s)--(i, ypart @#n) withpen pencircle scaled 1; endfor
        elseif(@#fillStatus = hlines):
          for i = ypart @#s step 5 until ypart @#n:
            addto @#bgpic doublepath (xpart @#w, i)--(xpart @#e, i) withpen pencircle scaled 1; endfor
        elseif(@#fillStatus = crosshatch):
          for i = 0  step 7 until 2 * (xpart @#e - xpart @#w):
            addto @#bgpic doublepath (xpart @#w + i, ypart @#s)--(xpart @#w, ypart @#s + i) withpen pencircle scaled 1; endfor
        fi
        clip @#bgpic to @#frameOuter; 
        if(globalTraRot): @#bgpic := @#bgpic rotatedaround(@#coord, globalRotation); fi
        draw @#bgpic withcolor @#fillColor;
      fi

      if(globalTraRot): @#frameOuter := @#frameOuter rotatedaround(@#coord, globalRotation); fi

      if(@#doubled):
        if(globalTraRot): @#frameInner := @#frameInner rotatedaround(@#coord, globalRotation); fi
        pickup pencircle scaled (@#thickness * globalStateLineDblCoefCurrent);
        if(@#lineStyle = 1): draw @#frameInner withcolor @#lineColor;
        elseif(@#lineStyle = 2): draw @#frameInner withcolor @#lineColor dashed evenly;
        elseif(@#lineStyle = 3): draw @#frameInner withcolor @#lineColor dashed withdots;
        elseif(@#lineStyle = 4): ;    fi
      else:
        pickup pencircle scaled @#thickness;
      fi

      if(@#lineStyle    = solid): draw @#frameOuter withcolor @#lineColor;
      elseif(@#lineStyle = dash): draw @#frameOuter withcolor @#lineColor dashed evenly;
      elseif(@#lineStyle = dot ): draw @#frameOuter withcolor @#lineColor dashed withdots;
      elseif(@#lineStyle = none): ;    fi

      if(not @#verysmall):
        if(globalTraRot):
          if(@#variable): draw @#lab rotatedaround(@#coord, globalRotation) withcolor @#labelColor;
          elseif(not globalRigidLabel): draw @#lab rotatedaround(@#coord, globalRotation) withcolor @#labelColor;
          else: draw @#lab;
          fi
        else: draw @#lab withcolor @#labelColor;
        fi
      fi
    fi
  endgroup
enddef;


% Called by DrawState to calculate the corners northeast, northwest, southwest and southeast

vardef eckpunkte@# =
  begingroup
    save pnw, pne, pse, psw;
    path pnw, pne, pse, psw;

    pnw := @#coord--(@#coord + (-@#maxwidth,  @#maxwidth));
    pne := @#coord--(@#coord + ( @#maxwidth,  @#maxwidth));
    pse := @#coord--(@#coord + ( @#maxwidth, -@#maxwidth));
    psw := @#coord--(@#coord + (-@#maxwidth, -@#maxwidth));
    @#nw := pnw intersectionpoint @#frame;
    @#ne := pne intersectionpoint @#frame;
    @#se := pse intersectionpoint @#frame;
    @#sw := psw intersectionpoint @#frame;
  endgroup
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%% Initial & Final %%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Defines a final state with name @# and draws it at the coordinate z = (x, y)
% with label Label written inside

vardef FinalState@#(expr z, Label) =
  begingroup
    save gsd;
    boolean gsd;
    gsd := globalStateDoubled;
    StateLineDouble;
    State@#(z, Label);
    globalStateDoubled := gsd;
  endgroup
enddef;


% Defines a final state with name @# and draws it at the coordinate z = (x, y)
% with label Label written inside

vardef FinalQState@#(expr z, Label) =
  begingroup
    save gsd;
    boolean gsd;
    gsd := globalStateDoubled;
    StateLineDouble;
    QState@#(z, Label);
    globalStateDoubled := gsd;
  endgroup
enddef;


% Defines a final state with name @# and draws it at the coordinate z = (x, y)
% with picture Pic and with label Label written inside

vardef FinalStateWithPic@#(expr z, Label, Pic) =
  begingroup
    save gsd;
    boolean gsd;
    gsd := globalStateDoubled;
    StateLineDouble;
    StateWithPic@#(z, Label, Pic);
    globalStateDoubled := gsd;
  endgroup
enddef;


% Defines a final state with name @# and draws it at the coordinate z = (x, y)
% with picture Pic and with label Label written inside

vardef FinalQStateWithPic@#(expr z, Label, Pic) =
  begingroup
    save gsd;
    boolean gsd;
    gsd := globalStateDoubled;
    StateLineDouble;
    QStateWithPic@#(z, Label, Pic);
    globalStateDoubled := gsd;
  endgroup
enddef;


% Adds an incoming arrow to the named state marking it as an initial state
% Second and third function for setting a label on the left or right

vardef Initial@#(expr direction) = DrawIF@#(direction, 0, "", false, false, false); enddef;
vardef InitialL@#(expr direction, Position, Label) = DrawIF@#(direction, Position, Label, true, true, false); enddef;
vardef InitialR@#(expr direction, Position, Label) = DrawIF@#(direction, Position, Label, true, false, false); enddef;


% Adds an outgoing arrow to the named state marking it as a final state
% Second and third function for setting a label on the left or right

vardef Final@#(expr direction) = DrawIF@#(direction, 0, "", false, false, true); enddef;
vardef FinalL@#(expr direction, Position, Label) = DrawIF@#(direction, Position, Label, true, true, true); enddef;
vardef FinalR@#(expr direction, Position, Label) = DrawIF@#(direction, Position, Label, true, false, true); enddef;


% Draws with incoming or outgoing arrow to a state

vardef DrawIF@#(expr direction, Position, Label, withLabel, isLeft, isOut) =
  begingroup
    save istLinks, doubled, pnta, pntb, tfarbe;
    save dist, distAxes, faktor, labstand, tstyle, tbreite, pos;
    numeric dist, distAxes, faktor, labstand, tstyle, tbreite, pos;
    boolean istLinks, doubled;
    pair pnta, pntb;
    color tfarbe;
    path line;   % "line" must be known after execution for adding further labels

    if(@#stateType = larslarge):         faktor := globalArrowOnLargeStateCurrent;
    elseif(@#stateType = larsmedium):    faktor := globalArrowOnMediumStateCurrent;
    elseif(@#stateType = larssmall):     faktor := globalArrowOnSmallStateCurrent;
    elseif(@#stateType = larsverysmall): faktor := globalArrowOnVerySmallStateCurrent;
    else: faktor := 1;
    fi

    pos := Position;
    dist := @#stateSize * faktor;
    distAxes := dist / 1.414;

    if(globalDimEdge):
      tfarbe           := globalDimEdgeLineColorCurrent;
      globalLabelColor := globalDimEdgeLabelColorCurrent;
      tstyle           := globalDimEdgeLineStyleCurrent;
      tbreite          := globalEdgeLineWidthCurrent * globalDimEdgeLineCoefCurrent;
    else:
      tfarbe           := globalEdgeLineColorCurrent;
      globalLabelColor := globalEdgeLabelColorCurrent;
      tstyle           := globalEdgeLineStyleCurrent;
      tbreite          := globalEdgeLineWidthCurrent;
    fi
    doubled := globalEdgeLineDblStatusCurrent;

    if(direction = "w"):      pnta := @#w;  pntb := @#w  + (-dist, 0);
    elseif(direction = "nw"): pnta := @#nw; pntb := @#nw + (-distAxes,  distAxes);
    elseif(direction = "n"):  pnta := @#n;  pntb := @#n  + (0,  dist);
    elseif(direction = "ne"): pnta := @#ne; pntb := @#ne + ( distAxes,  distAxes);
    elseif(direction = "e"):  pnta := @#e;  pntb := @#e  + ( dist, 0);
    elseif(direction = "se"): pnta := @#se; pntb := @#se + ( distAxes, -distAxes);
    elseif(direction = "s"):  pnta := @#s;  pntb := @#s  + (0, -dist);
    elseif(direction = "sw"): pnta := @#sw; pntb := @#sw + (-distAxes, -distAxes); 
    fi

    if(isOut): line := pnta--pntb;
    else:      line := pntb--pnta; fi
    if(globalTraRot): line := line rotatedaround(@#coord, globalRotation); fi

    if(globalReverseArrow): line := reverse line; istLinks := not isLeft; pos := 1 - pos;
    else: istLinks := isLeft; fi

    if(globalEdgeBordered):
      if(doubled):
        strichbreite := (2 * globalEdgeLineBorderCoefCurrent + 2 * globalEdgeLineDblCoefCurrent + globalEdgeLineDblSepCurrent) * tbreite;
        pickup pencircle scaled strichbreite;
        linecap := butt;
        draw line withcolor globalEdgeLineBorderColorCurrent;
        linecap := rounded;
        strichbreite := (2 * globalEdgeLineDblCoefCurrent + globalEdgeLineDblSepCurrent) * tbreite;
        pickup pencircle scaled strichbreite;
        if(tstyle = solid):    drawarrow line withcolor tfarbe;
        elseif(tstyle = dash): drawarrow line dashed evenly withcolor tfarbe;
        elseif(tstyle = dot):  drawarrow line dashed withdots withcolor tfarbe;
        elseif(tstyle = none): ; fi
        strichbreite := globalEdgeLineDblSepCurrent * tbreite;
        pickup pencircle scaled strichbreite; 
        draw line withcolor globalEdgeLineBorderColorCurrent;
      else:
        strichbreite := 2 * globalEdgeLineBorderCoefCurrent * tbreite + tbreite;
        pickup pencircle scaled strichbreite;
        linecap := butt;
        draw line withcolor globalEdgeLineBorderColorCurrent;
        linecap := rounded;
        pickup pencircle scaled tbreite;
        if(tstyle = solid):    drawarrow line withcolor tfarbe;
        elseif(tstyle = dash): drawarrow line dashed evenly withcolor tfarbe;
        elseif(tstyle = dot):  drawarrow line dashed withdots withcolor tfarbe;
        elseif(tstyle = none): ; fi
      fi
    else:
      if(doubled):
        strichbreite := (2 * globalEdgeLineDblCoefCurrent + globalEdgeLineDblSepCurrent) * tbreite;
        pickup pencircle scaled strichbreite;
        if(tstyle = solid):    drawarrow line withcolor tfarbe;
        elseif(tstyle = dash): drawarrow line dashed evenly withcolor tfarbe;
        elseif(tstyle = dot):  drawarrow line dashed withdots withcolor tfarbe;
        elseif(tstyle = none): ; fi
        strichbreite := globalEdgeLineDblSepCurrent * tbreite;
        pickup pencircle scaled strichbreite; 
        draw line withcolor white;
      else:
        pickup pencircle scaled tbreite;
        if(tstyle = solid):    drawarrow line withcolor tfarbe;
        elseif(tstyle = dash): drawarrow line dashed evenly withcolor tfarbe;
        elseif(tstyle = dot):  drawarrow line dashed withdots withcolor tfarbe;
        elseif(tstyle = none): ; fi
      fi
    fi

    labstand := 0.1cm;
    if(doubled):            labstand := labstand + (globalEdgeLineDblCoefCurrent + 0.5 * globalEdgeLineDblSepCurrent) * tbreite;
    else:                   labstand := labstand + 0.5 * tbreite;  fi
    if(globalEdgeBordered): labstand := labstand + globalEdgeLineBorderCoefCurrent * tbreite; fi
    globalLabelOffset := labstand;

    AddLabel(pos, Label, istLinks);
  endgroup
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% Loops %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Draws loop with (default) opening 60°. Draws Label at Position (0 < Position < 1) of loop.

vardef LoopE(suffix a)(expr Label, Position)  = DrawLoop(a,   0, Label, Position, globalLoopAngleCurrent, true); enddef;
vardef LoopNE(suffix a)(expr Label, Position) = DrawLoop(a,  45, Label, Position, globalLoopAngleCurrent, true); enddef;
vardef LoopN(suffix a)(expr Label, Position)  = DrawLoop(a,  90, Label, Position, globalLoopAngleCurrent, true); enddef;
vardef LoopNW(suffix a)(expr Label, Position) = DrawLoop(a, 135, Label, Position, globalLoopAngleCurrent, true); enddef;
vardef LoopW(suffix a)(expr Label, Position)  = DrawLoop(a, 180, Label, Position, globalLoopAngleCurrent, true); enddef;
vardef LoopSW(suffix a)(expr Label, Position) = DrawLoop(a, 225, Label, Position, globalLoopAngleCurrent, true); enddef;
vardef LoopS(suffix a)(expr Label, Position)  = DrawLoop(a, 270, Label, Position, globalLoopAngleCurrent, true); enddef;
vardef LoopSE(suffix a)(expr Label, Position) = DrawLoop(a, 315, Label, Position, globalLoopAngleCurrent, true); enddef;


% Draws loop with (default) opening 44°. Draws Label at Position (0 < Position < 1) of loop.

vardef CLoopE(suffix a)(expr Label, Position)  = DrawLoop(a,   0, Label, Position, globalCLoopAngleCurrent, true); enddef;
vardef CLoopNE(suffix a)(expr Label, Position) = DrawLoop(a,  45, Label, Position, globalCLoopAngleCurrent, true); enddef;
vardef CLoopN(suffix a)(expr Label, Position)  = DrawLoop(a,  90, Label, Position, globalCLoopAngleCurrent, true); enddef;
vardef CLoopNW(suffix a)(expr Label, Position) = DrawLoop(a, 135, Label, Position, globalCLoopAngleCurrent, true); enddef;
vardef CLoopW(suffix a)(expr Label, Position)  = DrawLoop(a, 180, Label, Position, globalCLoopAngleCurrent, true); enddef;
vardef CLoopSW(suffix a)(expr Label, Position) = DrawLoop(a, 225, Label, Position, globalCLoopAngleCurrent, true); enddef;
vardef CLoopS(suffix a)(expr Label, Position)  = DrawLoop(a, 270, Label, Position, globalCLoopAngleCurrent, true); enddef;
vardef CLoopSE(suffix a)(expr Label, Position) = DrawLoop(a, 315, Label, Position, globalCLoopAngleCurrent, true); enddef;


% Draws loop with (default) opening 44° or 60° in direction (0 <= direction < 360).
% Draws Label at Position. Rotation left (clockwise) or right.

vardef LoopL(suffix a)(expr direction, Label, Position) = DrawLoop(a, direction, Label, Position, globalLoopAngleCurrent, true); enddef;
vardef LoopR(suffix a)(expr direction, Label, Position) = DrawLoop(a, direction, Label, Position, globalLoopAngleCurrent, false); enddef;
vardef CLoopL(suffix a)(expr direction, Label, Position) = DrawLoop(a, direction, Label, Position, globalCLoopAngleCurrent, true); enddef;
vardef CLoopR(suffix a)(expr direction, Label, Position) = DrawLoop(a, direction, Label, Position, globalCLoopAngleCurrent, false); enddef;


% Draws loop

vardef DrawLoop(suffix a)(expr direction, Label, Position, opening, isLeft) =
  begingroup
    save xwert, ywert, angleout, anglein, aout, ain, winkel, labstand, faktor, tstyle, tbreite, laenge;
    save istLinks, doubled, schnitt, bezugspunkt, umkehrpunkt, tfarbe, pdir, bsvektor, pos, zwwert;
    numeric xwert, ywert, angleout, anglein, aout, ain, winkel, labstand, faktor;
    numeric tstyle, tbreite, laenge, pos;
    boolean istLinks, doubled;
    pair schnitt, bezugspunkt, umkehrpunkt, bsvektor, zwwert;
    color tfarbe;
    path line, pdir;  % "line" must be known after execution for adding further labels

    pos := Position;
    xwert := a.maxwidth * cosd(direction);
    ywert := a.maxwidth * sind(direction);

    if(globalDimEdge):
      tfarbe           := globalDimEdgeLineColorCurrent;
      globalLabelColor := globalDimEdgeLabelColorCurrent;
      tstyle           := globalDimEdgeLineStyleCurrent;
      tbreite          := globalEdgeLineWidthCurrent * globalDimEdgeLineCoefCurrent;
    else:
      tfarbe           := globalEdgeLineColorCurrent;
      globalLabelColor := globalEdgeLabelColorCurrent;
      tstyle           := globalEdgeLineStyleCurrent;
      tbreite          := globalEdgeLineWidthCurrent;
    fi
    doubled := globalEdgeLineDblStatusCurrent;

    pdir := a.coord--(a.coord + (xwert,ywert));
    if(globalTraRot): schnitt := pdir intersectionpoint a.frameNotRotated;
    else: schnitt := pdir intersectionpoint a.frame; fi
    winkel := direction;

    if(a.variable):
      bezugspunkt := (xpart schnitt, ypart a.coord);
      randr := xpart a.coord + a.picwidth / 2;
      randl := xpart a.coord - a.picwidth / 2;
      if(randr < xpart schnitt): 
        bezugspunkt := (randr, ypart a.coord);
        zwwert := schnitt - bezugspunkt;
        if(xpart zwwert = 0): winkel := 90 * ypart zwwert / abs(ypart zwwert);
        else: winkel := angle(zwwert); fi
      elseif(randl > xpart schnitt):
        bezugspunkt := (randl, ypart a.coord);
        zwwert := schnitt - bezugspunkt;
        if(xpart zwwert = 0): winkel := 90 * ypart zwwert / abs(ypart zwwert);
        else: winkel := angle(zwwert); fi
      else:
        if(ypart schnitt - ypart a.coord > 0): winkel := 90;
        else: winkel := 270; fi
      fi
    else:
      bezugspunkt := a.coord;
    fi

    if(a.stateType = larslarge):         faktor := globalLoopOnLargeStateCurrent;
    elseif(a.stateType = larsmedium):    faktor := globalLoopOnMediumStateCurrent;
    elseif(a.stateType = larssmall):     faktor := globalLoopOnSmallStateCurrent;
    elseif(a.stateType = larsverysmall): faktor := globalLoopOnVerySmallStateCurrent;
    else:                                faktor := 1;
    fi


    bsvektor := schnitt - bezugspunkt;
    laenge := xpart bsvektor ++ ypart bsvektor;

    umkehrpunkt := schnitt + a.stateSize * (schnitt - bezugspunkt) * faktor / laenge;

    if(isLeft): 
      angleout := winkel + opening / 2;
      anglein := 180 + winkel - opening / 2;
      aout := winkel - 90;
      ain := winkel - 90;
    else:
      angleout := winkel - opening / 2;
      anglein := 180 + winkel + opening / 2;
      aout := winkel + 90;
      ain := winkel + 90;
    fi 

    line := bezugspunkt{dir angleout}..{dir ain}umkehrpunkt{dir aout}..{dir anglein}bezugspunkt;

    if(globalReverseArrow): line := reverse line; istLinks := not isLeft; pos := 1 - pos;
    else: istLinks := isLeft; fi

    if(globalTraRot): 
      line := line cutbefore a.frameNotRotated cutafter a.frameNotRotated;
      line := line rotatedaround(a.coord, globalRotation);
    else:
      line := line cutbefore a.frame cutafter a.frame;
    fi

    if(globalEdgeBordered):
      if(doubled):
        strichbreite := (2 * globalEdgeLineBorderCoefCurrent + 2 * globalEdgeLineDblCoefCurrent + globalEdgeLineDblSepCurrent) * tbreite;
        pickup pencircle scaled strichbreite;
        linecap := butt;
        draw line withcolor globalEdgeLineBorderColorCurrent;
        linecap := rounded;
        strichbreite := (2 * globalEdgeLineDblCoefCurrent + globalEdgeLineDblSepCurrent) * tbreite;
        pickup pencircle scaled strichbreite;
        if(tstyle = solid):    drawarrow line withcolor tfarbe;
        elseif(tstyle = dash): drawarrow line dashed evenly withcolor tfarbe;
        elseif(tstyle = dot):  drawarrow line dashed withdots withcolor tfarbe;
        elseif(tstyle = none): ; fi
        strichbreite := globalEdgeLineDblSepCurrent * tbreite;
        pickup pencircle scaled strichbreite; 
        draw line withcolor globalEdgeLineBorderColorCurrent;
      else:
        strichbreite := 2 * globalEdgeLineBorderCoefCurrent * tbreite + tbreite;
        pickup pencircle scaled strichbreite;
        linecap := butt;
        draw line withcolor globalEdgeLineBorderColorCurrent;
        linecap := rounded;
        pickup pencircle scaled tbreite;
        if(tstyle = solid):    drawarrow line withcolor tfarbe;
        elseif(tstyle = dash): drawarrow line dashed evenly withcolor tfarbe;
        elseif(tstyle = dot):  drawarrow line dashed withdots withcolor tfarbe;
        elseif(tstyle = none): ; fi
      fi
    else:
      if(doubled):
        strichbreite := (2 * globalEdgeLineDblCoefCurrent + globalEdgeLineDblSepCurrent) * tbreite;
        pickup pencircle scaled strichbreite;
        if(tstyle = solid):    drawarrow line withcolor tfarbe;
        elseif(tstyle = dash): drawarrow line dashed evenly withcolor tfarbe;
        elseif(tstyle = dot):  drawarrow line dashed withdots withcolor tfarbe;
        elseif(tstyle = none): ; fi
        strichbreite := globalEdgeLineDblSepCurrent * tbreite;
        pickup pencircle scaled strichbreite; 
        draw line withcolor white;
      else:
        pickup pencircle scaled tbreite;
        if(tstyle = solid):    drawarrow line withcolor tfarbe;
        elseif(tstyle = dash): drawarrow line dashed evenly withcolor tfarbe;
        elseif(tstyle = dot):  drawarrow line dashed withdots withcolor tfarbe;
        elseif(tstyle = none): ; fi
      fi
    fi

    labstand := 0.1cm;
    if(doubled):            labstand := labstand + (globalEdgeLineDblCoefCurrent + 0.5 * globalEdgeLineDblSepCurrent) * tbreite;
    else:                   labstand := labstand + 0.5 * tbreite;  fi
    if(globalEdgeBordered): labstand := labstand + globalEdgeLineBorderCoefCurrent * tbreite; fi
    globalLabelOffset := labstand;

    AddLabel(2 * pos, Label, istLinks);
  endgroup
enddef;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% Edges, Arcs and Curves %%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Draws a straight arrow from state a to state b and sets a label (left or right of the arrow)
% at Position (0 < Position < 1)

vardef EdgeL(suffix a, b)(expr Label, Position) =
  begingroup
    save winkelA, winkelB, zwwert;
    numeric winkelA, winkelB;
    pair zwwert;
    zwwert := b.coord - a.coord;
    if(xpart(zwwert) = 0): winkelA := 90 * ypart zwwert / abs(ypart zwwert);
    else: winkelA := angle((b.coord - a.coord)); fi
    winkelB := winkelA + 180;
    Curve(a, b, winkelA, winkelB, 1, Label, Position, true, true, false);
  endgroup
enddef;


vardef EdgeR(suffix a, b)(expr Label, Position) =
  begingroup
    save winkelA, winkelB, zwwert;
    numeric winkelA, winkelB;
    pair zwwert;
    zwwert := b.coord - a.coord;
    if(xpart(zwwert) = 0): winkelA := 90 * ypart zwwert / abs(ypart zwwert);
    else: winkelA := angle((b.coord - a.coord)); fi
    winkelB := winkelA + 180;
    Curve(a, b, winkelA, winkelB, 1, Label, Position, false, true, false);
  endgroup
enddef;


% Draws an arc (arrow) from state a to state b and sets a label (left or right of the arrow)
% at Position (0 < Position < 1); direction differs from straight line in 15°, 30° or variable angle

vardef ArcL(suffix a, b)(expr Label, Position) =
  begingroup
    save winkelA, winkelB, zwwert;
    numeric winkelA, winkelB;
    pair zwwert;
    zwwert := b.coord - a.coord;
    if(xpart(zwwert) = 0): winkelA := 90 * ypart zwwert / abs(ypart zwwert);
    else: winkelA := angle((b.coord - a.coord)); fi
    winkelB := winkelA + 180;
    Curve(a, b, winkelA+globalArcAngleCurrent, winkelB-globalArcAngleCurrent, 1, Label, Position, true, false, false);
  endgroup
enddef;


vardef ArcR(suffix a, b)(expr Label, Position) =
  begingroup
    save winkelA, winkelB, zwwert;
    numeric winkelA, winkelB;
    pair zwwert;
    zwwert := b.coord - a.coord;
    if(xpart(zwwert) = 0): winkelA := 90 * ypart zwwert / abs(ypart zwwert);
    else: winkelA := angle((b.coord - a.coord)); fi
    winkelB := winkelA + 180;
    Curve(a, b, winkelA-globalArcAngleCurrent, winkelB+globalArcAngleCurrent, 1, Label, Position, false, false, false);
  endgroup
enddef;


vardef LArcL(suffix a, b)(expr Label, Position) =
  begingroup
    save winkelA, winkelB, zwwert;
    numeric winkelA, winkelB;
    pair zwwert;
    zwwert := b.coord - a.coord;
    if(xpart(zwwert) = 0): winkelA := 90 * ypart zwwert / abs(ypart zwwert);
    else: winkelA := angle((b.coord - a.coord)); fi
    winkelB := winkelA + 180;
    Curve(a, b, winkelA+globalLArcAngle, winkelB-globalLArcAngle, 1, Label, Position, true, false, false);
  endgroup
enddef;


vardef LArcR(suffix a, b)(expr Label, Position) =
  begingroup
    save winkelA, winkelB, zwwert;
    numeric winkelA, winkelB;
    pair zwwert;
    zwwert := b.coord - a.coord;
    if(xpart(zwwert) = 0): winkelA := 90 * ypart zwwert / abs(ypart zwwert);
    else: winkelA := angle((b.coord - a.coord)); fi
    winkelB := winkelA + 180;
    Curve(a, b, winkelA-globalLArcAngle, winkelB+globalLArcAngle, 1, Label, Position, false, false, false);
  endgroup
enddef;


vardef VArcL(suffix a, b)(expr arcangle, ncurv, Label, Position) =
  begingroup
    save winkelA, winkelB, zwwert;
    numeric winkelA, winkelB;
    pair zwwert;
    zwwert := b.coord - a.coord;
    if(xpart(zwwert) = 0): winkelA := 90 * ypart zwwert / abs(ypart zwwert);
    else: winkelA := angle((b.coord - a.coord)); fi
    winkelB := winkelA + 180;
    Curve(a, b, winkelA+arcangle, winkelB-arcangle, ncurv, Label, Position, true, false, false);
  endgroup
enddef;


vardef VArcR(suffix a, b)(expr arcangle, ncurv, Label, Position) =
  begingroup
    save winkelA, winkelB, zwwert;
    numeric winkelA, winkelB;
    pair zwwert;
    zwwert := b.coord - a.coord;
    if(xpart(zwwert) = 0): winkelA := 90 * ypart zwwert / abs(ypart zwwert);
    else: winkelA := angle((b.coord - a.coord)); fi
    winkelB := winkelA + 180;
    Curve(a, b, winkelA-arcangle, winkelB+arcangle, ncurv, Label, Position, false, false, false);
  endgroup
enddef;


% Draws curve (transition) with label on the left / right; choose outgoing and incoming angle
% and eccentricity "ncurv" (default = 1). 

vardef VCurveL(suffix a, b)(expr angleA, angleB, ncurv, Label, Position) =
  Curve(a, b, angleA, angleB, ncurv, Label, Position, true, false, false);  enddef;

vardef VCurveR(suffix a, b)(expr angleA, angleB, ncurv, Label, Position) =
  Curve(a, b, angleA, angleB, ncurv, Label, Position, false, false, false);  enddef;


% Draws zigzag transition with label on the left or right

vardef ZZEdgeL(suffix a, b)(expr Label, Position) =
  begingroup
    save winkelA, winkelB, zwwert;
    numeric winkelA, winkelB;
    pair zwwert;
    zwwert := b.coord - a.coord;
    if(xpart(zwwert) = 0): winkelA := 90 * ypart zwwert / abs(ypart zwwert);
    else: winkelA := angle((b.coord - a.coord)); fi
    winkelB := winkelA + 180;
    Curve(a, b, winkelA, winkelB, 1, Label, Position, true, false, true);
  endgroup
enddef;

vardef ZZEdgeR(suffix a, b)(expr Label, Position) =
  begingroup
    save winkelA, winkelB, zwwert;
    numeric winkelA, winkelB;
    pair zwwert;
    zwwert := b.coord - a.coord;
    if(xpart(zwwert) = 0): winkelA := 90 * ypart zwwert / abs(ypart zwwert);
    else: winkelA := angle((b.coord - a.coord)); fi
    winkelB := winkelA + 180;
    Curve(a, b, winkelA, winkelB, 1, Label, Position, false, false, true);
  endgroup
enddef;


% This function is called by every Edge-, Arc- and Curve-function. It draws
% the transition, finds the correct place for the label and draws it. 

def Curve(suffix a, b)(expr angleA, angleB, ncurv, Label, Position, isLeft, isEdge, isZigZag) =
  begingroup
    save pos, controla, controlb, steigungvektor, diff;
    save startpunkt, endpunkt, vektor, zpunkt, lrpunkt, lstartpunkt, rstartpunkt, lendpunkt;
    save winkelb, strichbreite, vlaenge, zschrittweite, faktor, labstand, tstyle, tbreite;
    save bLinks, istLinks, doubled, tfarbe, lineStraight;
    save faktorx, faktory, sinus, pos;

    pair pos, controla, controlb, steigungvektor, diff;
    pair startpunkt, endpunkt, vektor, zpunkt, lrpunkt, lstartpunkt, rstartpunkt, lendpunkt;
    numeric winkelb, strichbreite, vlaenge, zschrittweite, faktor, labstand, tstyle, tbreite;
    numeric faktorx, faktory, sinus, pos;
    boolean bLinks, istLinks, doubled;
    color tfarbe;
    path line, lineStraight;  % "line" must be known after execution for adding further labels

    if(globalDimEdge):
      tfarbe           := globalDimEdgeLineColorCurrent;
      globalLabelColor := globalDimEdgeLabelColorCurrent;
      tstyle           := globalDimEdgeLineStyleCurrent;
      tbreite          := globalEdgeLineWidthCurrent * globalDimEdgeLineCoefCurrent;
    else:
      tfarbe           := globalEdgeLineColorCurrent;
      globalLabelColor := globalEdgeLabelColorCurrent;
      tstyle           := globalEdgeLineStyleCurrent;
      tbreite          := globalEdgeLineWidthCurrent;
    fi
    if(isZigZag): tbreite := tbreite * globalZZLineWidthCurrent; fi
    doubled := globalEdgeLineDblStatusCurrent;

    pos := Position;
    winkelb := angleB + 180;
    line := a.coord{dir angleA}..{dir winkelb}b.coord;

    if(isZigZag):
      bLinks      := true;
      lineStraight  := line cutbefore a.frame cutafter b.frame;
      startpunkt    := point 0 of lineStraight;
      endpunkt      := point 1 of lineStraight;
      vektor        := endpunkt - startpunkt;
      if(xpart vektor = 0): sinus := 1 * ypart vektor / abs(ypart vektor);
      else: sinus    := sind(angle(vektor)); fi
      if(abs(sinus) > 0.01):
        vlaenge     := abs(ypart vektor / sinus);         %%% hypotenuse
        faktorx     := globalZZSizeCurrent / 2 / vlaenge; %%% shifting factor (tips)
        faktory     := faktorx;                           %%% shifting factor (tips)
      else:
        vlaenge     := abs(xpart endpunkt - xpart startpunkt);
        faktorx     := 0;                                 %%% shifting factor (tips)
        faktory     := globalZZSizeCurrent / 2 / vlaenge; %%% shifting factor (tips)
      fi
      lstartpunkt   := startpunkt shifted (-1 * ypart vektor * faktorx, xpart vektor * faktory);
      rstartpunkt   := startpunkt shifted (ypart vektor * faktorx, -1 * xpart vektor * faktory);
      line          := startpunkt;
      zschrittweite := globalZZSizeCurrent / 3;
      if(vlaenge < 0.65cm): line := line--endpunkt;
      else:
        for j=0.3cm step zschrittweite until (vlaenge-0.3cm): 
          zpunkt := startpunkt + (j / vlaenge) * vektor;
          if(bLinks): lrpunkt := lstartpunkt + (j / vlaenge + zschrittweite / vlaenge / 2) * vektor;
          else: lrpunkt := rstartpunkt + (j / vlaenge + zschrittweite / vlaenge / 2) * vektor;  fi
          if(j+zschrittweite < vlaenge-0.3cm): line := line--zpunkt--lrpunkt; fi
          bLinks := not bLinks;
        endfor
        line := line--zpunkt--endpunkt;
      fi
    elseif(globalForthBackOffset and isEdge):
      line        := line cutbefore a.frame cutafter b.frame;
      startpunkt  := point 0 of line;
      endpunkt    := point 1 of line; 
      vektor      := endpunkt - startpunkt;
      if(xpart vektor = 0): sinus = 1 * ypart vektor / abs(ypart vektor);
      else: sinus  := sind(angle(vektor)); fi
      if(abs(sinus) > 0.01):
        vlaenge     := abs(ypart vektor / sinus); %%% hypotenuse
        faktorx     := 2 * tbreite / vlaenge;     %%% shifting factor
        faktory     := faktorx;                   %%% shifting factor
      else:
        vlaenge     := abs(xpart endpunkt - xpart startpunkt);
        faktorx     := 0;                         %%% shifting factor
        faktory     := 2 * tbreite / vlaenge;     %%% shifting factor
      fi
      line        := line shifted (-1 * ypart vektor * faktorx, xpart vektor * faktory);
    elseif(ncurv <> 1):
      controla := postcontrol 0 of line;
      controlb := precontrol 1 of line;
      controla := a.coord + ncurv * (controla - a.coord);
      controlb := b.coord + ncurv * (controlb - b.coord);
      line := a.coord..controls controla and controlb..b.coord;
      line := line cutbefore a.frame cutafter b.frame;
    else:
      line := line cutbefore a.frame cutafter b.frame;
    fi

    if(globalReverseArrow): line := reverse line; istLinks := not isLeft; pos := 1 - pos;
    else: istLinks := isLeft; fi

    if(globalEdgeBordered):
      if(doubled):
        strichbreite := (2 * globalEdgeLineBorderCoefCurrent + 2 * globalEdgeLineDblCoefCurrent + globalEdgeLineDblSepCurrent) * tbreite;
        pickup pencircle scaled strichbreite;
        linecap := butt;
        draw line withcolor globalEdgeLineBorderColorCurrent;
        linecap := rounded;
        strichbreite := (2 * globalEdgeLineDblCoefCurrent + globalEdgeLineDblSepCurrent) * tbreite;
        pickup pencircle scaled strichbreite;
        if(tstyle = solid):    drawarrow line withcolor tfarbe;
        elseif(tstyle = dash): drawarrow line dashed evenly withcolor tfarbe;
        elseif(tstyle = dot):  drawarrow line dashed withdots withcolor tfarbe;
        elseif(tstyle = none): ; fi
        strichbreite := globalEdgeLineDblSepCurrent * tbreite;
        pickup pencircle scaled strichbreite; 
        draw line withcolor globalEdgeLineBorderColorCurrent;
      else:
        strichbreite := 2 * globalEdgeLineBorderCoefCurrent * tbreite + tbreite;
        pickup pencircle scaled strichbreite;
        linecap := butt;
        draw line withcolor globalEdgeLineBorderColorCurrent;
        linecap := rounded;
        pickup pencircle scaled tbreite;
        if(tstyle = solid):    drawarrow line withcolor tfarbe;
        elseif(tstyle = dash): drawarrow line dashed evenly withcolor tfarbe;
        elseif(tstyle = dot):  drawarrow line dashed withdots withcolor tfarbe;
        elseif(tstyle = none): ; fi
      fi
    else:
      if(doubled):
        strichbreite := (2 * globalEdgeLineDblCoefCurrent + globalEdgeLineDblSepCurrent) * tbreite;
        pickup pencircle scaled strichbreite;
        if(tstyle = solid):    drawarrow line withcolor tfarbe;
        elseif(tstyle = dash): drawarrow line dashed evenly withcolor tfarbe;
        elseif(tstyle = dot):  drawarrow line dashed withdots withcolor tfarbe;
        elseif(tstyle = none): ; fi
        strichbreite := globalEdgeLineDblSepCurrent * tbreite;
        pickup pencircle scaled strichbreite; 
        draw line withcolor white;
      else:
        pickup pencircle scaled tbreite;
        if(tstyle = solid):    drawarrow line withcolor tfarbe;
        elseif(tstyle = dash): drawarrow line dashed evenly withcolor tfarbe;
        elseif(tstyle = dot):  drawarrow line dashed withdots withcolor tfarbe;
        elseif(tstyle = none): ; fi
      fi
    fi

    labstand := 0.1cm;
    if(isZigZag):           labstand := labstand + globalZZSizeCurrent / 2;  line := lineStraight; fi
    if(doubled):            labstand := labstand + (globalEdgeLineDblCoefCurrent + 0.5 * globalEdgeLineDblSepCurrent) * tbreite;
    else:                   labstand := labstand + 0.5 * tbreite;  fi
    if(globalEdgeBordered): labstand := labstand + globalEdgeLineBorderCoefCurrent * tbreite; fi
    globalLabelOffset := labstand;

    AddLabel(pos, Label, istLinks);
  endgroup
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% Label %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Add extra label to transition; must be called directly after Edge, Arc or Curve command

vardef LabelL(expr Position, Label) = AddLabel(Position, Label, true ); enddef;
vardef LabelR(expr Position, Label) = AddLabel(Position, Label, false); enddef;


% Add label to transition / loop / initial / final; the path "line" must be known

vardef AddLabel(expr Position, Label, isLeft) =
  begingroup
    save steigung, steigungvektor, pos, steigungx, steigungy;
    numeric steigung, steigungx, steigungy;
    pair pos, steigungvektor;

    labeloffset    := globalLabelOffset;
    defaultscale   := globalEdgeLabelScaleCurrent;
    pos            := point Position of line;
    steigungvektor := direction Position of line; % Steigung an diesem Punkt als Vektor
    steigungx      := xpart steigungvektor;
    steigungy      := ypart steigungvektor;

    %%% To hinder arithmetic overflows, for every xpart that is
    %%% in ]-0.01, 0.01[ the direction is set to infinity
    if(abs(steigungx) < 0.01):
      steigungx := 0;
      steigung  := steigungy * 10;     %%% value is not used
    else: steigung := ypart steigungvektor / xpart steigungvektor; fi

    if(isLeft):
      if(steigungx < 0):
        if(steigungy < 0):
          if(steigungx = 0):            label.rt(Label, pos) withcolor globalLabelColor;
          elseif(steigung < 0.2):       label.bot(Label, pos) withcolor globalLabelColor;
          elseif(steigung > 5):         label.rt(Label, pos) withcolor globalLabelColor;
          else:                         label.lrt(Label, pos) withcolor globalLabelColor;
          fi
        else: 
          if(steigungx = 0):            label.lft(Label, pos) withcolor globalLabelColor;
          elseif(steigung > -0.2):      label.bot(Label, pos) withcolor globalLabelColor;
          elseif(steigung < -5):        label.lft(Label, pos) withcolor globalLabelColor;
          else:                         label.llft(Label, pos) withcolor globalLabelColor;
          fi
        fi
      else:
        if(steigungy < 0):
          if(steigungx = 0):            label.rt(Label, pos) withcolor globalLabelColor;
          elseif(steigung > -0.2):      label.top(Label, pos) withcolor globalLabelColor;
          elseif(steigung < -5):        label.rt(Label, pos) withcolor globalLabelColor;
          else:                         label.urt(Label, pos) withcolor globalLabelColor;
          fi
        else:
          if(steigungx = 0):            label.lft(Label, pos) withcolor globalLabelColor;
          elseif(steigung < 0.2):       label.top(Label, pos) withcolor globalLabelColor;
          elseif(steigung > 5):         label.lft(Label, pos) withcolor globalLabelColor;
          else:                         label.ulft(Label, pos) withcolor globalLabelColor;
          fi
        fi
      fi
    else:
      if(steigungx < 0):
        if(steigungy < 0): 
          if(steigungx = 0):            label.lft(Label, pos) withcolor globalLabelColor;
          elseif(steigung < 0.2):       label.top(Label, pos) withcolor globalLabelColor;
          elseif(steigung > 5):         label.lft(Label, pos) withcolor globalLabelColor;
          else:                         label.ulft(Label, pos) withcolor globalLabelColor;
          fi
        else: 
          if(steigungx = 0):            label.rt(Label, pos) withcolor globalLabelColor;
          elseif(steigung > -0.2):      label.top(Label, pos) withcolor globalLabelColor;
          elseif(steigung < -5):        label.rt(Label, pos) withcolor globalLabelColor;
          else:                         label.urt(Label, pos) withcolor globalLabelColor;
          fi
        fi
      else:
        if(steigungy < 0):
          if(steigungx = 0):            label.lft(Label, pos) withcolor globalLabelColor;
          elseif(steigung > -0.2):      label.bot(Label, pos) withcolor globalLabelColor;
          elseif(steigung < -5):        label.lft(Label, pos) withcolor globalLabelColor;
          else:                         label.llft(Label, pos) withcolor globalLabelColor;
          fi
        else:
          if(steigungx = 0):            label.rt(Label, pos) withcolor globalLabelColor;
          elseif(steigung < 0.2):       label.bot(Label, pos) withcolor globalLabelColor;
          elseif(steigung > 5):         label.rt(Label, pos) withcolor globalLabelColor;
          else:                         label.lrt(Label, pos) withcolor globalLabelColor;
          fi
        fi
      fi
    fi
  endgroup
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% Translation & Rotation %%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% All states (with loops and initial / final arrow) between the commands "TraRot(translation, rotation)"
% and "TraRotEnd" will be drawn translated and rotated, all loops, initial / final arrows and labels of
% transitions rotated

vardef TraRot(expr trala, rot) = 
  begingroup
    globalTranslation := trala;
    globalRotation := rot;
    globalTraRot := true;
  endgroup
enddef;

vardef TraRotEnd = 
  begingroup
    globalTranslation := (0,0);
    globalRotation := 0;
    globalTraRot := false;
  endgroup
enddef;


% Sets globalRigidLabel (whether label of state is rotated in TraRot area) true / false

vardef RigidLabel = globalRigidLabel := true; enddef;
vardef SwivelLabel = globalRigidLabel := false; enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% Parameter of Normal States %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

vardef ChgStateLineColor(expr col) = globalStateLineColorCurrent := col; enddef;
vardef SetStateLineColor(expr col) = begingroup
  globalStateLineColor := col;
  globalStateLineColorCurrent := col;
endgroup enddef;
vardef RstStateLineColor = globalStateLineColorCurrent := globalStateLineColor; enddef;

vardef ChgStateLabelColor(expr col) = globalStateLabelColorCurrent := col; enddef;
vardef SetStateLabelColor(expr col) = begingroup
  globalStateLabelColor := col;
  globalStateLabelColorCurrent := col;
endgroup enddef;
vardef RstStateLabelColor = globalStateLabelColorCurrent := globalStateLabelColor; enddef;

vardef ChgStateFillColor(expr col) = globalStateFillColorCurrent := col; enddef;
vardef SetStateFillColor(expr col) = begingroup
  globalStateFillColor := col;
  globalStateFillColorCurrent := col;
endgroup enddef;
vardef RstStateFillColor = globalStateFillColorCurrent := globalStateFillColor; enddef;

vardef ChgStateLineStyle(expr style) = globalStateLineStyleCurrent := style; enddef;
vardef SetStateLineStyle(expr style) = begingroup
  globalStateLineStyle := style;
  globalStateLineStyleCurrent := style;
endgroup enddef;
vardef RstStateLineStyle = globalStateLineStyleCurrent := globalStateLineStyle; enddef;

vardef ChgStateLineWidth(expr width) = globalStateLineWidthCurrent := width; enddef;
vardef SetStateLineWidth(expr width) = begingroup
  globalStateLineWidth := width;
  globalStateLineWidthCurrent := width;
endgroup enddef;
vardef RstStateLineWidth = globalStateLineWidthCurrent := globalStateLineWidth; enddef;

vardef ChgStateLabelScale(expr fact) = globalStateLabelScaleCurrent := fact; enddef;
vardef SetStateLabelScale(expr fact) = begingroup
  globalStateLabelScale := fact;
  globalStateLabelScaleCurrent := fact;
endgroup enddef;
vardef RstStateLabelScale = globalStateLabelScaleCurrent := globalStateLabelScale; enddef;

vardef ChgStateFillStatus(expr style) = globalStateFillStatusCurrent := style; enddef;
vardef SetStateFillStatus(expr style) = begingroup
  globalStateFillStatus := style;
  globalStateFillStatusCurrent := style;
endgroup enddef;
vardef RstStateFillStatus = globalStateFillStatusCurrent := globalStateFillStatus; enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% Parameter of Doubled States %%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% After calling this function all states / edges will be drawn with a doubled / single line

vardef StateLineDouble = globalStateDoubled := true; enddef;
vardef StateLineSimple = globalStateDoubled := false; enddef;

% Change parameter of states with doubled line

vardef ChgStateLineDblCoef(expr coef) = globalStateLineDblCoef := coef; enddef;
vardef SetStateLineDblCoef(expr coef) = begingroup
  globalStateLineDblCoef := coef;
  globalStateLineDblCoefCurrent := coef;
endgroup enddef;
vardef RstStateLineDblCoef = globalStateLineDblCoefCurrent := globalStateLineDblCoef; enddef;

vardef ChgStateLineDblSep(expr sep) = globalStateLineDblSep := sep; enddef;
vardef SetStateLineDblSep(expr sep) = begingroup
  globalStateLineDblSep := sep;
  globalStateLineDblSepCurrent := sep;
endgroup enddef;
vardef RstStateLineDblSep = globalStateLineDblSepCurrent := globalStateLineDblSep; enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% Dimmed States %%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Normal / dimmed states

vardef DimState = globalDimState := true;  enddef;
vardef RstState = globalDimState := false; enddef;

vardef FixDimState(expr linestyle, linecol, linecoef, labcol, fillcol) =
  begingroup
    ChgDimStateLineStyle(linestyle);
    ChgDimStateLineColor(linecol);
    ChgDimStateLineCoef(linecoef);
    ChgDimStateLabelColor(labcol);
    ChgDimStateFillColor(fillcol);
  endgroup
enddef;

vardef ChgDimStateLineStyle(expr style) = globalDimStateLineStyleCurrent := style; enddef;
vardef SetDimStateLineStyle(expr style) = begingroup
  globalDimStateLineStyle := style;
  globalDimStateLineStyleCurrent := style;
endgroup enddef;
vardef RstDimStateLineStyle = globalDimStateLineStyleCurrent := globalDimStateLineStyle; enddef;

vardef ChgDimStateLineColor(expr col) = globalDimStateLineColorCurrent := col; enddef;
vardef SetDimStateLineColor(expr col) = begingroup
  globalDimStateLineColor := col;
  globalDimStateLineColorCurrent := col;
endgroup enddef;
vardef RstDimStateLineColor = globalDimStateLineColorCurrent := globalDimStateLineColor; enddef;

vardef ChgDimStateLineCoef(expr coef) = globalDimStateLineCoefCurrent := coef; enddef;
vardef SetDimStateLineCoef(expr coef) = begingroup
  globalDimStateLineCoef := coef;
  globalDimStateLineCoefCurrent := coef;
endgroup enddef;
vardef RstDimStateLineCoef = globalDimStateLineCoefCurrent := globalDimStateLineCoef; enddef;

vardef ChgDimStateLabelColor(expr col) = globalDimStateLabelColorCurrent := col; enddef;
vardef SetDimStateLabelColor(expr col) = begingroup
  globalDimStateLabelColor := col;
  globalDimStateLabelColorCurrent := col;
endgroup enddef;
vardef RstDimStateLabelColor = globalDimStateLabelColorCurrent := globalDimStateLabelColor; enddef;

vardef ChgDimStateFillColor(expr col) = globalDimStateFillColorCurrent := col; enddef;
vardef SetDimStateFillColor(expr col) = begingroup
  globalDimStateFillColor := col;
  globalDimStateFillColorCurrent := col;
endgroup enddef;
vardef RstDimStateFillColor = globalDimStateFillColorCurrent := globalDimStateFillColor; enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%% State Size %%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Sets the state size to small, medium or large size

vardef SmallState  =
  begingroup
    globalStateSize := 0.6cm;
    globalCurrentState := larssmall;
  endgroup
enddef;

vardef MediumState = 
  begingroup
    globalStateSize := 0.9cm;
    globalCurrentState := larsmedium;
  endgroup
enddef;

vardef LargeState  = 
  begingroup
    globalStateSize := 1.2cm;
    globalCurrentState := larslarge;
  endgroup
enddef;


% Sets the state size to Diam

vardef FixStateDiameter(expr Diam) =
  begingroup
    globalStateSize := Diam;
    globalCurrentState := larsvariable;
  endgroup
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%% Hide State %%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Hided states are not displayed

vardef HideState = globalHideState := true; enddef;
vardef ShowState = globalHideState := false; enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Initial / Final Arrow Length %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

vardef ChgArrowOnLargeState(expr value) = globalArrowOnLargeStateCurrent := value; enddef;
vardef SetArrowOnLargeState(expr value) = begingroup
  globalArrowOnLargeState := value;
  globalArrowOnLargeStateCurrent := value;
endgroup enddef;
vardef RstArrowOnLargeState = globalArrowOnLargeStateCurrent := globalArrowOnLargeState; enddef;

vardef ChgArrowOnMediumState(expr value) = globalArrowOnMediumStateCurrent := value; enddef;
vardef SetArrowOnMediumState(expr value) = begingroup
  globalArrowOnMediumState := value;
  globalArrowOnMediumStateCurrent := value;
endgroup enddef;
vardef RstArrowOnMediumState = globalArrowOnMediumStateCurrent := globalArrowOnMediumState; enddef;

vardef ChgArrowOnSmallState(expr value) = globalArrowOnSmallStateCurrent := value; enddef;
vardef SetArrowOnSmallState(expr value) = begingroup
  globalArrowOnSmallState := value;
  globalArrowOnSmallStateCurrent := value;
endgroup enddef;
vardef RstArrowOnSmallState = globalArrowOnSmallStateCurrent := globalArrowOnSmallState; enddef;

vardef ChgArrowOnVerySmallState(expr value) = globalArrowOnVerySmallStateCurrent := value; enddef;
vardef SetArrowOnVerySmallState(expr value) = begingroup
  globalArrowOnVerySmallState := value;
  globalArrowOnVerySmallStateCurrent := value;
endgroup enddef;
vardef RstArrowOnVerySmallState = globalArrowOnVerySmallStateCurrent := globalArrowOnVerySmallState; enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%% Parameters of Loops %%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

vardef ChgLoopAngle(expr value) = globalLoopAngleCurrent := value; enddef;
vardef SetLoopAngle(expr value) = begingroup
  globalLoopAngle := value;
  globalLoopAngleCurrent := value;
endgroup enddef;
vardef RstLoopAngle = globalLoopAngleCurrent := globalLoopAngle; enddef;

vardef ChgCLoopAngle(expr value) = globalCLoopAngleCurrent := value; enddef;
vardef SetCLoopAngle(expr value) = begingroup
  globalCLoopAngle := value;
  globalCLoopAngleCurrent := value;
endgroup enddef;
vardef RstCLoopAngle = globalCLoopAngleCurrent := globalCLoopAngle; enddef;

vardef ChgLoopOnLargeState(expr value) = globalLoopOnLargeStateCurrent := value; enddef;
vardef SetLoopOnLargeState(expr value) = begingroup
  globalLoopOnLargeState := value;
  globalLoopOnLargeStateCurrent := value;
endgroup enddef;
vardef RstLoopOnLargeState = globalLoopOnLargeStateCurrent := globalLoopOnLargeState; enddef;

vardef ChgLoopOnMediumState(expr value) = globalLoopOnMediumStateCurrent := value; enddef;
vardef SetLoopOnMediumState(expr value) = begingroup
  globalLoopOnMediumState := value;
  globalLoopOnMediumStateCurrent := value;
endgroup enddef;
vardef RstLoopOnMediumState = globalLoopOnMediumStateCurrent := globalLoopOnMediumState; enddef;

vardef ChgLoopOnSmallState(expr value) = globalLoopOnSmallStateCurrent := value; enddef;
vardef SetLoopOnSmallState(expr value) = begingroup
  globalLoopOnSmallState := value;
  globalLoopOnSmallStateCurrent := value;
endgroup enddef;
vardef RstLoopOnSmallState = globalLoopOnSmallStateCurrent := globalLoopOnSmallState; enddef;

vardef ChgLoopOnVerySmallState(expr value) = globalLoopOnVerySmallStateCurrent := value; enddef;
vardef SetLoopOnVerySmallState(expr value) = begingroup
  globalLoopOnVerySmallState := value;
  globalLoopOnVerySmallStateCurrent := value;
endgroup enddef;
vardef RstLoopOnVerySmallState = globalLoopOnVerySmallStateCurrent := globalLoopOnVerySmallState; enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Parameters of Normal Transitions %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

vardef ChgEdgeLineDblStatus(expr dbl) = globalEdgeLineDblStatusCurrent := dbl; enddef;
vardef SetEdgeLineDblStatus(expr dbl) = begingroup
  globalEdgeLineDblStatus := dbl;
  globalEdgeLineDblStatusCurrent := dbl;
endgroup enddef;
vardef RstEdgeLineDblStatus = globalEdgeLineDblStatusCurrent := globalEdgeLineDblStatus; enddef;

vardef ChgEdgeLineColor(expr col) = globalEdgeLineColorCurrent := col; enddef;
vardef SetEdgeLineColor(expr col) = begingroup
  globalEdgeLineColor := col;
  globalEdgeLineColorCurrent := col;
endgroup enddef;
vardef RstEdgeLineColor = globalEdgeLineColorCurrent := globalEdgeLineColor; enddef;

vardef ChgEdgeLabelColor(expr col) = globalEdgeLabelColorCurrent := col; enddef;
vardef SetEdgeLabelColor(expr col) = begingroup
  globalEdgeLabelColor := col;
  globalEdgeLabelColorCurrent := col;
endgroup enddef;
vardef RstEdgeLabelColor = globalEdgeLabelColorCurrent := globalEdgeLabelColor; enddef;

vardef ChgEdgeLineStyle(expr style) = globalEdgeLineStyleCurrent := style; enddef;
vardef SetEdgeLineStyle(expr style) = begingroup
  globalEdgeLineStyle := style;
  globalEdgeLineStyleCurrent := style;
endgroup enddef;
vardef RstEdgeLineStyle = globalEdgeLineStyleCurrent := globalEdgeLineStyle; enddef;

vardef ChgEdgeLineWidth(expr width) = globalEdgeLineWidthCurrent := width; enddef;
vardef SetEdgeLineWidth(expr width) = begingroup
  globalEdgeLineWidth := width;
  globalEdgeLineWidthCurrent := width;
endgroup enddef;
vardef RstEdgeLineWidth = globalEdgeLineWidthCurrent := globalEdgeLineWidth; enddef;

vardef ChgEdgeLabelScale(expr scale) = globalEdgeLabelScaleCurrent := scale; enddef;
vardef SetEdgeLabelScale(expr scale) = begingroup
  globalEdgeLabelScale := scale;
  globalEdgeLabelScaleCurrent := scale;
endgroup enddef;
vardef RstEdgeLabelScale = globalEdgeLabelScaleCurrent := globalEdgeLabelScale; enddef;

vardef ChgArcAngle(expr value) = globalArcAngleCurrent := value; enddef;
vardef SetArcAngle(expr value) = begingroup
  globalArcAngle := value;
  globalArcAngleCurrent := value;
endgroup enddef;
vardef RstArcAngle = globalArcAngleCurrent := globalArcAngle; enddef;

vardef ChgLArcAngle(expr value) = globalLArcAngleCurrent := value; enddef;
vardef SetLArcAngle(expr value) = begingroup
  globalLArcAngle := value;
  globalLArcAngleCurrent := value;
endgroup enddef;
vardef RstLArcAngle = globalLArcAngleCurrent := globalLArcAngle; enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Parameters of Bordered Transitions %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

vardef EdgeBorder = globalEdgeBordered := true; enddef;
vardef EdgeBorderOff = globalEdgeBordered := false; enddef;

vardef ChgEdgeLineBorderColor(expr col) = globalEdgeLineBorderColorCurrent := col; enddef;
vardef SetEdgeLineBorderColor(expr col) = begingroup
  globalEdgeLineBorderColor := col;
  globalEdgeLineBorderColorCurrent := col;
endgroup enddef;
vardef RstEdgeLineBorderColor = globalEdgeLineBorderColorCurrent := globalEdgeLineBorderColor; enddef;

vardef ChgEdgeLineBorderCoef(expr coef) = globalEdgeLineBorderCoefCurrent := coef; enddef;
vardef SetEdgeLineBorderCoef(expr coef) = begingroup
  globalEdgeLineBorderCoef := coef;
  globalEdgeLineBorderCoefCurrent := coef;
endgroup enddef;
vardef RstEdgeLineBorderCoef = globalEdgeLineBorderCoefCurrent := globalEdgeLineBorderCoef; enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Parameters of Doubled Transitions %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
vardef EdgeLineDouble = globalEdgeLineDblStatusCurrent := true; enddef;
vardef EdgeLineSimple = globalEdgeLineDblStatusCurrent := false; enddef;

vardef ChgEdgeLineDblCoef(expr coef) = globalEdgeLineDblCoefCurrent := coef; enddef;
vardef SetEdgeLineDblCoef(expr coef) = begingroup
  globalEdgeLineDblCoef := coef;
  globalEdgeLineDblCoefCurrent := coef;
endgroup enddef;
vardef RstEdgeLineDblCoef = globalEdgeLineDblCoefCurrent := globalEdgeLineDblCoef; enddef;

vardef ChgEdgeLineDblSep(expr sep) = globalEdgeLineDblSepCurrent := sep; enddef;
vardef SetEdgeLineDblSep(expr sep) = begingroup
  globalEdgeLineDblSep := sep;
  globalEdgeLineDblSepCurrent := sep;
endgroup enddef;
vardef RstEdgeLineDblSep = globalEdgeLineDblSepCurrent := globalEdgeLineDblSep; enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Parameters of Dimmed Transitions %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

vardef DimEdge  = globalDimEdge  := true;  enddef;
vardef RstEdge  = globalDimEdge  := false; enddef;

vardef ChgDimEdgeLineColor(expr col) = globalDimEdgeLineColorCurrent := col; enddef;
vardef SetDimEdgeLineColor(expr col) = begingroup
  globalDimEdgeLineColor := col;
  globalDimEdgeLineColorCurrent := col;
endgroup enddef;
vardef RstDimEdgeLineColor = globalDimEdgeLineColorCurrent := globalDimEdgeLineColor; enddef;

vardef ChgDimEdgeLabelColor(expr col) = globalDimEdgeLabelColorCurrent := col; enddef;
vardef SetDimEdgeLabelColor(expr col) = begingroup
  globalDimEdgeLabelColor := col;
  globalDimEdgeLabelColorCurrent := col;
endgroup enddef;
vardef RstDimEdgeLabelColor = globalDimEdgeLabelColorCurrent := globalDimEdgeLabelColor; enddef;

vardef ChgDimEdgeLineStyle(expr style) = globalDimEdgeLineStyleCurrent := style; enddef;
vardef SetDimEdgeLineStyle(expr style) = begingroup
  globalDimEdgeLineStyle := style;
  globalDimEdgeLineStyleCurrent := style;
endgroup enddef;
vardef RstDimEdgeLineStyle = globalDimEdgeLineStyleCurrent := globalDimEdgeLineStyle; enddef;

vardef ChgDimEdgeLineCoef(expr coef) = globalDimEdgeLineCoefCurrent := coef; enddef;
vardef SetDimEdgeLineCoef(expr coef) = begingroup
  globalDimEdgeLineCoef := coef;
  globalDimEdgeLineCoefCurrent := coef;
endgroup enddef;
vardef RstDimEdgeLineCoef = globalDimEdgeLineCoefCurrent := globalDimEdgeLineCoef; enddef;

vardef FixDimEdge(expr linestyle, linecol, linecoef, labelcol) =
  begingroup
    ChgDimEdgeLineStyle(linestyle);
    ChgDimEdgeLineColor(linecol);
    ChgDimEdgeLineCoef(linecoef);
    ChgDimEdgeLabelColor(labelcol);
  endgroup
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% Forth Back Offset For Edges %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% sets offset for edges, so that two straight lines can be drawn side by side

vardef ForthBackOffset = globalForthBackOffset := true;  enddef;
vardef RstEdgeOffset   = globalForthBackOffset := false; enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%% Zigzag Transitions %%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

vardef ChgZZSize(expr size) = globalZZSizeCurrent := size; enddef;
vardef SetZZSize(expr size) = begingroup
  globalZZSize := size;
  globalZZSizeCurrent := size;
endgroup enddef;
vardef RstZZSize = globalZZSizeCurrent := globalZZSize; enddef;

vardef ChgZZLineWidth(expr wid) = globalZZLineWidthCurrent := wid; enddef;
vardef SetZZLineWidth(expr wid) = begingroup
  globalZZLineWidth := wid;
  globalZZLineWidthCurrent := wid;
endgroup enddef;
vardef RstZZLineWidth = globalZZLineWidthCurrent := globalZZLineWidth; enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%% Reverse Transitions %%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

vardef ReverseArrow = globalReverseArrow := true; enddef;
vardef StraightArrow = globalReverseArrow := false; enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Parameters of Transition Arrow Head %%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% Note that ahangle and ahlength are Metapost variables

vardef ChgEdgeArrowAngle(expr value) = ahangle := value; enddef;
vardef SetEdgeArrowAngle(expr value) = begingroup
  globalEdgeArrowAngle := value;
  ahangle := value;
endgroup enddef;
vardef RstEdgeArrowAngle = ahangle := globalEdgeArrowAngle; enddef;

vardef ChgEdgeArrowLength(expr value) = ahlength := value; enddef;
vardef SetEdgeArrowLength(expr value) = begingroup
  globalEdgeArrowLength := value;
  ahlength := value;
endgroup enddef;
vardef RstEdgeArrowLength = ahlength := globalEdgeArrowLength; enddef;


















