API 0.9.5
AppKit/CoreGraphics/CGGeometry.j
Go to the documentation of this file.
00001 /*
00002  * CGGeometry.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 #define _function(inline) function inline { return _##inline; }
00024 
00025 _function(CGPointMake(x, y))
00026 _function(CGPointMakeZero())
00027 _function(CGPointMakeCopy(aPoint))
00028 _function(CGPointCreateCopy(aPoint))
00029 
00030 _function(CGPointEqualToPoint(lhsPoint, rhsPoint))
00031 _function(CGStringFromPoint(aPoint))
00032 
00033 _function(CGSizeMake(width, height))
00034 _function(CGSizeMakeZero())
00035 _function(CGSizeMakeCopy(aSize))
00036 _function(CGSizeCreateCopy(aSize))
00037 
00038 _function(CGSizeEqualToSize(lhsSize, rhsSize))
00039 _function(CGStringFromSize(aSize))
00040 
00041 _function(CGRectMake(x, y, width, height))
00042 _function(CGRectMakeZero())
00043 _function(CGRectMakeCopy(aRect))
00044 _function(CGRectCreateCopy(aRect))
00045 
00046 _function(CGRectEqualToRect(lhsRect, rhsRect))
00047 _function(CGStringFromRect(aRect))
00048 
00049 _function(CGRectOffset(aRect, dX, dY))
00050 _function(CGRectInset(aRect, dX, dY))
00051 
00052 _function(CGRectGetHeight(aRect))
00053 _function(CGRectGetMaxX(aRect))
00054 _function(CGRectGetMaxY(aRect))
00055 _function(CGRectGetMidX(aRect))
00056 _function(CGRectGetMidY(aRect))
00057 _function(CGRectGetMinX(aRect))
00058 _function(CGRectGetMinY(aRect))
00059 _function(CGRectGetWidth(aRect))
00060 
00061 _function(CGRectIsEmpty(aRect))
00062 _function(CGRectIsNull(aRect))
00063 
00064 _function(CGRectContainsPoint(aRect, aPoint))
00065 
00066 _function(CGInsetMake(top, right, bottom, left))
00067 _function(CGInsetMakeZero())
00068 _function(CGInsetMakeCopy(anInset))
00069 _function(CGInsetIsEmpty(anInset))
00070 _function(CGInsetEqualToInset(lhsInset, rhsInset))
00071 
00072 CGMinXEdge = 0;
00073 CGMinYEdge = 1;
00074 CGMaxXEdge = 2;
00075 CGMaxYEdge = 3;
00076 
00077 CGRectNull = _CGRectMake(Infinity, Infinity, 0.0, 0.0);
00078 
00094 function CGRectDivide(inRect, slice, rem, amount, edge)
00095 {
00096     slice.origin = _CGPointMakeCopy(inRect.origin);
00097     slice.size = _CGSizeMakeCopy(inRect.size);
00098     rem.origin = _CGPointMakeCopy(inRect.origin);
00099     rem.size = _CGSizeMakeCopy(inRect.size);
00100 
00101     switch (edge)
00102     {
00103         case CGMinXEdge:
00104             slice.size.width = amount;
00105             rem.origin.x += amount;
00106             rem.size.width -= amount;
00107             break;
00108 
00109         case CGMaxXEdge:
00110             slice.origin.x = _CGRectGetMaxX(slice) - amount;
00111             slice.size.width = amount;
00112             rem.size.width -= amount;
00113             break;
00114 
00115         case CGMinYEdge:
00116             slice.size.height = amount;
00117             rem.origin.y += amount;
00118             rem.size.height -= amount;
00119             break;
00120 
00121         case CGMaxYEdge:
00122             slice.origin.y = _CGRectGetMaxY(slice) - amount;
00123             slice.size.height = amount;
00124             rem.size.height -= amount;
00125     }
00126 }
00127 
00136 function CGRectContainsRect(lhsRect, rhsRect)
00137 {
00138     var union = CGRectUnion(lhsRect, rhsRect);
00139 
00140     return _CGRectEqualToRect(union, lhsRect);
00141 }
00142 
00150 function CGRectIntersectsRect(lhsRect, rhsRect)
00151 {
00152     var intersection = CGRectIntersection(lhsRect, rhsRect);
00153 
00154     return !_CGRectIsEmpty(intersection);
00155 }
00156 
00164 function CGRectIntegral(aRect)
00165 {
00166     aRect = CGRectStandardize(aRect);
00167 
00168     // Store these out separately, if not the GetMaxes will return incorrect values.
00169     var x = FLOOR(_CGRectGetMinX(aRect)),
00170         y = FLOOR(_CGRectGetMinY(aRect));
00171 
00172     aRect.size.width = CEIL(_CGRectGetMaxX(aRect)) - x;
00173     aRect.size.height = CEIL(_CGRectGetMaxY(aRect)) - y;
00174 
00175     aRect.origin.x = x;
00176     aRect.origin.y = y;
00177 
00178     return aRect;
00179 }
00180 
00188 function CGRectIntersection(lhsRect, rhsRect)
00189 {
00190     var intersection = _CGRectMake(
00191         MAX(_CGRectGetMinX(lhsRect), _CGRectGetMinX(rhsRect)),
00192         MAX(_CGRectGetMinY(lhsRect), _CGRectGetMinY(rhsRect)),
00193         0, 0);
00194 
00195     intersection.size.width = MIN(_CGRectGetMaxX(lhsRect), _CGRectGetMaxX(rhsRect)) - _CGRectGetMinX(intersection);
00196     intersection.size.height = MIN(_CGRectGetMaxY(lhsRect), _CGRectGetMaxY(rhsRect)) - _CGRectGetMinY(intersection);
00197 
00198     return _CGRectIsEmpty(intersection) ? _CGRectMakeZero() : intersection;
00199 }
00200 
00201 /*
00202 
00203 */
00204 function CGRectStandardize(aRect)
00205 {
00206     var width = _CGRectGetWidth(aRect),
00207         height = _CGRectGetHeight(aRect),
00208         standardized = _CGRectMakeCopy(aRect);
00209 
00210     if (width < 0.0)
00211     {
00212         standardized.origin.x += width;
00213         standardized.size.width = -width;
00214     }
00215 
00216     if (height < 0.0)
00217     {
00218         standardized.origin.y += height;
00219         standardized.size.height = -height;
00220     }
00221 
00222     return standardized;
00223 }
00224 
00225 function CGRectUnion(lhsRect, rhsRect)
00226 {
00227     var lhsRectIsNull = !lhsRect || lhsRect === CGRectNull,
00228         rhsRectIsNull = !rhsRect || rhsRect === CGRectNull;
00229 
00230     if (lhsRectIsNull)
00231         return rhsRectIsNull ? CGRectNull : rhsRect;
00232 
00233     if (rhsRectIsNull)
00234         return lhsRectIsNull ? CGRectNull : lhsRect;
00235 
00236     var minX = MIN(_CGRectGetMinX(lhsRect), _CGRectGetMinX(rhsRect)),
00237         minY = MIN(_CGRectGetMinY(lhsRect), _CGRectGetMinY(rhsRect)),
00238         maxX = MAX(_CGRectGetMaxX(lhsRect), _CGRectGetMaxX(rhsRect)),
00239         maxY = MAX(_CGRectGetMaxY(lhsRect), _CGRectGetMaxY(rhsRect));
00240 
00241     return _CGRectMake(minX, minY, maxX - minX, maxY - minY);
00242 }
00243 
00244 function CGPointFromString(aString)
00245 {
00246     var comma = aString.indexOf(',');
00247 
00248     return { x:parseInt(aString.substr(1, comma - 1)), y:parseInt(aString.substring(comma + 1, aString.length)) };
00249 }
00250 
00251 function CGSizeFromString(aString)
00252 {
00253     var comma = aString.indexOf(',');
00254 
00255     return { width:parseInt(aString.substr(1, comma - 1)), height:parseInt(aString.substring(comma + 1, aString.length)) };
00256 }
00257 
00258 function CGRectFromString(aString)
00259 {
00260     var comma = aString.indexOf(',', aString.indexOf(',') + 1);
00261 
00262     return { origin:CGPointFromString(aString.substr(1, comma - 1)), size:CGSizeFromString(aString.substring(comma + 2, aString.length)) };
00263 }
00264 
00265 function CGPointFromEvent(anEvent)
00266 {
00267     return _CGPointMake(anEvent.clientX, anEvent.clientY);
00268 }
00269 
00270 function CGInsetFromString(aString)
00271 {
00272     var numbers = aString.substr(1, aString.length - 2).split(',');
00273 
00274     return _CGInsetMake(parseFloat(numbers[0]), parseFloat(numbers[1]), parseFloat(numbers[2]), parseFloat(numbers[3]));
00275 }
00276 
00277 CGInsetFromCPString = CGInsetFromString;
00278 
00279 function CPStringFromCGInset(anInset)
00280 {
00281     return '{' + anInset.top + ", " + anInset.left + ", " + anInset.bottom + ", " + anInset.right + '}';
00282 }
00283 
 All Classes Files Functions Variables Defines