Example #1
0
  /**
   * Intersect the rectangle with another rectangle.
   *
   * @param other the other rectangle
   * @return the intersection of this rectangle with the other one
   */
  @Nullable
  public Rect intersect(Rect other) {
    Rect intersection;

    int left1 = getX();
    int right1 = getX() + getWidth();
    int top1 = getY();
    int bottom1 = getY() + getHeight();

    int left2 = other.getX();
    int right2 = other.getX() + other.getWidth();
    int top2 = other.getY();
    int bottom2 = other.getY() + other.getHeight();

    if (right1 < left2 || right2 < left1 || bottom1 < top2 || bottom2 < top1) {
      intersection = null;
    } else {
      int x = Math.max(left1, left2);
      int width = Math.min(right1, right2) - x;
      int y = Math.max(top1, top2);
      int height = Math.min(bottom1, bottom2) - y;
      intersection = new Rect(x, y, width, height);
    }
    return intersection;
  }
Example #2
0
 /**
  * Create the positions array of the verticies for the rectangle.
  *
  * @param rect the position and size of the rectangle
  * @return the positions
  */
 @Nonnull
 private float[] createPositions(@Nonnull Rect rect) {
   return new float[] {
     rect.getX(),
     rect.getY(),
     rect.getX() + rect.getWidth(),
     rect.getY(),
     rect.getX() + rect.getWidth(),
     rect.getY() + rect.getHeight(),
     rect.getX(),
     rect.getY() + rect.getHeight()
   };
 }