OSDN Git Service

Ellipse. Added constructor and setters
[mikumikustudio/libgdx-mikumikustudio.git] / gdx / src / com / badlogic / gdx / math / Ellipse.java
1 \r
2 package com.badlogic.gdx.math;\r
3 \r
4 import java.io.Serializable;\r
5 \r
6 import com.badlogic.gdx.math.Vector2;\r
7 \r
8 /** A convenient 2D ellipse class, based on the circle class\r
9  * @author tonyp7 */\r
10 public class Ellipse implements Serializable {\r
11 \r
12         public float x, y;\r
13         public float width, height;\r
14 \r
15         private static final long serialVersionUID = 7381533206532032099L;\r
16 \r
17         /** Construct a new ellipse with all values set to zero */\r
18         public Ellipse () {\r
19 \r
20         }\r
21 \r
22         public Ellipse (Ellipse ellipse) {\r
23                 this.x = ellipse.x;\r
24                 this.y = ellipse.y;\r
25                 this.width = ellipse.width;\r
26                 this.height = ellipse.height;\r
27         }\r
28 \r
29         public Ellipse (float x, float y, float width, float height) {\r
30                 this.x = x;\r
31                 this.y = y;\r
32                 this.width = width;\r
33                 this.height = height;\r
34         }\r
35 \r
36         public Ellipse (Vector2 position, float width, float height) {\r
37                 this.x = position.x;\r
38                 this.y = position.y;\r
39                 this.width = width;\r
40                 this.height = height;\r
41         }\r
42 \r
43         public boolean contains (float x, float y) {\r
44                 x = x - this.x;\r
45                 y = y - this.y;\r
46 \r
47                 return (x * x) / (width * 0.5f * width * 0.5f) + (y * y) / (height * 0.5f * height * 0.5f) <= 1.0f;\r
48         }\r
49 \r
50         public boolean contains (Vector2 point) {\r
51                 return contains(point.x, point.y);\r
52         }\r
53 \r
54         public void set (float x, float y, float width, float height) {\r
55                 this.x = x;\r
56                 this.y = y;\r
57                 this.width = width;\r
58                 this.height = height;\r
59         }\r
60 \r
61         public void set (Ellipse ellipse) {\r
62                 x = ellipse.x;\r
63                 y = ellipse.y;\r
64                 width = ellipse.width;\r
65                 height = ellipse.height;\r
66         }\r
67 \r
68         /** Sets the x and y-coordinates of ellipse center from vector\r
69          * @param position The position vector \r
70          * @return this ellipse for chaining */\r
71         public Ellipse setPosition (Vector2 position) {\r
72                 this.x = position.x;\r
73                 this.y = position.y;\r
74 \r
75                 return this;\r
76         }\r
77 \r
78         /** Sets the x and y-coordinates of ellipse center\r
79          * @param x The x-coordinate\r
80          * @param y The y-coordinate \r
81          * @return this ellipse for chaining */\r
82         public Ellipse setPosition (float x, float y) {\r
83                 this.x = x;\r
84                 this.y = y;\r
85 \r
86                 return this;\r
87         }\r
88 \r
89         /** Sets the width and height of this ellipse\r
90          * @param width The width\r
91          * @param height The height \r
92          * @return this ellipse for chaining */\r
93         public Ellipse setSize (float width, float height) {\r
94                 this.width = width;\r
95                 this.height = height;\r
96 \r
97                 return this;\r
98         }\r
99 }\r