|
cocos2d for iPhone 1.0.0
2D engine for iOS and OS X
|
00001 /* cocos2d for iPhone 00002 * http://www.cocos2d-iphone.org 00003 * 00004 * Copyright (c) 2007 Scott Lembcke 00005 * 00006 * Copyright (c) 2010 Lam Pham 00007 * 00008 * Permission is hereby granted, free of charge, to any person obtaining a copy 00009 * of this software and associated documentation files (the "Software"), to deal 00010 * in the Software without restriction, including without limitation the rights 00011 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00012 * copies of the Software, and to permit persons to whom the Software is 00013 * furnished to do so, subject to the following conditions: 00014 * 00015 * The above copyright notice and this permission notice shall be included in 00016 * all copies or substantial portions of the Software. 00017 * 00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00019 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00020 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00021 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00022 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00023 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00024 * SOFTWARE. 00025 */ 00026 00027 /* 00028 * Some of the functions were based on Chipmunk's cpVect.h. 00029 */ 00030 00047 #import <Availability.h> 00048 00049 #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED 00050 #import <CoreGraphics/CGGeometry.h> 00051 #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) 00052 #import <Foundation/Foundation.h> 00053 #endif 00054 00055 #import <math.h> 00056 #import <objc/objc.h> 00057 00058 #ifdef __cplusplus 00059 extern "C" { 00060 #endif 00061 00066 #define ccp(__X__,__Y__) CGPointMake(__X__,__Y__) 00067 00068 00073 static inline CGPoint 00074 ccpNeg(const CGPoint v) 00075 { 00076 return ccp(-v.x, -v.y); 00077 } 00078 00083 static inline CGPoint 00084 ccpAdd(const CGPoint v1, const CGPoint v2) 00085 { 00086 return ccp(v1.x + v2.x, v1.y + v2.y); 00087 } 00088 00093 static inline CGPoint 00094 ccpSub(const CGPoint v1, const CGPoint v2) 00095 { 00096 return ccp(v1.x - v2.x, v1.y - v2.y); 00097 } 00098 00103 static inline CGPoint 00104 ccpMult(const CGPoint v, const CGFloat s) 00105 { 00106 return ccp(v.x*s, v.y*s); 00107 } 00108 00113 static inline CGPoint 00114 ccpMidpoint(const CGPoint v1, const CGPoint v2) 00115 { 00116 return ccpMult(ccpAdd(v1, v2), 0.5f); 00117 } 00118 00123 static inline CGFloat 00124 ccpDot(const CGPoint v1, const CGPoint v2) 00125 { 00126 return v1.x*v2.x + v1.y*v2.y; 00127 } 00128 00133 static inline CGFloat 00134 ccpCross(const CGPoint v1, const CGPoint v2) 00135 { 00136 return v1.x*v2.y - v1.y*v2.x; 00137 } 00138 00143 static inline CGPoint 00144 ccpPerp(const CGPoint v) 00145 { 00146 return ccp(-v.y, v.x); 00147 } 00148 00153 static inline CGPoint 00154 ccpRPerp(const CGPoint v) 00155 { 00156 return ccp(v.y, -v.x); 00157 } 00158 00163 static inline CGPoint 00164 ccpProject(const CGPoint v1, const CGPoint v2) 00165 { 00166 return ccpMult(v2, ccpDot(v1, v2)/ccpDot(v2, v2)); 00167 } 00168 00173 static inline CGPoint 00174 ccpRotate(const CGPoint v1, const CGPoint v2) 00175 { 00176 return ccp(v1.x*v2.x - v1.y*v2.y, v1.x*v2.y + v1.y*v2.x); 00177 } 00178 00183 static inline CGPoint 00184 ccpUnrotate(const CGPoint v1, const CGPoint v2) 00185 { 00186 return ccp(v1.x*v2.x + v1.y*v2.y, v1.y*v2.x - v1.x*v2.y); 00187 } 00188 00193 static inline CGFloat 00194 ccpLengthSQ(const CGPoint v) 00195 { 00196 return ccpDot(v, v); 00197 } 00198 00203 CGFloat ccpLength(const CGPoint v); 00204 00209 CGFloat ccpDistance(const CGPoint v1, const CGPoint v2); 00210 00215 CGPoint ccpNormalize(const CGPoint v); 00216 00221 CGPoint ccpForAngle(const CGFloat a); 00222 00227 CGFloat ccpToAngle(const CGPoint v); 00228 00229 00233 float clampf(float value, float min_inclusive, float max_inclusive); 00234 00238 CGPoint ccpClamp(CGPoint p, CGPoint from, CGPoint to); 00239 00243 CGPoint ccpFromSize(CGSize s); 00244 00252 CGPoint ccpCompOp(CGPoint p, float (*opFunc)(float)); 00253 00261 CGPoint ccpLerp(CGPoint a, CGPoint b, float alpha); 00262 00263 00267 BOOL ccpFuzzyEqual(CGPoint a, CGPoint b, float variance); 00268 00269 00274 CGPoint ccpCompMult(CGPoint a, CGPoint b); 00275 00279 float ccpAngleSigned(CGPoint a, CGPoint b); 00280 00284 float ccpAngle(CGPoint a, CGPoint b); 00285 00293 CGPoint ccpRotateByAngle(CGPoint v, CGPoint pivot, float angle); 00294 00316 BOOL ccpLineIntersect(CGPoint p1, CGPoint p2, 00317 CGPoint p3, CGPoint p4, 00318 float *s, float *t); 00319 00320 /* 00321 ccpSegmentIntersect returns YES if Segment A-B intersects with segment C-D 00322 @since v1.0.0 00323 */ 00324 BOOL ccpSegmentIntersect(CGPoint A, CGPoint B, CGPoint C, CGPoint D); 00325 00326 /* 00327 ccpIntersectPoint returns the intersection point of line A-B, C-D 00328 @since v1.0.0 00329 */ 00330 CGPoint ccpIntersectPoint(CGPoint A, CGPoint B, CGPoint C, CGPoint D); 00331 00332 #ifdef __cplusplus 00333 } 00334 #endif