OSDN Git Service

343b92609cb523619831f56722151cdd6efa9831
[mikumikustudio/libgdx-mikumikustudio.git] / gdx / src / com / badlogic / gdx / math / Rectangle.java
1 /*******************************************************************************\r
2  * Copyright 2011 See AUTHORS file.\r
3  * \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
6  * \r
7  * http://www.apache.org/licenses/LICENSE-2.0\r
8  * \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
13 \r
14 package com.badlogic.gdx.math;\r
15 \r
16 import java.io.Serializable;\r
17 \r
18 /**\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
21  * \r
22  */\r
23 public class Rectangle implements Serializable {\r
24         private static final long serialVersionUID = 5733252015138115702L;\r
25         public float x, y;\r
26         public float width, height;\r
27 \r
28         /**\r
29          * Constructs a new rectangle with all values set to zero\r
30          */\r
31         public Rectangle () {\r
32 \r
33         }\r
34 \r
35         /**\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
41          */\r
42         public Rectangle (float x, float y, float width, float height) {\r
43                 this.x = x;\r
44                 this.y = y;\r
45                 this.width = width;\r
46                 this.height = height;\r
47         }\r
48 \r
49         /**\r
50          * Constructs a rectangle based on the given rectangle\r
51          * @param rect The rectangle\r
52          */\r
53         public Rectangle (Rectangle rect) {\r
54                 x = rect.x;\r
55                 y = rect.y;\r
56                 width = rect.width;\r
57                 height = rect.height;\r
58         }\r
59 \r
60         /**\r
61          * @return the x-coordinate of the bottom left corner\r
62          */\r
63         public float getX () {\r
64                 return x;\r
65         }\r
66 \r
67         /**\r
68          * Sets the x-coordinate of the bottom left corner\r
69          * @param x The x-coordinate\r
70          */\r
71         public void setX (float x) {\r
72                 this.x = x;\r
73         }\r
74 \r
75         /**\r
76          * @return the y-coordinate of the bottom left corner\r
77          */\r
78         public float getY () {\r
79                 return y;\r
80         }\r
81 \r
82         /**\r
83          * Sets the y-coordinate of the bottom left corner\r
84          * @param y The y-coordinate\r
85          */\r
86         public void setY (float y) {\r
87                 this.y = y;\r
88         }\r
89 \r
90         /**\r
91          * @return the width\r
92          */\r
93         public float getWidth () {\r
94                 return width;\r
95         }\r
96 \r
97         /**\r
98          * Sets the width of this rectangle\r
99          * @param width The width\r
100          */\r
101         public void setWidth (float width) {\r
102                 this.width = width;\r
103         }\r
104 \r
105         /**\r
106          * @return the height\r
107          */\r
108         public float getHeight () {\r
109                 return height;\r
110         }\r
111 \r
112         /**\r
113          * Sets the height of this rectangle\r
114          * @param height The height\r
115          */\r
116         public void setHeight (float height) {\r
117                 this.height = height;\r
118         }\r
119 \r
120         /**\r
121          * @param rectangle the other {@link Rectangle}.\r
122          * @return whether the other rectangle is contained in this rectangle.\r
123          */\r
124         public boolean contains (Rectangle rectangle) {\r
125                 float xmin = rectangle.x;\r
126                 float xmax = xmin + rectangle.width;\r
127 \r
128                 float ymin = rectangle.y;\r
129                 float ymax = ymin + rectangle.height;\r
130 \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
133         }\r
134         \r
135         /**\r
136          * @param rectangle the other {@link Rectangle}\r
137          * @return whether this rectangle overlaps the other rectangle.\r
138          */\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
144         }\r
145 }\r