forked from as3/as3-utils
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEllipse.as
More file actions
210 lines (177 loc) · 5.89 KB
/
Ellipse.as
File metadata and controls
210 lines (177 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
CASA Lib for ActionScript 3.0
Copyright (c) 2010, Aaron Clinger & Contributors of CASA Lib
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Neither the name of the CASA Lib nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package utils.geom
{
import flash.geom.Point;
/**
Stores position and size of an ellipse (circle or oval).
@author Aaron Clinger
@author Mike Creighton
@version 04/13/08
*/
public class Ellipse
{
protected var _x:Number;
protected var _y:Number;
protected var _width:Number;
protected var _height:Number;
/**
Creates new Ellipse object.
@param x: The horizontal position of the ellipse.
@param y: The vertical position of the ellipse.
@param width: Width of the ellipse at its widest horizontal point.
@param height: Height of the ellipse at its tallest point.
*/
public function Ellipse(x:Number, y:Number, width:Number, height:Number)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
/**
The horizontal coordinate of the point.
*/
public function get x():Number
{
return this._x;
}
public function set x(xPos:Number):void
{
this._x = xPos;
}
/**
The vertical coordinate of the point.
*/
public function get y():Number
{
return this._y;
}
public function set y(yPos:Number):void
{
this._y = yPos;
}
/**
The width of the ellipse.
*/
public function get width():Number
{
return this._width;
}
public function set width(width:Number):void
{
this._width = width;
}
/**
The height of the rectangle.
*/
public function get height():Number
{
return this._height;
}
public function set height(height:Number):void
{
this._height = height;
}
/**
The center of the ellipse.
*/
public function get center():Point
{
return new Point(this.x + this.width * 0.5, this.y + this.height * 0.5);
}
public function set center(c:Point):void
{
this.x = c.x - this.width * 0.5;
this.y = c.y - this.height * 0.5;
}
/**
The size of the ellipse, expressed as a Point object with the values of the width and height properties.
*/
public function get size():Point
{
return new Point(this.width, this.height);
}
/**
The circumference of the ellipse.
@usageNote Calculating the circumference of an ellipse is difficult; this is an approximation but should be fine for most cases.
*/
public function get perimeter():Number
{
return (Math.sqrt(.5 * (Math.pow(this.width, 2) + Math.pow(this.height, 2))) * Math.PI * 2) * 0.5;
}
/**
The area of the ellipse.
*/
public function get area():Number
{
return Math.PI * (this.width * 0.5) * (this.height * 0.5);
}
/**
Finds the <code>x</code>, <code>y</code> position of the degree along the circumference of the ellipse.
@param degree: Number representing a degree on the ellipse.
@return A Point object.
@usageNote <code>degree</code> can be over 360 or even negitive numbers; minding <code>0 = 360 = 720</code>, <code>540 = 180</code>, <code>-90 = 270</code>, etc.
*/
public function getPointOfDegree(degree:Number):Point
{
var radian:Number = (degree - 90) * (Math.PI / 180);
var xRadius:Number = this.width * 0.5;
var yRadius:Number = this.height * 0.5;
return new Point(this.x + xRadius + Math.cos(radian) * xRadius, this.y + yRadius + Math.sin(radian) * yRadius);
}
/**
Finds if point is contained inside the ellipse perimeter.
@param point: A Point object.
@return Returns <code>true</code> if shape's area contains point; otherwise <code>false</code>.
*/
public function containsPoint(point:Point):Boolean
{
var xRadius:Number = this.width * 0.5;
var yRadius:Number = this.height * 0.5;
var xTar:Number = point.x - this.x - xRadius;
var yTar:Number = point.y - this.y - yRadius;
return Math.pow(xTar / xRadius, 2) + Math.pow(yTar / yRadius, 2) <= 1;
}
/**
Determines if the Ellipse specified in the <code>ellipse</code> parameter is equal to this Ellipse object.
@param ellipse: An Ellipse object.
@return Returns <code>true</code> if object is equal to this Ellipse; otherwise <code>false</code>.
*/
public function equals(ellipse:Ellipse):Boolean
{
return this.x == ellipse.x && this.y == ellipse.y && this.width == ellipse.width && this.height == ellipse.height;
}
/**
@return A new Ellipse object with the same values as this Ellipse.
*/
public function clone():Ellipse
{
return new Ellipse(this.x, this.y, this.width, this.height);
}
}
}