summary refs log tree commit diff stats
path: root/libs/cocos2d/Support/CGPointExtension.h
diff options
context:
space:
mode:
authorStarla Insigna <starla4444@gmail.com>2011-07-30 11:19:14 -0400
committerStarla Insigna <starla4444@gmail.com>2011-07-30 11:19:14 -0400
commit9cd57b731ab1c666d4a1cb725538fdc137763d12 (patch)
tree5bac45ae5157a1cb10c6e45500cbf72789917980 /libs/cocos2d/Support/CGPointExtension.h
downloadcartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.gz
cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.bz2
cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.zip
Initial commit (version 0.2.1)
Diffstat (limited to 'libs/cocos2d/Support/CGPointExtension.h')
-rwxr-xr-xlibs/cocos2d/Support/CGPointExtension.h334
1 files changed, 334 insertions, 0 deletions
diff --git a/libs/cocos2d/Support/CGPointExtension.h b/libs/cocos2d/Support/CGPointExtension.h new file mode 100755 index 0000000..96edeb7 --- /dev/null +++ b/libs/cocos2d/Support/CGPointExtension.h
@@ -0,0 +1,334 @@
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/*
28 * Some of the functions were based on Chipmunk's cpVect.h.
29 */
30
31/**
32 @file
33 CGPoint extensions based on Chipmunk's cpVect file.
34 These extensions work both with CGPoint and cpVect.
35
36 The "ccp" prefix means: "CoCos2d Point"
37
38 Examples:
39 - ccpAdd( ccp(1,1), ccp(2,2) ); // preferred cocos2d way
40 - ccpAdd( CGPointMake(1,1), CGPointMake(2,2) ); // also ok but more verbose
41
42 - cpvadd( cpv(1,1), cpv(2,2) ); // way of the chipmunk
43 - ccpAdd( cpv(1,1), cpv(2,2) ); // mixing chipmunk and cocos2d (avoid)
44 - cpvadd( CGPointMake(1,1), CGPointMake(2,2) ); // mixing chipmunk and CG (avoid)
45 */
46
47#import <Availability.h>
48
49#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
50#import <CoreGraphics/CGGeometry.h>
51#elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
52#import <Foundation/Foundation.h>
53#endif
54
55#import <math.h>
56#import <objc/objc.h>
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
62/** Helper macro that creates a CGPoint
63 @return CGPoint
64 @since v0.7.2
65 */
66#define ccp(__X__,__Y__) CGPointMake(__X__,__Y__)
67
68
69/** Returns opposite of point.
70 @return CGPoint
71 @since v0.7.2
72 */
73static inline CGPoint
74ccpNeg(const CGPoint v)
75{
76 return ccp(-v.x, -v.y);
77}
78
79/** Calculates sum of two points.
80 @return CGPoint
81 @since v0.7.2
82 */
83static inline CGPoint
84ccpAdd(const CGPoint v1, const CGPoint v2)
85{
86 return ccp(v1.x + v2.x, v1.y + v2.y);
87}
88
89/** Calculates difference of two points.
90 @return CGPoint
91 @since v0.7.2
92 */
93static inline CGPoint
94ccpSub(const CGPoint v1, const CGPoint v2)
95{
96 return ccp(v1.x - v2.x, v1.y - v2.y);
97}
98
99/** Returns point multiplied by given factor.
100 @return CGPoint
101 @since v0.7.2
102 */
103static inline CGPoint
104ccpMult(const CGPoint v, const CGFloat s)
105{
106 return ccp(v.x*s, v.y*s);
107}
108
109/** Calculates midpoint between two points.
110 @return CGPoint
111 @since v0.7.2
112 */
113static inline CGPoint
114ccpMidpoint(const CGPoint v1, const CGPoint v2)
115{
116 return ccpMult(ccpAdd(v1, v2), 0.5f);
117}
118
119/** Calculates dot product of two points.
120 @return CGFloat
121 @since v0.7.2
122 */
123static inline CGFloat
124ccpDot(const CGPoint v1, const CGPoint v2)
125{
126 return v1.x*v2.x + v1.y*v2.y;
127}
128
129/** Calculates cross product of two points.
130 @return CGFloat
131 @since v0.7.2
132 */
133static inline CGFloat
134ccpCross(const CGPoint v1, const CGPoint v2)
135{
136 return v1.x*v2.y - v1.y*v2.x;
137}
138
139/** Calculates perpendicular of v, rotated 90 degrees counter-clockwise -- cross(v, perp(v)) >= 0
140 @return CGPoint
141 @since v0.7.2
142 */
143static inline CGPoint
144ccpPerp(const CGPoint v)
145{
146 return ccp(-v.y, v.x);
147}
148
149/** Calculates perpendicular of v, rotated 90 degrees clockwise -- cross(v, rperp(v)) <= 0
150 @return CGPoint
151 @since v0.7.2
152 */
153static inline CGPoint
154ccpRPerp(const CGPoint v)
155{
156 return ccp(v.y, -v.x);
157}
158
159/** Calculates the projection of v1 over v2.
160 @return CGPoint
161 @since v0.7.2
162 */
163static inline CGPoint
164ccpProject(const CGPoint v1, const CGPoint v2)
165{
166 return ccpMult(v2, ccpDot(v1, v2)/ccpDot(v2, v2));
167}
168
169/** Rotates two points.
170 @return CGPoint
171 @since v0.7.2
172 */
173static inline CGPoint
174ccpRotate(const CGPoint v1, const CGPoint v2)
175{
176 return ccp(v1.x*v2.x - v1.y*v2.y, v1.x*v2.y + v1.y*v2.x);
177}
178
179/** Unrotates two points.
180 @return CGPoint
181 @since v0.7.2
182 */
183static inline CGPoint
184ccpUnrotate(const CGPoint v1, const CGPoint v2)
185{
186 return ccp(v1.x*v2.x + v1.y*v2.y, v1.y*v2.x - v1.x*v2.y);
187}
188
189/** Calculates the square length of a CGPoint (not calling sqrt() )
190 @return CGFloat
191 @since v0.7.2
192 */
193static inline CGFloat
194ccpLengthSQ(const CGPoint v)
195{
196 return ccpDot(v, v);
197}
198
199/** Calculates distance between point an origin
200 @return CGFloat
201 @since v0.7.2
202 */
203CGFloat ccpLength(const CGPoint v);
204
205/** Calculates the distance between two points
206 @return CGFloat
207 @since v0.7.2
208 */
209CGFloat ccpDistance(const CGPoint v1, const CGPoint v2);
210
211/** Returns point multiplied to a length of 1.
212 @return CGPoint
213 @since v0.7.2
214 */
215CGPoint ccpNormalize(const CGPoint v);
216
217/** Converts radians to a normalized vector.
218 @return CGPoint
219 @since v0.7.2
220 */
221CGPoint ccpForAngle(const CGFloat a);
222
223/** Converts a vector to radians.
224 @return CGFloat
225 @since v0.7.2
226 */
227CGFloat ccpToAngle(const CGPoint v);
228
229
230/** Clamp a value between from and to.
231 @since v0.99.1
232 */
233float clampf(float value, float min_inclusive, float max_inclusive);
234
235/** Clamp a point between from and to.
236 @since v0.99.1
237 */
238CGPoint ccpClamp(CGPoint p, CGPoint from, CGPoint to);
239
240/** Quickly convert CGSize to a CGPoint
241 @since v0.99.1
242 */
243CGPoint ccpFromSize(CGSize s);
244
245/** Run a math operation function on each point component
246 * absf, fllorf, ceilf, roundf
247 * any function that has the signature: float func(float);
248 * For example: let's try to take the floor of x,y
249 * ccpCompOp(p,floorf);
250 @since v0.99.1
251 */
252CGPoint ccpCompOp(CGPoint p, float (*opFunc)(float));
253
254/** Linear Interpolation between two points a and b
255 @returns
256 alpha == 0 ? a
257 alpha == 1 ? b
258 otherwise a value between a..b
259 @since v0.99.1
260 */
261CGPoint ccpLerp(CGPoint a, CGPoint b, float alpha);
262
263
264/** @returns if points have fuzzy equality which means equal with some degree of variance.
265 @since v0.99.1
266 */
267BOOL ccpFuzzyEqual(CGPoint a, CGPoint b, float variance);
268
269
270/** Multiplies a nd b components, a.x*b.x, a.y*b.y
271 @returns a component-wise multiplication
272 @since v0.99.1
273 */
274CGPoint ccpCompMult(CGPoint a, CGPoint b);
275
276/** @returns the signed angle in radians between two vector directions
277 @since v0.99.1
278 */
279float ccpAngleSigned(CGPoint a, CGPoint b);
280
281/** @returns the angle in radians between two vector directions
282 @since v0.99.1
283*/
284float ccpAngle(CGPoint a, CGPoint b);
285
286/** Rotates a point counter clockwise by the angle around a pivot
287 @param v is the point to rotate
288 @param pivot is the pivot, naturally
289 @param angle is the angle of rotation cw in radians
290 @returns the rotated point
291 @since v0.99.1
292 */
293CGPoint ccpRotateByAngle(CGPoint v, CGPoint pivot, float angle);
294
295/** A general line-line intersection test
296 @param p1
297 is the startpoint for the first line P1 = (p1 - p2)
298 @param p2
299 is the endpoint for the first line P1 = (p1 - p2)
300 @param p3
301 is the startpoint for the second line P2 = (p3 - p4)
302 @param p4
303 is the endpoint for the second line P2 = (p3 - p4)
304 @param s
305 is the range for a hitpoint in P1 (pa = p1 + s*(p2 - p1))
306 @param t
307 is the range for a hitpoint in P3 (pa = p2 + t*(p4 - p3))
308 @return bool
309 indicating successful intersection of a line
310 note that to truly test intersection for segments we have to make
311 sure that s & t lie within [0..1] and for rays, make sure s & t > 0
312 the hit point is p3 + t * (p4 - p3);
313 the hit point also is p1 + s * (p2 - p1);
314 @since v0.99.1
315 */
316BOOL ccpLineIntersect(CGPoint p1, CGPoint p2,
317 CGPoint p3, CGPoint p4,
318 float *s, float *t);
319
320/*
321 ccpSegmentIntersect returns YES if Segment A-B intersects with segment C-D
322 @since v1.0.0
323 */
324BOOL ccpSegmentIntersect(CGPoint A, CGPoint B, CGPoint C, CGPoint D);
325
326/*
327 ccpIntersectPoint returns the intersection point of line A-B, C-D
328 @since v1.0.0
329 */
330CGPoint ccpIntersectPoint(CGPoint A, CGPoint B, CGPoint C, CGPoint D);
331
332#ifdef __cplusplus
333}
334#endif