![]() |
API 0.9.5
|
00001 /* 00002 * CPGeometry.j 00003 * AppKit 00004 * 00005 * Created by Francisco Tolmasky. 00006 * Copyright 2008, 280 North, Inc. 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 00024 00025 CPMinXEdge = 0; 00026 CPMinYEdge = 1; 00027 CPMaxXEdge = 2; 00028 CPMaxYEdge = 3; 00029 00030 // FIXME: the rest! 00031 CPMakePoint = CGPointMake; 00032 CPMakeSize = CGSizeMake; 00033 CPMakeRect = CGRectMake; 00034 00046 function CPPointCreateCopy(aPoint) 00047 { 00048 return { x: aPoint.x, y: aPoint.y }; 00049 } 00050 00058 function CPPointMake(x, y) 00059 { 00060 return { x: x, y: y }; 00061 } 00062 00070 function CPRectInset(aRect, dX, dY) 00071 { 00072 return CPRectMake( aRect.origin.x + dX, aRect.origin.y + dY, 00073 aRect.size.width - 2 * dX, aRect.size.height - 2 * dY); 00074 } 00075 00082 function CPRectIntegral(aRect) 00083 { 00084 // FIXME!!! 00085 alert("CPRectIntegral unimplemented"); 00086 } 00087 00095 function CPRectIntersection(lhsRect, rhsRect) 00096 { 00097 var intersection = CPRectMake( 00098 Math.max(CPRectGetMinX(lhsRect), CPRectGetMinX(rhsRect)), 00099 Math.max(CPRectGetMinY(lhsRect), CPRectGetMinY(rhsRect)), 00100 0, 0); 00101 00102 intersection.size.width = Math.min(CPRectGetMaxX(lhsRect), CPRectGetMaxX(rhsRect)) - CPRectGetMinX(intersection); 00103 intersection.size.height = Math.min(CPRectGetMaxY(lhsRect), CPRectGetMaxY(rhsRect)) - CPRectGetMinY(intersection); 00104 00105 return CPRectIsEmpty(intersection) ? CPRectMakeZero() : intersection; 00106 } 00107 00114 function CPRectCreateCopy(aRect) 00115 { 00116 return { origin: CPPointCreateCopy(aRect.origin), size: CPSizeCreateCopy(aRect.size) }; 00117 } 00118 00128 function CPRectMake(x, y, width, height) 00129 { 00130 return { origin: CPPointMake(x, y), size: CPSizeMake(width, height) }; 00131 } 00132 00141 function CPRectOffset(aRect, dX, dY) 00142 { 00143 return CPRectMake(aRect.origin.x + dX, aRect.origin.y + dY, aRect.size.width, aRect.size.height); 00144 } 00145 00151 function CPRectStandardize(aRect) 00152 { 00153 var width = CPRectGetWidth(aRect), 00154 height = CPRectGetHeight(aRect), 00155 standardized = CPRectCreateCopy(aRect); 00156 00157 if (width < 0.0) 00158 { 00159 standardized.origin.x += width; 00160 standardized.size.width = -width; 00161 } 00162 00163 if (height < 0.0) 00164 { 00165 standardized.origin.y += height; 00166 standardized.size.height = -height; 00167 } 00168 00169 return standardized; 00170 } 00171 00179 function CPRectUnion(lhsRect, rhsRect) 00180 { 00181 var minX = Math.min(CPRectGetMinX(lhsRect), CPRectGetMinX(rhsRect)), 00182 minY = Math.min(CPRectGetMinY(lhsRect), CPRectGetMinY(rhsRect)), 00183 maxX = Math.max(CPRectGetMaxX(lhsRect), CPRectGetMaxX(rhsRect)), 00184 maxY = Math.max(CPRectGetMaxY(lhsRect), CPRectGetMaxY(rhsRect)); 00185 00186 return CPRectMake(minX, minY, maxX - minX, maxY - minY); 00187 } 00188 00195 function CPSizeCreateCopy(aSize) 00196 { 00197 return { width: aSize.width, height: aSize.height }; 00198 } 00199 00207 function CPSizeMake(width, height) 00208 { 00209 return { width: width, height: height }; 00210 } 00211 00220 function CPRectContainsPoint(aRect, aPoint) 00221 { 00222 return aPoint.x >= CPRectGetMinX(aRect) && 00223 aPoint.y >= CPRectGetMinY(aRect) && 00224 aPoint.x < CPRectGetMaxX(aRect) && 00225 aPoint.y < CPRectGetMaxY(aRect); 00226 } 00227 00236 function CPRectContainsRect(possibleOuter, possibleInner) 00237 { 00238 return CGRectContainsRect(possibleOuter, possibleInner); 00239 } 00240 00249 function CPPointEqualToPoint(lhsPoint, rhsPoint) 00250 { 00251 return lhsPoint.x == rhsPoint.x && lhsPoint.y == rhsPoint.y; 00252 } 00253 00261 function CPRectEqualToRect(lhsRect, rhsRect) 00262 { 00263 return CPPointEqualToPoint(lhsRect.origin, rhsRect.origin) && 00264 CPSizeEqualToSize(lhsRect.size, rhsRect.size); 00265 } 00266 00272 function CPRectGetHeight(aRect) 00273 { 00274 return aRect.size.height; 00275 } 00276 00282 function CPRectGetMaxX(aRect) 00283 { 00284 return aRect.origin.x + aRect.size.width; 00285 } 00286 00292 function CPRectGetMaxY(aRect) 00293 { 00294 return aRect.origin.y + aRect.size.height; 00295 } 00296 00302 function CPRectGetMidX(aRect) 00303 { 00304 return aRect.origin.x + (aRect.size.width) / 2.0; 00305 } 00306 00312 function CPRectGetMidY(aRect) 00313 { 00314 return aRect.origin.y + (aRect.size.height) / 2.0; 00315 } 00316 00322 function CPRectGetMinX(aRect) 00323 { 00324 return aRect.origin.x; 00325 } 00326 00332 function CPRectGetMinY(aRect) 00333 { 00334 return aRect.origin.y; 00335 } 00336 00342 function CPRectGetWidth(aRect) 00343 { 00344 return aRect.size.width; 00345 } 00346 00354 function CPRectIntersectsRect(lhsRect, rhsRect) 00355 { 00356 return !CPRectIsEmpty(CPRectIntersection(lhsRect, rhsRect)); 00357 } 00358 00366 function CPRectIsEmpty(aRect) 00367 { 00368 return aRect.size.width <= 0.0 || aRect.size.height <= 0.0; 00369 } 00370 00377 function CPRectIsNull(aRect) 00378 { 00379 return aRect.size.width <= 0.0 || aRect.size.height <= 0.0; 00380 } 00381 00391 function CPDivideRect(inRect, slice, rem, amount, edge) 00392 { 00393 CGRectDivide(inRect, slice, rem, amount, edge); 00394 } 00395 00403 function CPSizeEqualToSize(lhsSize, rhsSize) 00404 { 00405 return lhsSize.width == rhsSize.width && lhsSize.height == rhsSize.height; 00406 } 00407 00414 function CPStringFromPoint(aPoint) 00415 { 00416 return "{" + aPoint.x + ", " + aPoint.y + "}"; 00417 } 00418 00425 function CPStringFromSize(aSize) 00426 { 00427 return "{" + aSize.width + ", " + aSize.height + "}"; 00428 } 00429 00436 function CPStringFromRect(aRect) 00437 { 00438 return "{" + CPStringFromPoint(aRect.origin) + ", " + CPStringFromSize(aRect.size) + "}"; 00439 } 00440 00447 function CPPointFromString(aString) 00448 { 00449 var comma = aString.indexOf(','); 00450 00451 return { x:parseFloat(aString.substr(1, comma - 1), 10), y:parseFloat(aString.substring(comma + 1, aString.length), 10) }; 00452 } 00453 00460 function CPSizeFromString(aString) 00461 { 00462 var comma = aString.indexOf(','); 00463 00464 return { width:parseFloat(aString.substr(1, comma - 1), 10), height:parseFloat(aString.substring(comma + 1, aString.length), 10) }; 00465 } 00466 00473 function CPRectFromString(aString) 00474 { 00475 var comma = aString.indexOf(',', aString.indexOf(',') + 1); 00476 00477 return { origin:CPPointFromString(aString.substr(1, comma - 1)), size:CPSizeFromString(aString.substring(comma + 2, aString.length)) }; 00478 } 00479 00485 function CPPointFromEvent(anEvent) 00486 { 00487 return CPPointMake(anEvent.clientX, anEvent.clientY, 0); 00488 } 00489 00495 function CPSizeMakeZero() 00496 { 00497 return CPSizeMake(0, 0); 00498 } 00499 00505 function CPRectMakeZero() 00506 { 00507 return CPRectMake(0, 0, 0, 0); 00508 } 00509 00515 function CPPointMakeZero() 00516 { 00517 return CPPointMake(0, 0, 0); 00518 } 00519