-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASPointExtensions.hx
430 lines (370 loc) · 10.2 KB
/
ASPointExtensions.hx
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package ;
/*
* Some of the functions were based on Chipmunk's cpVect.h.
*/
/**
@file
Point extensions based on Chipmunk's cpVect file.
These extensions work both with Point and cpVect.
The "ccp" prefix means: "CoCos2d Point"
Examples:
- ccpAdd( ccp(1,1), ccp(2,2) ); // preferred cocos2d way
- ccpAdd( PointMake(1,1), PointMake(2,2) ); // also ok but more verbose
- cpvadd( cpv(1,1), cpv(2,2) ); // way of the chipmunk
- ccpAdd( cpv(1,1), cpv(2,2) ); // mixing chipmunk and cocos2d (avoid)
- cpvadd( PointMake(1,1), PointMake(2,2) ); // mixing chipmunk and CG (avoid)
*/
import CGAffineTransform;
import flash.geom.Point;
import ASSize;
typedef LineIntersection = {
var S:Float;
var T:Float;
var bool:Bool;
}
class ASPointExtensions {
inline static var kPointEpsilon = 0.0;
/** Returns opposite of point.
@return Point
@since v0.7.2
*/
public static function neg(v:Point) :Point
{
return new Point (-v.x, -v.y);
}
/** Calculates sum of two points.
@return Point
@since v0.7.2
*/
public static function add(v1:Point, v2:Point) :Point
{
return new Point (v1.x + v2.x, v1.y + v2.y);
}
/** Calculates difference of two points.
@return Point
@since v0.7.2
*/
public static function sub(v1:Point, v2:Point) :Point
{
return new Point (v1.x - v2.x, v1.y - v2.y);
}
/** Returns point multiplied by given factor.
@return Point
@since v0.7.2
*/
public static function mult(v:Point, s:Float) :Point
{
return new Point (v.x*s, v.y*s);
}
/** Multiplies a nd b components, a.x*b.x, a.y*b.y
@returns a component-wise multiplication
@since v0.99.1
*/
public static function compMult(a:Point, b:Point) :Point
{
return new Point (a.x * b.x, a.y * b.y);
}
/** Calculates midpoint between two points.
@return Point
@since v0.7.2
*/
public static function midpoint(v1:Point, v2:Point) :Point
{
return mult (add (v1, v2), 0.5);
}
/** Calculates dot product of two points.
@return CGFloat
@since v0.7.2
*/
public static function dot(v1:Point, v2:Point) :Float
{
return v1.x*v2.x + v1.y*v2.y;
}
/** Calculates cross product of two points.
@return CGFloat
@since v0.7.2
*/
public static function cross(v1:Point, v2:Point) :Float
{
return v1.x*v2.y - v1.y*v2.x;
}
/** Calculates perpendicular of v, rotated 90 degrees counter-clockwise -- cross(v, perp(v)) >= 0
@return Point
@since v0.7.2
*/
public static function perp(v:Point) :Point
{
return new Point (-v.y, v.x);
}
/** Calculates perpendicular of v, rotated 90 degrees clockwise -- cross(v, rperp(v)) <= 0
@return Point
@since v0.7.2
*/
public static function rperp(v:Point) :Point
{
return new Point (v.y, -v.x);
}
/** Calculates the projection of v1 over v2.
@return Point
@since v0.7.2
*/
public static function project(v1:Point, v2:Point) :Point
{
return mult (v2, dot(v1,v2) / dot(v2,v2));
}
/** Rotates two points.
@return Point
@since v0.7.2
*/
public static function rotate (v1:Point, v2:Point) :Point {
return new Point (v1.x*v2.x - v1.y*v2.y, v1.x*v2.y + v1.y*v2.x);
}
/** Unrotates two points.
@return Point
@since v0.7.2
*/
public static function unrotate (v1:Point, v2:Point) :Point {
return new Point (v1.x*v2.x + v1.y*v2.y, v1.y*v2.x - v1.x*v2.y);
}
/** Calculates the square length of a Point (not calling sqrt() )
@return CGFloat
@since v0.7.2
*/
public static function lengthSQ(v:Point) :Float
{
return dot(v, v);
return Math.sqrt ( v.x+v.y );
}
/** Calculates distance between point an origin
@return CGFloat
@since v0.7.2
*/
inline public static function length (v:Point) :Float
{
return Math.sqrt ( lengthSQ(v) );
}
/** Calculates the distance between two points
@return CGFloat
@since v0.7.2
*/
inline public static function distance(v1:Point, v2:Point) :Float
{
return length(sub(v1, v2));
}
/** Returns point multiplied to a length of 1.
@return Point
@since v0.7.2
*/
inline public static function normalize(v:Point) :Point
{
return mult (v, 1.0/length(v));
}
/** Converts radians to a normalized vector.
@return Point
@since v0.7.2
*/
public static function forAngle(a:Float) :Point
{
return new Point (Math.cos(a), Math.sin(a));
}
/** Converts a vector to radians.
@return CGFloat
@since v0.7.2
*/
public static function toAngle(v:Point) :Float
{
return Math.atan2 (v.y, v.x);
}
/** Linear Interpolation between two points a and b
@returns
alpha == 0 ? a
alpha == 1 ? b
otherwise a value between a..b
@since v0.99.1
*/
public static function lerp(a:Point, b:Point, alpha:Float) :Point
{
return add (mult (a, 1.0 - alpha), mult (b, alpha));
}
/** Clamp a value between from and to.
@since v0.99.1
*/
public static function clampf(value:Float, min_inclusive:Float, max_inclusive:Float) :Float
{
if (min_inclusive > max_inclusive) {
/*var obj = ASMacros.ASSWAP (min_inclusive, max_inclusive);
min_inclusive = obj.x;
max_inclusive = obj.y;*/
}
return value < min_inclusive ? min_inclusive : value < max_inclusive? value : max_inclusive;
}
/** Clamp a point between from and to.
@since v0.99.1
*/
public static function clamp(p:Point, min_inclusive:Point, max_inclusive:Point) :Point
{
return new Point (clampf(p.x,min_inclusive.x,max_inclusive.x), clampf(p.y, min_inclusive.y, max_inclusive.y));
}
/** Quickly convert CGSize to a Point
@since v0.99.1
*/
public static function fromSize(s:ASSize) :Point
{
return new Point (s.width, s.height);
}
/** Run a math operation function on each point component
* absf, fllorf, ceilf, roundf
* any function that has the signature: float func(float);
* For example: let's try to take the floor of x,y
* ccpCompOp(p,floorf);
@since v0.99.1
*/
public static function compOp(p:Point, opFunc:Dynamic) :Point
{
return new Point (opFunc(p.x), opFunc(p.y));
}
/** @returns if points have fuzzy equality which means equal with some degree of variance.
@since v0.99.1
*/
public static function fuzzyEqual(a:Point, b:Point, v:Float) :Bool
{
if(a.x - v <= b.x && b.x <= a.x + v)
if(a.y - v <= b.y && b.y <= a.y + v)
return true;
return false;
}
/** @returns the signed angle in radians between two vector directions
@since v0.99.1
*/
public static function angleSigned(a:Point, b:Point) :Float
{
var a2:Point = normalize(a);
var b2:Point = normalize(b);
var angle:Float = Math.atan2 (a2.x * b2.y - a2.y * b2.x, dot(a2, b2));
if( Math.abs(angle) < kPointEpsilon ) return 0.0;
return angle;
}
/** @returns the angle in radians between two vector directions
@since v0.99.1
*/
public static function angle (a:Point, b:Point) :Float
{
var ang :Float = Math.acos ( dot (normalize(a), normalize(b)) );
if( Math.abs(ang) < kPointEpsilon ) return 0.0;
return ang;
}
/** Rotates a point counter clockwise by the angle around a pivot
@param v is the point to rotate
@param pivot is the pivot, naturally
@param angle is the angle of rotation cw in radians
@returns the rotated point
@since v0.99.1
*/
public static function rotateByAngle(v:Point, pivot:Point, angle:Float) :Point
{
var r :Point = sub(v, pivot);
var cosa :Float = Math.cos(angle);
var sina :Float = Math.sin(angle);
var t :Float = r.x;
r.x = t*cosa - r.y*sina + pivot.x;
r.y = t*sina + r.y*cosa + pivot.y;
return r;
}
/** A general line-line intersection test
@param p1
is the startpoint for the first line P1 = (p1 - p2)
@param p2
is the endpoint for the first line P1 = (p1 - p2)
@param p3
is the startpoint for the second line P2 = (p3 - p4)
@param p4
is the endpoint for the second line P2 = (p3 - p4)
@param s
is the range for a hitpoint in P1 (pa = p1 + s*(p2 - p1))
@param t
is the range for a hitpoint in P3 (pa = p2 + t*(p4 - p3))
@return bool
indicating successful intersection of a line
note that to truly test intersection for segments we have to make
sure that s & t lie within [0..1] and for rays, make sure s & t > 0
the hit point is p3 + t * (p4 - p3);
the hit point also is p1 + s * (p2 - p1);
@since v0.99.1
*/
public static function lineIntersect (A:Point, B:Point, C:Point, D:Point, S:Float, T:Float) :LineIntersection
{
// FAIL: Line undefined
if ( (A.x==B.x && A.y==B.y) || (C.x==D.x && C.y==D.y) ) return {S:S, T:T, bool:false};
var BAx:Float = B.x - A.x;
var BAy:Float = B.y - A.y;
var DCx:Float = D.x - C.x;
var DCy:Float = D.y - C.y;
var ACx:Float = A.x - C.x;
var ACy:Float = A.y - C.y;
var denom:Float = DCy*BAx - DCx*BAy;
var b :Bool = false;
S = DCx*ACy - DCy*ACx;
T = BAx*ACy - BAy*ACx;
if (denom == 0) {
if (S == 0 || T == 0) {
// Lines incident
b = true;
}
else {
// Lines parallel and not incident
b = false;
}
}
else {
S = S / denom;
T = T / denom;
}
// Point of intersection
// P:Point;
// P.x = A.x + *S * (B.x - A.x);
// P.y = A.y + *S * (B.y - A.y);
return {S:S, T:T, bool:b};
}
/*
ccpSegmentIntersect returns YES if Segment A-B intersects with segment C-D
@since v1.0.0
*/
public static function segmentIntersect(A:Point, B:Point, C:Point, D:Point) :Bool
{
var S:Float = 0, T:Float = 0;
var ST :LineIntersection = lineIntersect (A, B, C, D, S, T);
S = ST.S;
T = ST.T;
if( ST.bool && (S >= 0.0 && S <= 1.0 && T >= 0.0 && T <= 1.0) )
return true;
return false;
}
/*
ccpIntersectPoint returns the intersection point of line A-B, C-D
@since v1.0.0
*/
public static function intersectPoint(A:Point, B:Point, C:Point, D:Point) :Point
{
var S:Float=0, T:Float=0;
var ST :LineIntersection = lineIntersect (A, B, C, D, S, T);
S = ST.S;
T = ST.T;
if( ST.bool ) {
// Point of intersection
return new Point (A.x + S * (B.x - A.x), A.y + S * (B.y - A.y));
}
return new Point (0,0);
}
public static function equalToPoint (point:Point, newPoint:Point) :Bool {
if (point.x == newPoint.x) if (point.y == newPoint.y) return true;
return false;
}
public static function applyAffineTransform (point:Point, t:CGAffineTransform) :Point {
return point;
}
public static function pointFromString (p:String) :Point
{
var arr = p.split("{").join("").split("}").join("").split(",");
return new Point ( Std.parseInt(arr[0]), Std.parseInt(arr[1]) );
}
}