1 /*******************************************************************************
\r
2 * Copyright 2011 See AUTHORS file.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
\r
5 * License. You may obtain a copy of the License at
\r
7 * http://www.apache.org/licenses/LICENSE-2.0
\r
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
\r
10 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
\r
11 * governing permissions and limitations under the License.
\r
12 ******************************************************************************/
\r
14 package com.badlogic.gdx.math;
\r
16 import java.io.Serializable;
\r
19 * Encapsulates a 2D rectangle defined by it's bottom corner point and its extends in x (width) and y (height).
\r
20 * @author badlogicgames@gmail.com
\r
23 public class Rectangle implements Serializable {
\r
24 private static final long serialVersionUID = 5733252015138115702L;
\r
26 public float width, height;
\r
29 * Constructs a new rectangle with all values set to zero
\r
31 public Rectangle () {
\r
36 * Constructs a new rectangle with the given corner point in the bottom left and dimensions.
\r
37 * @param x The corner point x-coordinate
\r
38 * @param y The corner point y-coordinate
\r
39 * @param width The width
\r
40 * @param height The height
\r
42 public Rectangle (float x, float y, float width, float height) {
\r
46 this.height = height;
\r
50 * Constructs a rectangle based on the given rectangle
\r
51 * @param rect The rectangle
\r
53 public Rectangle (Rectangle rect) {
\r
57 height = rect.height;
\r
61 * @return the x-coordinate of the bottom left corner
\r
63 public float getX () {
\r
68 * Sets the x-coordinate of the bottom left corner
\r
69 * @param x The x-coordinate
\r
71 public void setX (float x) {
\r
76 * @return the y-coordinate of the bottom left corner
\r
78 public float getY () {
\r
83 * Sets the y-coordinate of the bottom left corner
\r
84 * @param y The y-coordinate
\r
86 public void setY (float y) {
\r
93 public float getWidth () {
\r
98 * Sets the width of this rectangle
\r
99 * @param width The width
\r
101 public void setWidth (float width) {
\r
102 this.width = width;
\r
106 * @return the height
\r
108 public float getHeight () {
\r
113 * Sets the height of this rectangle
\r
114 * @param height The height
\r
116 public void setHeight (float height) {
\r
117 this.height = height;
\r
121 * @param rectangle the other {@link Rectangle}.
\r
122 * @return whether the other rectangle is contained in this rectangle.
\r
124 public boolean contains (Rectangle rectangle) {
\r
125 float xmin = rectangle.x;
\r
126 float xmax = xmin + rectangle.width;
\r
128 float ymin = rectangle.y;
\r
129 float ymax = ymin + rectangle.height;
\r
131 return ((xmin > x && xmin < x + width) || (xmax > x && xmax < x + width))
\r
132 && ((ymin > y && ymin < y + height) || (ymax > y && ymax < y + height));
\r
136 * @param rectangle the other {@link Rectangle}
\r
137 * @return whether this rectangle overlaps the other rectangle.
\r
139 public boolean overlaps(Rectangle rectangle) {
\r
140 return !(x > rectangle.x + rectangle.width ||
\r
141 x + width < rectangle.x ||
\r
142 y > rectangle.y + rectangle.height ||
\r
143 y + height < rectangle.y);
\r
146 public void set (int x, int y, int width, int height) {
\r
149 this.width = width;
\r
150 this.height = height;
\r