diff options
Diffstat (limited to 'libs/cocos2d/Support/CGPointExtension.m')
-rwxr-xr-x | libs/cocos2d/Support/CGPointExtension.m | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/libs/cocos2d/Support/CGPointExtension.m b/libs/cocos2d/Support/CGPointExtension.m new file mode 100755 index 0000000..b06859d --- /dev/null +++ b/libs/cocos2d/Support/CGPointExtension.m | |||
@@ -0,0 +1,196 @@ | |||
1 | /* cocos2d for iPhone | ||
2 | * http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 2007 Scott Lembcke | ||
5 | * | ||
6 | * Copyright (c) 2010 Lam Pham | ||
7 | * | ||
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
9 | * of this software and associated documentation files (the "Software"), to deal | ||
10 | * in the Software without restriction, including without limitation the rights | ||
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
12 | * copies of the Software, and to permit persons to whom the Software is | ||
13 | * furnished to do so, subject to the following conditions: | ||
14 | * | ||
15 | * The above copyright notice and this permission notice shall be included in | ||
16 | * all copies or substantial portions of the Software. | ||
17 | * | ||
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
24 | * SOFTWARE. | ||
25 | */ | ||
26 | |||
27 | #include "stdio.h" | ||
28 | #include "math.h" | ||
29 | |||
30 | #import "../ccMacros.h" // CC_SWAP | ||
31 | #include "CGPointExtension.h" | ||
32 | |||
33 | #define kCGPointEpsilon FLT_EPSILON | ||
34 | |||
35 | CGFloat | ||
36 | ccpLength(const CGPoint v) | ||
37 | { | ||
38 | return sqrtf(ccpLengthSQ(v)); | ||
39 | } | ||
40 | |||
41 | CGFloat | ||
42 | ccpDistance(const CGPoint v1, const CGPoint v2) | ||
43 | { | ||
44 | return ccpLength(ccpSub(v1, v2)); | ||
45 | } | ||
46 | |||
47 | CGPoint | ||
48 | ccpNormalize(const CGPoint v) | ||
49 | { | ||
50 | return ccpMult(v, 1.0f/ccpLength(v)); | ||
51 | } | ||
52 | |||
53 | CGPoint | ||
54 | ccpForAngle(const CGFloat a) | ||
55 | { | ||
56 | return ccp(cosf(a), sinf(a)); | ||
57 | } | ||
58 | |||
59 | CGFloat | ||
60 | ccpToAngle(const CGPoint v) | ||
61 | { | ||
62 | return atan2f(v.y, v.x); | ||
63 | } | ||
64 | |||
65 | CGPoint ccpLerp(CGPoint a, CGPoint b, float alpha) | ||
66 | { | ||
67 | return ccpAdd(ccpMult(a, 1.f - alpha), ccpMult(b, alpha)); | ||
68 | } | ||
69 | |||
70 | float clampf(float value, float min_inclusive, float max_inclusive) | ||
71 | { | ||
72 | if (min_inclusive > max_inclusive) { | ||
73 | CC_SWAP(min_inclusive,max_inclusive); | ||
74 | } | ||
75 | return value < min_inclusive ? min_inclusive : value < max_inclusive? value : max_inclusive; | ||
76 | } | ||
77 | |||
78 | CGPoint ccpClamp(CGPoint p, CGPoint min_inclusive, CGPoint max_inclusive) | ||
79 | { | ||
80 | return ccp(clampf(p.x,min_inclusive.x,max_inclusive.x), clampf(p.y, min_inclusive.y, max_inclusive.y)); | ||
81 | } | ||
82 | |||
83 | CGPoint ccpFromSize(CGSize s) | ||
84 | { | ||
85 | return ccp(s.width, s.height); | ||
86 | } | ||
87 | |||
88 | CGPoint ccpCompOp(CGPoint p, float (*opFunc)(float)) | ||
89 | { | ||
90 | return ccp(opFunc(p.x), opFunc(p.y)); | ||
91 | } | ||
92 | |||
93 | BOOL ccpFuzzyEqual(CGPoint a, CGPoint b, float var) | ||
94 | { | ||
95 | if(a.x - var <= b.x && b.x <= a.x + var) | ||
96 | if(a.y - var <= b.y && b.y <= a.y + var) | ||
97 | return true; | ||
98 | return false; | ||
99 | } | ||
100 | |||
101 | CGPoint ccpCompMult(CGPoint a, CGPoint b) | ||
102 | { | ||
103 | return ccp(a.x * b.x, a.y * b.y); | ||
104 | } | ||
105 | |||
106 | float ccpAngleSigned(CGPoint a, CGPoint b) | ||
107 | { | ||
108 | CGPoint a2 = ccpNormalize(a); | ||
109 | CGPoint b2 = ccpNormalize(b); | ||
110 | float angle = atan2f(a2.x * b2.y - a2.y * b2.x, ccpDot(a2, b2)); | ||
111 | if( fabs(angle) < kCGPointEpsilon ) return 0.f; | ||
112 | return angle; | ||
113 | } | ||
114 | |||
115 | CGPoint ccpRotateByAngle(CGPoint v, CGPoint pivot, float angle) | ||
116 | { | ||
117 | CGPoint r = ccpSub(v, pivot); | ||
118 | float cosa = cosf(angle), sina = sinf(angle); | ||
119 | float t = r.x; | ||
120 | r.x = t*cosa - r.y*sina + pivot.x; | ||
121 | r.y = t*sina + r.y*cosa + pivot.y; | ||
122 | return r; | ||
123 | } | ||
124 | |||
125 | |||
126 | BOOL ccpSegmentIntersect(CGPoint A, CGPoint B, CGPoint C, CGPoint D) | ||
127 | { | ||
128 | float S, T; | ||
129 | |||
130 | if( ccpLineIntersect(A, B, C, D, &S, &T ) | ||
131 | && (S >= 0.0f && S <= 1.0f && T >= 0.0f && T <= 1.0f) ) | ||
132 | return YES; | ||
133 | |||
134 | return NO; | ||
135 | } | ||
136 | |||
137 | CGPoint ccpIntersectPoint(CGPoint A, CGPoint B, CGPoint C, CGPoint D) | ||
138 | { | ||
139 | float S, T; | ||
140 | |||
141 | if( ccpLineIntersect(A, B, C, D, &S, &T) ) { | ||
142 | // Point of intersection | ||
143 | CGPoint P; | ||
144 | P.x = A.x + S * (B.x - A.x); | ||
145 | P.y = A.y + S * (B.y - A.y); | ||
146 | return P; | ||
147 | } | ||
148 | |||
149 | return CGPointZero; | ||
150 | } | ||
151 | |||
152 | BOOL ccpLineIntersect(CGPoint A, CGPoint B, | ||
153 | CGPoint C, CGPoint D, | ||
154 | float *S, float *T) | ||
155 | { | ||
156 | // FAIL: Line undefined | ||
157 | if ( (A.x==B.x && A.y==B.y) || (C.x==D.x && C.y==D.y) ) return NO; | ||
158 | |||
159 | const float BAx = B.x - A.x; | ||
160 | const float BAy = B.y - A.y; | ||
161 | const float DCx = D.x - C.x; | ||
162 | const float DCy = D.y - C.y; | ||
163 | const float ACx = A.x - C.x; | ||
164 | const float ACy = A.y - C.y; | ||
165 | |||
166 | const float denom = DCy*BAx - DCx*BAy; | ||
167 | |||
168 | *S = DCx*ACy - DCy*ACx; | ||
169 | *T = BAx*ACy - BAy*ACx; | ||
170 | |||
171 | if (denom == 0) { | ||
172 | if (*S == 0 || *T == 0) { | ||
173 | // Lines incident | ||
174 | return YES; | ||
175 | } | ||
176 | // Lines parallel and not incident | ||
177 | return NO; | ||
178 | } | ||
179 | |||
180 | *S = *S / denom; | ||
181 | *T = *T / denom; | ||
182 | |||
183 | // Point of intersection | ||
184 | // CGPoint P; | ||
185 | // P.x = A.x + *S * (B.x - A.x); | ||
186 | // P.y = A.y + *S * (B.y - A.y); | ||
187 | |||
188 | return YES; | ||
189 | } | ||
190 | |||
191 | float ccpAngle(CGPoint a, CGPoint b) | ||
192 | { | ||
193 | float angle = acosf(ccpDot(ccpNormalize(a), ccpNormalize(b))); | ||
194 | if( fabs(angle) < kCGPointEpsilon ) return 0.f; | ||
195 | return angle; | ||
196 | } | ||