/**
   * Assumes that the polygons are disjoint. Find the vertex indices corresponding to the end points
   * of the two connecting edges.
   *
   * @param polygon1 the first polygon
   * @param polygon2 the second polygon
   * @param verticesIndices in[2][2] contains the indexes of the connecting edges end points. The
   *     row index refers to which polygon the vertex index belongs to, whereas the column index
   *     refers to which connecting edge the vertex index belongs to. For example,
   *     vertexIndexes[0][1] is the index of the vertex of the first polygon, also end point the
   *     second connecting edge.
   * @return success (false = failed, true = succeeded)
   */
  public static boolean findConnectingEdgesVerticesIndexes(
      ConvexPolygon2d polygon1, ConvexPolygon2d polygon2, int[][] verticesIndices) {
    boolean success = false;

    if (polygon1.isEmpty() || polygon2.isEmpty()) return false;

    if (polygon1.hasExactlyOneVertex() && polygon2.hasExactlyOneVertex()) {
      verticesIndices[0][0] = 0;
      verticesIndices[0][1] = 0;
      verticesIndices[1][0] = 0;
      verticesIndices[1][1] = 0;
      return true;
    }

    if (polygon1.hasExactlyOneVertex()) {
      verticesIndices[0][0] = 0;
      verticesIndices[0][1] = 0;
      success = polygon2.getLineOfSightVerticesIndices(polygon1.getVertex(0), verticesIndices[1]);
      return success;
    }

    if (polygon2.hasExactlyOneVertex()) {
      verticesIndices[1][0] = 0;
      verticesIndices[1][1] = 0;
      success = polygon1.getLineOfSightVerticesIndices(polygon2.getVertex(0), verticesIndices[0]);
      return success;
    }

    // First pick a random vertex from polygon1
    Point2d vertex = polygon1.getVertex(0);

    int[] lineOfSight1 = new int[2];
    int[] lineOfSight2 = new int[2];
    int L1, R1, L2, R2;

    // Then find its two line of sight points on polygon 2:
    success = polygon2.getLineOfSightVerticesIndices(vertex, lineOfSight1);
    if (!success) return false;

    L2 = lineOfSight1[0];
    R2 = lineOfSight1[1];

    // Then find the line of sight vertices on polygon 1:
    success = polygon1.getLineOfSightVerticesIndices(polygon2.getVertex(R2), lineOfSight1);
    if (!success) return false;
    success = polygon1.getLineOfSightVerticesIndices(polygon2.getVertex(L2), lineOfSight2);
    if (!success) return false;

    L1 = lineOfSight1[0];
    R1 = lineOfSight2[1];

    // Find the line of sight vertices back and forth between the two polygons until they are
    // constant between two iterations.
    boolean done = false;
    while (!done) {
      success = polygon2.getLineOfSightVerticesIndices(polygon1.getVertex(L1), lineOfSight1);
      if (!success) return false;
      success = polygon2.getLineOfSightVerticesIndices(polygon1.getVertex(R1), lineOfSight2);
      if (!success) return false;

      if ((L2 == lineOfSight2[0]) && (R2 == lineOfSight1[1])) {
        done = true;
        break;
      }

      L2 = lineOfSight2[0];
      R2 = lineOfSight1[1];

      success = polygon1.getLineOfSightVerticesIndices(polygon2.getVertex(L2), lineOfSight1);
      if (!success) return false;
      success = polygon1.getLineOfSightVerticesIndices(polygon2.getVertex(R2), lineOfSight2);
      if (!success) return false;

      if ((L1 == lineOfSight2[0]) && (R1 == lineOfSight1[1])) {
        done = true;
        break;
      }

      L1 = lineOfSight2[0];
      R1 = lineOfSight1[1];
    }

    verticesIndices[0][0] = R1;
    verticesIndices[0][1] = L1;
    verticesIndices[1][0] = L2;
    verticesIndices[1][1] = R2;
    return true;
  }