-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPoint.ts
235 lines (214 loc) · 6.48 KB
/
Point.ts
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* The Point object represents a location in a two-dimensional coordinate
* system, where <i>x</i> represents the horizontal axis and <i>y</i>
* represents the vertical axis.
*
* <p>The following code creates a point at(0,0):</p>
*
* <p>Methods and properties of the following classes use Point objects:</p>
*
* <ul>
* <li>BitmapData</li>
* <li>DisplayObject</li>
* <li>DisplayObjectContainer</li>
* <li>DisplacementMapFilter</li>
* <li>NativeWindow</li>
* <li>Matrix</li>
* <li>Rectangle</li>
* </ul>
*
* <p>You can use the <code>new Point()</code> constructor to create a Point
* object.</p>
*/
export class Point
{
public _rawData:Float32Array = new Float32Array(2);
/**
* The horizontal coordinate of the point. The default value is 0.
*/
public get x():number
{
return this._rawData[0];
}
public set x(value:number)
{
this._rawData[0] = value;
}
/**
* The vertical coordinate of the point. The default value is 0.
*/
public get y():number
{
return this._rawData[1];
}
public set y(value:number)
{
this._rawData[1] = value;
}
/**
* The length of the line segment from(0,0) to this point.
*/
public get length():number
{
return Math.sqrt(this._rawData[0]*this._rawData[0] + this._rawData[1]*this._rawData[1]);
}
/**
* Creates a new point. If you pass no parameters to this method, a point is
* created at(0,0).
*
* @param x The horizontal coordinate.
* @param y The vertical coordinate.
*/
constructor(x:number = 0, y:number = 0)
{
this._rawData[0] = x;
this._rawData[1] = y;
}
/**
* Adds the coordinates of another point to the coordinates of this point to
* create a new point.
*
* @param v The point to be added.
* @return The new point.
*/
public add(v:Point):Point
{
return new Point(this._rawData[0] + v._rawData[0], this._rawData[1] + v._rawData[1]);
}
/**
* Creates a copy of this Point object.
*
* @return The new Point object.
*/
public clone():Point
{
return new Point(this.x, this.y);
}
public copyFrom(sourcePoint:Point):void
{
this._rawData[0] = sourcePoint._rawData[0];
this._rawData[1] = sourcePoint._rawData[1];
}
/**
* Determines whether two points are equal. Two points are equal if they have
* the same <i>x</i> and <i>y</i> values.
*
* @param toCompare The point to be compared.
* @return A value of <code>true</code> if the object is equal to this Point
* object; <code>false</code> if it is not equal.
*/
public equals(toCompare:Point):boolean
{
return (this._rawData[0] === toCompare._rawData[0] && this._rawData[1] == toCompare._rawData[1]);
}
/**
* Scales the line segment between(0,0) and the current point to a set
* length.
*
* @param thickness The scaling value. For example, if the current point is
* (0,5), and you normalize it to 1, the point returned is
* at(0,1).
*/
public normalize(thickness:number = 1):void
{
var len:number = this.length;
if ((this.x !== 0 || this.y !== 0) && len) {
var relativeThickness:number = thickness/len;
this._rawData[0] *= relativeThickness;
this._rawData[1] *= relativeThickness;
}
}
/**
* Offsets the Point object by the specified amount. The value of
* <code>dx</code> is added to the original value of <i>x</i> to create the
* new <i>x</i> value. The value of <code>dy</code> is added to the original
* value of <i>y</i> to create the new <i>y</i> value.
*
* @param dx The amount by which to offset the horizontal coordinate,
* <i>x</i>.
* @param dy The amount by which to offset the vertical coordinate, <i>y</i>.
*/
public offset(dx:number, dy:number):void
{
this._rawData[0] += dx;
this._rawData[1] += dy;
}
public setTo(xa:number, ya:number):void
{
this._rawData[0] = xa;
this._rawData[1] = ya;
}
/**
* Subtracts the coordinates of another point from the coordinates of this
* point to create a new point.
*
* @param v The point to be subtracted.
* @return The new point.
*/
public subtract(v:Point):Point
{
return new Point(this.x - v.x, this.y - v.y);
}
/**
* Returns a string that contains the values of the <i>x</i> and <i>y</i>
* coordinates. The string has the form <code>"(x=<i>x</i>,
* y=<i>y</i>)"</code>, so calling the <code>toString()</code> method for a
* point at 23,17 would return <code>"(x=23, y=17)"</code>.
*
* @return The string representation of the coordinates.
*/
public toString():string
{
return "[Point] (x=" + this.x + ", y=" + this.y + ")";
}
/**
* Returns the distance between <code>pt1</code> and <code>pt2</code>.
*
* @param pt1 The first point.
* @param pt2 The second point.
* @return The distance between the first and second points.
*/
public static distance(pt1:Point, pt2:Point):number
{
var dx:number = pt2.x - pt1.x;
var dy:number = pt2.y - pt1.y;
return (dx === 0) ? Math.abs(dy) : (dy === 0) ? Math.abs(dx) : Math.sqrt(dx*dx + dy*dy);
}
/**
* Determines a point between two specified points. The parameter
* <code>f</code> determines where the new interpolated point is located
* relative to the two end points specified by parameters <code>pt1</code>
* and <code>pt2</code>. The closer the value of the parameter <code>f</code>
* is to <code>1.0</code>, the closer the interpolated point is to the first
* point(parameter <code>pt1</code>). The closer the value of the parameter
* <code>f</code> is to 0, the closer the interpolated point is to the second
* point(parameter <code>pt2</code>).
*
* @param pt1 The first point.
* @param pt2 The second point.
* @param f The level of interpolation between the two points. Indicates
* where the new point will be, along the line between
* <code>pt1</code> and <code>pt2</code>. If <code>f</code>=1,
* <code>pt1</code> is returned; if <code>f</code>=0,
* <code>pt2</code> is returned.
* @return The new, interpolated point.
*/
public static interpolate(pt1:Point, pt2:Point, f:number):Point
{
var f1: number = 1 - f;
var raw1:Float32Array = pt1._rawData;
var raw2:Float32Array = pt2._rawData;
return new Point(raw1[0]*f + raw2[0]*f1, raw1[1]*f + raw2[1]*f1);
}
/**
* Converts a pair of polar coordinates to a Cartesian point coordinate.
*
* @param len The length coordinate of the polar pair.
* @param angle The angle, in radians, of the polar pair.
* @return The Cartesian point.
*/
public static polar(len:number, angle:number):Point
{
return new Point(len*Math.cos(angle), len*Math.sin(angle));
}
}