/**
   * Outputs filled with points listOfPointsToBeConnected. Finds the point with the lowest Y - that
   * will be the first point of the broken line. If It finds several points with equal Ys - makes a
   * line from the most left (lowest X) point to the most right point, then connects this point with
   * next point with the lowest Y from the remaining set. The last point of the broken line will be
   * the point with the highest Y, or if there will be several points with the highest Y - the point
   * with the highest X from this set.
   */
  void connectPoints() {

    ArrayList<MyPoint> pointsInOneRow =
        new ArrayList<MyPoint>(); // will store points with equal Ys.
    MyPoint currentPoint, nextPoint;
    ListIterator<MyPoint> itr;

    while (randomListOfPoints.size() > 0) {

      pointsInOneRow.clear(); // clear the pointsInOneRow.
      itr = randomListOfPoints.listIterator();
      // initialize list iterator and place it before the first element in the randomListOfPoints.
      currentPoint = itr.next(); // the first element from the randomListOfPoints.
      itr.remove(); // delete the first element from the randomListOfPoints.
      if (itr.hasNext()) { // if it's not the end of the randomListOfPoints.

        nextPoint = itr.next(); // the second element from the randomListOfPoints.

      } else {

        // the point not from the range of possible Xs and Ys, so we can be sure that its' Y won't
        // be equal to the currentPoints'.
        nextPoint = new MyPoint(-1, -1);
      }
      pointsInOneRow.add(
          currentPoint); // add current point to a list of points, that lies on one line.
      // if the currentPoint and the nextPoint are on the same line, that is parallel to the X axis.
      while (currentPoint.getY() == nextPoint.getY()) {

        pointsInOneRow.add(
            nextPoint); // add the nextPoint to a list of points, that lies on one line.
        itr.remove(); // delete the second element from the randomListOfPoints .
        currentPoint = nextPoint; // the currentPoint equals to the nextPoint now.
        if (itr.hasNext()) { // if it's not the end of the randomListOfPoints.

          nextPoint = itr.next(); // the second element from the randomListOfPoints.

        } else {

          // the point not from the range of possible Xs and Ys, so we can be sure that its' Y won't
          // be equal to the currentPoints'.
          nextPoint = new MyPoint(-1, -1);
        }
      }

      Collections.sort(
          pointsInOneRow, new XcoordSorterComparator()); // sort the pointsInOneRow by X
      /* add all elements from the pointsInOneRow to the end of the listOfPointsToBeConnected.
       * If the listOfPointsToBeConnected.size == 0 - the first element from the pointsInOneRow will be the start
       * of the broken line, if the listOfPointsToBeConnected.size != 0 - the first element from the
       * pointsInOneRow will be connected with the last element from the listOfPointsToBeConnected*/
      listOfPointsToBeConnected.addAll(listOfPointsToBeConnected.size(), pointsInOneRow);
    }

    System.out.println("\n\nList of connected points:\n" + listOfPointsToBeConnected);
  }
 @Override
 public int compare(MyPoint o1, MyPoint o2) {
   return (int) o1.getY() - (int) o2.getY();
 }