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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
/* <license>
* This file is part of the dis-Emi-A HaXe Library. Copyright © edA-qa mort-ora-y
* For full copyright and license information please refer to doc/license.txt.
* </license>
*/
package mathx;
/**
* FUNC1 is used to define all single parameter Pt functions, sicne they can
* take both a point and an XY parameter instead
*/
define(`FUNC1',`
INLINE public function $1`'( b : Point2 ) : $2
{
define(`BX',b.x)
define(`BY',b.y)
$3
}
INLINE public function $1`'XY( bx : Float, by : Float ) : $2
{
define(`BX',bx)
define(`BY',by)
$3
}
')
define(`MFUNC1',`
MEDINLINE public function $1`'( b : Point2 ) : $2
{
define(`BX',b.x)
define(`BY',b.y)
$3
}
MEDINLINE public function $1`'XY( bx : Float, by : Float ) : $2
{
define(`BX',bx)
define(`BY',by)
$3
}
')
/**
* This is a 2-space vector class. It behaves as either a directional vector
* or as a point.
*
* It lacks many functions at the moment, as they are being added on
* a as-needed basis.
*
* It is intended that Point2 is an immutable class, but the optimization
* required for Looping prevents this... so if you need to be sure you
* have a proper copy, use the clone() function.
*/
class Point2
{
public var x : Float;
public var y : Float;
/**
* Do not use the Ctor directly, rather use the consturction functions,
* such as "at". This will enable an easier optimization plan in the
* future, and allows for less ambiguity...?
*/
INLINE private function new( ax : Float, ay : Float )
{
x = ax;
y = ay;
}
////////////////////////////////////////////////////////////////////////////////////////
// Construction functions
/*INLINE*/ static public function at( ax : Float, ay : Float) : Point2
{
return new Point2( ax, ay );
}
static public function polar( radius : Float, angle : Float ) : Point2
{
return new Point2(
radius * Math.cos( angle ),
radius * Math.sin( angle )
);
}
static public function parsed( str : String ) : Point2
{
var re = ~/\(([-0-9.]+),([-0-9.]+)\)/;
if( re.match( str ) )
return new Point2(
Std.parseFloat( re.matched(1) ),
Std.parseFloat( re.matched(2) )
);
//try polar coordinates (support proper degree symbol or degree)
var re = ~/\(([-0-9.]+),([-0-9.]+)(°|d)\)/;
if( re.match( str ) )
return Point2.polar(
Std.parseFloat( re.matched(1) ),
Std.parseFloat( re.matched(2) )
);
throw new TypeParseError( "Point2", str );
}
/**
* A convenience method to create an array of poitns from a static
* array structure.
*/
static public function list( pts : Array<Array<Float>> ) : Array<Point2>
{
var ret = new Array<Point2>();
for( pt in pts )
{
if( pt.length == 0 )
ret.push( null );
else
ret.push( Point2.at( pt[0], pt[1] ) );
}
return ret;
}
#if flash
static public function from( pt : flash.geom.Point ) : Point2
{
return new Point2( pt.x, pt.y );
}
public function toFlash( ) : flash.geom.Point
{
return new flash.geom.Point( x, y );
}
#end
//creates from a given anlge in radians
INLINE static public function fromAngle( a : Float ) : Point2
{
return new Point2( Math.cos( a ), Math.sin( a ) );
}
//HAXE: cannot be INLINE due a defect in haxe relating to interfaces/typedefs not matching then
public function clone() : Point2
{
return new Point2( x, y );
}
INLINE public function equals( o : Point2 ) : Bool
{
return MathUtil.equals( o.x, x ) && MathUtil.equals( o.y, y );
}
////////////////////////////////////////////////////////////////////////////////////////
// Algebraic functions
FUNC1(sub,Point2,`
return new Point2( x - BX, y - BY );
')
//similar to sub, but with opposite argument orders
FUNC1(vectorTo,Point2,`
return new Point2( BX - x, BY - y );
')
FUNC1(add,Point2,`
return new Point2( x + BX, y + BY );
')
INLINE public function mul( c : Float ) : Point2
{
return new Point2( x * c, y * c );
}
INLINE public function div( c : Float ) : Point2
{
return new Point2( x / c, y / c );
}
FUNC1(midTo,Point2,`
return new Point2( (BX -x)/2+x, (BY-y)/2+y );
')
FUNC1(scale,Point2,`
return new Point2( x * BX, y * BY );
')
FUNC1(divScale,Point2,`
return new Point2( x / BX, y / BY );
')
/**
* Gets the point between this point and the next and the given factor
* between the two.
*/
INLINE public function factorTo( b : Point2, factor : Float ) : Point2
{
return new Point2(
x + (b.x - x ) * factor,
y + (b.y - y ) * factor
);
}
/**
* The inverse of factorTo. This assumes the point is on the line
* segment.
*
* @return [out] f : a.factorTo( b, f ) = this
* If the points are equal the result will be 1.0 rather than NaN. The logic
* being this function is intended to say how far (As a factor) to point
* B from A this lies. Thus when A == B we are at b, thus 1.0.
*/
MEDINLINE public function factorOf( a : Point2, b : Point2 ) : Float
{
return MathUtil.isZero( b.x - a.x )
? (MathUtil.isZero( b.y - a.y ) ? 1 : ( y - a.y ) / ( b.y - a.y )) //handle both zero case
: ( x - a.x ) / ( b.x - a.x );
}
////////////////////////////////////////////////////////////////////////////////////////
// Geometric functions
INLINE public function unit() : Point2
{
var len = Math.sqrt( x*x + y*y );
return new Point2( x/len, y/len );
}
INLINE public function unitMul( f : Float ) : Point2
{
var len = Math.sqrt( x*x + y*y ) / f;
return new Point2( x/len, y/len );
}
INLINE public function norm() : Float
{
return Math.sqrt( x*x + y*y );
}
INLINE public function normSqr() : Float
{
return x*x + y*y;
}
FUNC1(distanceToSqr,Float,`
return (BX-x)*(BX-x) + (BY-y)*(BY-y);
')
FUNC1(distanceTo,Float,`
return Math.sqrt( (BX-x)*(BX-x) + (BY-y)*(BY-y) );//sqrt(distanceToSqr)
')
/**
* @return [out] a unit vector from here to point b, or 0,0 if the points
* are the same
*/
MFUNC1(unitVectorTo,Point2,`
var ret = new Point2( BX-x, BY-y );
var norm = Math.sqrt( ret.x*ret.x + ret.y*ret.y );
if( MathUtil.isZero( norm ) )
{
ret.x = 0;
ret.y = 0;
}
else
{
ret.x /= norm;
ret.y /= norm;
}
return ret;
')
/**
* How far away from an intersection we are (from rounded numbers)
*/
MEDINLINE public function offsetNorm() : Float
{
var xoff = Math.round( x ) - x;
var yoff = Math.round( y ) - y;
return Math.sqrt( xoff*xoff + yoff*yoff );
}
/**
* Obtains the angle of the vector from the origin to this point relative
* to the positive xAxis.
* @return [out] angle in range = [ -M_PI, M_PI ]
*/
INLINE public function xAxisAngle() : Float
{
return Geometry.xAxisAngleV( this );
}
/**
* Reverses the direction of this vector.
*/
INLINE public function reverse() : Point2
{
return new Point2( -x, -y );
}
INLINE public function ratio() : Float
{
return x / y;
}
INLINE public function rotate( rad : Float ) : Point2
{
return fromAngle( xAxisAngle() + rad );
}
////////////////////////////////////////////////////////////////////////////////////////
// Closest point functions
INLINE public function floor() : Point2
{
return new Point2( Math.floor( x ), Math.floor( y ) );
}
INLINE public function ceil() : Point2
{
return new Point2( Math.ceil( x ), Math.ceil( y ) );
}
INLINE public function round() : Point2
{
return new Point2( Math.round( x ), Math.round( y ) );
}
/**
* Limits the x,y component to within the range (inclusive). X and Y
* are limited independently.
*/
INLINE public function constrain( low : Point2, high : Point2 ) : Point2
{
return new Point2(
Math.min( high.x, Math.max( low.x, x ) ),
Math.min( high.y, Math.max( low.y, y ) )
);
}
////////////////////////////////////////////////////////////////////////////////////////
// Demotion functions
INLINE public function roundDemote() : MatPoint
{
return MatPoint.at( Math.round( x ), Math.round( y ) );
}
INLINE public function ceilDemote() : MatPoint
{
return MatPoint.at( Math.ceil( x ), Math.ceil( y ) );
}
INLINE public function floorDemote() : MatPoint
{
return MatPoint.at( Math.floor( x ), Math.floor( y ) );
}
////////////////////////////////////////////////////////////////////////////////////////
// Formatters
public function toString() : String
{
return "(" + x + "," + y + ")";
}
//////////////////////////////////////////////////////////////////////////////////////
// Related convenience methods
static public function arrFlipX( pts : Array<Point2> ) : Array<Point2>
{
var ret = new Array<Point2>();
for( pt in pts )
ret.push( new Point2( -pt.x, pt.y ) );
return ret;
}
}
|