nodes: 0, module; 1, function_definition; 2, function_name:sort_segment_points; 3, parameters; 4, block; 5, identifier:Aps; 6, identifier:Bps; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, for_statement; 12, for_statement; 13, return_statement; 14, comment:"""Takes two line segments and sorts all their points,
      so that they form a continuous path
  
      Args:
          Aps: Array of tracktotrip.Point
          Bps: Array of tracktotrip.Point
      Returns:
          Array with points ordered
      """; 15, assignment; 16, assignment; 17, call; 18, identifier:i; 19, call; 20, block; 21, identifier:m; 22, call; 23, block; 24, identifier:mid; 25, identifier:mid; 26, list; 27, identifier:j; 28, integer:0; 29, attribute; 30, argument_list; 31, identifier:range; 32, argument_list; 33, expression_statement; 34, for_statement; 35, expression_statement; 36, identifier:range; 37, argument_list; 38, expression_statement; 39, identifier:mid; 40, identifier:append; 41, subscript; 42, binary_operator:len(Aps)-1; 43, assignment; 44, identifier:m; 45, call; 46, block; 47, call; 48, identifier:j; 49, call; 50, call; 51, identifier:Aps; 52, integer:0; 53, call; 54, integer:1; 55, identifier:dist; 56, call; 57, identifier:range; 58, argument_list; 59, expression_statement; 60, if_statement; 61, attribute; 62, argument_list; 63, identifier:len; 64, argument_list; 65, attribute; 66, argument_list; 67, identifier:len; 68, argument_list; 69, identifier:distance_tt_point; 70, argument_list; 71, identifier:j; 72, call; 73, assignment; 74, comparison_operator:dist > distm; 75, block; 76, identifier:mid; 77, identifier:append; 78, subscript; 79, identifier:Bps; 80, identifier:mid; 81, identifier:append; 82, subscript; 83, identifier:Aps; 84, subscript; 85, subscript; 86, identifier:len; 87, argument_list; 88, identifier:distm; 89, call; 90, identifier:dist; 91, identifier:distm; 92, expression_statement; 93, if_statement; 94, identifier:Aps; 95, binary_operator:i+1; 96, identifier:Bps; 97, identifier:m; 98, identifier:Aps; 99, identifier:i; 100, identifier:Aps; 101, binary_operator:i+1; 102, identifier:Bps; 103, identifier:distance_tt_point; 104, argument_list; 105, assignment; 106, comparison_operator:direction > 0; 107, block; 108, identifier:i; 109, integer:1; 110, identifier:i; 111, integer:1; 112, subscript; 113, subscript; 114, identifier:direction; 115, call; 116, identifier:direction; 117, integer:0; 118, expression_statement; 119, expression_statement; 120, break_statement; 121, identifier:Aps; 122, identifier:i; 123, identifier:Bps; 124, identifier:m; 125, identifier:dot; 126, argument_list; 127, assignment; 128, call; 129, call; 130, call; 131, identifier:j; 132, binary_operator:m + 1; 133, attribute; 134, argument_list; 135, identifier:normalize; 136, argument_list; 137, identifier:normalize; 138, argument_list; 139, identifier:m; 140, integer:1; 141, identifier:mid; 142, identifier:append; 143, subscript; 144, call; 145, call; 146, identifier:Bps; 147, identifier:m; 148, identifier:line; 149, argument_list; 150, attribute; 151, argument_list; 152, call; 153, call; 154, subscript; 155, identifier:gen2arr; 156, attribute; 157, argument_list; 158, attribute; 159, argument_list; 160, identifier:Bps; 161, identifier:m; 162, subscript; 163, identifier:gen2arr; 164, subscript; 165, identifier:gen2arr; 166, identifier:Aps; 167, identifier:i; 168, identifier:Aps; 169, binary_operator:i+1; 170, identifier:i; 171, integer:1
code: def sort_segment_points(Aps, Bps):
      """Takes two line segments and sorts all their points,
      so that they form a continuous path
  
      Args:
          Aps: Array of tracktotrip.Point
          Bps: Array of tracktotrip.Point
      Returns:
          Array with points ordered
      """
      mid = []
      j = 0
      mid.append(Aps[0])
      for i in range(len(Aps)-1):
          dist = distance_tt_point(Aps[i], Aps[i+1])
          for m in range(j, len(Bps)):
              distm = distance_tt_point(Aps[i], Bps[m])
              if dist > distm:
                  direction = dot(normalize(line(Aps[i].gen2arr(), Aps[i+1].gen2arr())), normalize(Bps[m].gen2arr()))
                  if direction > 0:
                      j = m + 1
                      mid.append(Bps[m])
                      break
  
          mid.append(Aps[i+1])
      for m in range(j, len(Bps)):
          mid.append(Bps[m])
      return mid