code: public static LinkVar[] getVarsByDist(int head, boolean isRight, LinkVar[] rootVars, LinkVar[][] childVars) {
          int dir = isRight ? 1 : -1;
          int maxDist = isRight ? rootVars.length - head - 1: head - 1;
          LinkVar[] varsByDist = new LinkVar[maxDist];
          for (int cDist = 1; cDist <= maxDist; cDist++) {
              int c = head + dir*cDist;
              if (head == -1) {
                  varsByDist[cDist-1] = rootVars[c];
              } else {
                  varsByDist[cDist-1] = childVars[head][c];
              }
          }
          return varsByDist;
      }
docs: Gets the variables for this factor ordered from the head outward.
  @param head The 0-indexed head (i.e. -1 denotes the wall).
  @param isRight Whether or not this is a right or left head automata.
  @param rootVars The link vars with the wall as parent.
  @param childVars The other child variables.
  @return An array of variables order by increasing distance from the head.