00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "CGGeometry.h"
00024 #include "CGAffineTransform.h"
00025
00026 @import "CGGeometry.j"
00027 @import "CGAffineTransform.j"
00028
00029
00030 kCGPathElementMoveToPoint = 0;
00031 kCGPathElementAddLineToPoint = 1;
00032 kCGPathElementAddQuadCurveToPoint = 2;
00033 kCGPathElementAddCurveToPoint = 3;
00034 kCGPathElementCloseSubpath = 4;
00035
00036 kCGPathElementAddArc = 5;
00037 kCGPathElementAddArcToPoint = 6;
00038
00047 function CGPathCreateMutable()
00048 {
00049 return { count:0, start:NULL, current:NULL, elements:[] };
00050 }
00051
00056 function CGPathCreateMutableCopy(aPath)
00057 {
00058 var path = CGPathCreateMutable();
00059
00060 CGPathAddPath(path, aPath);
00061
00062 return path;
00063 }
00064
00069 function CGPathCreateCopy(aPath)
00070 {
00071 return CGPathCreateMutableCopy(aPath);
00072 }
00073
00074 function CGPathRelease(aPath)
00075 {
00076 }
00077
00078 function CGPathRetain(aPath)
00079 {
00080 return aPath;
00081 }
00082
00083 function CGPathAddArc(aPath, aTransform, x, y, aRadius, aStartAngle, anEndAngle, isClockwise)
00084 {
00085 if (aTransform && !_CGAffineTransformIsIdentity(aTransform))
00086 {
00087 var center = _CGPointMake(x, y),
00088 end = _CGPointMake(COS(anEndAngle), SIN(anEndAngle)),
00089 start = _CGPointMake(COS(aStartAngle), SIN(aStartAngle));
00090
00091 end = _CGPointApplyAffineTransform(end, aTransform);
00092 start = _CGPointApplyAffineTransform(start, aTransform);
00093 center = _CGPointApplyAffineTransform(center, aTransform);
00094
00095 x = center.x;
00096 y = center.y;
00097
00098 var oldEndAngle = anEndAngle,
00099 oldStartAngle = aStartAngle;
00100
00101 anEndAngle = ATAN2(end.y - aTransform.ty, end.x - aTransform.tx);
00102 aStartAngle = ATAN2(start.y - aTransform.ty, start.x - aTransform.tx);
00103
00104
00105
00106
00107 if (anEndAngle == aStartAngle && oldEndAngle != oldStartAngle)
00108 if (oldStartAngle > oldEndAngle)
00109 anEndAngle = anEndAngle - PI2;
00110 else
00111 aStartAngle = aStartAngle - PI2;
00112
00113 aRadius = _CGSizeMake(aRadius, 0);
00114 aRadius = _CGSizeApplyAffineTransform(aRadius, aTransform);
00115 aRadius = SQRT(aRadius.width * aRadius.width + aRadius.height * aRadius.height);
00116 }
00117
00118 aPath.current = _CGPointMake(x + aRadius * COS(anEndAngle), y + aRadius * SIN(anEndAngle));
00119 aPath.elements[aPath.count++] = { type:kCGPathElementAddArc, x:x, y:y, radius:aRadius, startAngle:aStartAngle, endAngle:anEndAngle };
00120 }
00121
00122 function CGPathAddArcToPoint(aPath, aTransform, x1, y1, x2, y2, aRadius)
00123 {
00124 }
00125
00126 function CGPathAddCurveToPoint(aPath, aTransform, cp1x, cp1y, cp2x, cp2y, x, y)
00127 {
00128 var cp1 = _CGPointMake(cp1x, cp1y),
00129 cp2 = _CGPointMake(cp2x, cp2y),
00130 end = _CGPointMake(x, y);
00131
00132 if (aTransform)
00133 {
00134 cp1 = _CGPointApplyAffineTransform(cp1, aTransform);
00135 cp2 = _CGPointApplyAffineTransform(cp2, aTransform);
00136 end = _CGPointApplyAffineTransform(end, aTransform);
00137 }
00138
00139 aPath.current = end;
00140 aPath.elements[aPath.count++] = { type:kCGPathElementAddCurveToPoint, cp1x:cp1.x, cp1y:cp1.y, cp2x:cp2.x, cp2y:cp2.y, x:end.x, y:end.y };
00141 }
00142
00143 function CGPathAddLines(aPath, aTransform, points, count)
00144 {
00145 var i = 1;
00146
00147 if (arguments["count"] == NULL)
00148 var count = points.length;
00149
00150 if (!aPath || count < 2)
00151 return;
00152
00153 CGPathMoveToPoint(aPath, aTransform, points[0].x, points[0].y);
00154
00155 for (; i < count; ++i)
00156 CGPathAddLineToPoint(aPath, aTransform, points[i].x, points[i].y);
00157 }
00158
00159 function CGPathAddLineToPoint(aPath, aTransform, x, y)
00160 {
00161 var point = _CGPointMake(x, y);
00162
00163 if (aTransform != NULL)
00164 point = _CGPointApplyAffineTransform(point, aTransform);
00165
00166 aPath.elements[aPath.count++] = { type: kCGPathElementAddLineToPoint, x:point.x, y:point.y };
00167 aPath.current = point;
00168 }
00169
00170 function CGPathAddPath(aPath, aTransform, anotherPath)
00171 {
00172 var i = 0,
00173 count = anotherPath.count;
00174
00175 for (; i < count; ++i)
00176 {
00177 var element = anotherPath.elements[i];
00178
00179 aPath.elements[aPath.count] = { type:element.type, x:element.x, y:element.y,
00180 cpx:element.cpx, cpy:element.cpy,
00181 radius:element.radius, startAngle:element.startAngle, endAngle:element.endAngle,
00182 cp1x:element.cp1x, cp1y:element.cp1y, cp2x:element.cp2x, cp2y:element.cp2y,
00183 points: element.points ? element.points.slice() : nil};
00184
00185 aPath.count++
00186 }
00187
00188 aPath.current = anotherPath.current;
00189 }
00190
00191 function CGPathAddQuadCurveToPoint(aPath, aTransform, cpx, cpy, x, y)
00192 {
00193 var cp = _CGPointMake(cpx, cpy),
00194 end = _CGPointMake(x, y);
00195
00196 if (aTransform)
00197 {
00198 cp = _CGPointApplyAffineTransform(control, aTransform);
00199 end = _CGPointApplyAffineTransform(end, aTransform);
00200 }
00201
00202 aPath.elements[aPath.count++] = { type:kCGPathElementAddQuadCurveToPoint, cpx:cp.x, cpy:cp.y, x:end.x, y:end.y }
00203 aPath.current = end;
00204 }
00205
00206 function CGPathAddRect(aPath, aTransform, aRect)
00207 {
00208 CGPathAddRects(aPath, aTransform, [aRect], 1);
00209 }
00210
00211 function CGPathAddRects(aPath, aTransform, rects, count)
00212 {
00213 var i = 0;
00214
00215 if (arguments["count"] == NULL)
00216 var count = rects.length;
00217
00218 for (; i < count; ++i)
00219 {
00220 var rect = rects[i];
00221
00222 CGPathMoveToPoint(aPath, aTransform, _CGRectGetMinX(rect), _CGRectGetMinY(rect));
00223 CGPathAddLineToPoint(aPath, aTransform, _CGRectGetMaxX(rect), _CGRectGetMinY(rect));
00224 CGPathAddLineToPoint(aPath, aTransform, _CGRectGetMaxX(rect), _CGRectGetMaxY(rect));
00225 CGPathAddLineToPoint(aPath, aTransform, _CGRectGetMinX(rect), _CGRectGetMaxY(rect));
00226
00227 CGPathCloseSubpath(aPath);
00228 }
00229 }
00230
00231 function CGPathMoveToPoint(aPath, aTransform, x, y)
00232 {
00233 var point = _CGPointMake(x, y),
00234 count = aPath.count;
00235
00236 if (aTransform != NULL)
00237 point = _CGPointApplyAffineTransform(point, aTransform);
00238
00239 aPath.start = point;
00240 aPath.current = point;
00241
00242 var previous = aPath.elements[count - 1];
00243
00244 if (count != 0 && previous.type == kCGPathElementMoveToPoint)
00245 {
00246 previous.x = point.x;
00247 previous.y = point.y;
00248 }
00249 else
00250 aPath.elements[aPath.count++] = { type:kCGPathElementMoveToPoint, x:point.x, y:point.y };
00251 }
00252
00253 function CGPathWithEllipseInRect(aRect)
00254 {
00255 var path = CGPathCreateMutable();
00256
00257 if (_CGRectGetWidth(aRect) == _CGRectGetHeight(aRect))
00258 CGPathAddArc(path, nil, _CGRectGetMidX(aRect), _CGRectGetMidY(aRect), _CGRectGetWidth(aRect) / 2.0, 0.0, 2 * PI, YES);
00259 else
00260 {
00261 var axis = _CGSizeMake(_CGRectGetWidth(aRect) / 2.0, _CGRectGetHeight(aRect) / 2.0),
00262 center = _CGPointMake(_CGRectGetMinX(aRect) + axis.width, _CGRectGetMinY(aRect) + axis.height);
00263
00264 CGPathMoveToPoint(path, nil, center.x, center.y - axis.height);
00265
00266 CGPathAddCurveToPoint(path, nil, center.x + (KAPPA * axis.width), center.y - axis.height, center.x + axis.width, center.y - (KAPPA * axis.height), center.x + axis.width, center.y);
00267 CGPathAddCurveToPoint(path, nil, center.x + axis.width, center.y + (KAPPA * axis.height), center.x + (KAPPA * axis.width), center.y + axis.height, center.x, center.y + axis.height);
00268 CGPathAddCurveToPoint(path, nil, center.x - (KAPPA * axis.width), center.y + axis.height, center.x - axis.width, center.y + (KAPPA * axis.height), center.x - axis.width, center.y);
00269 CGPathAddCurveToPoint(path, nil, center.x - axis.width, center.y - (KAPPA * axis.height), center.x - (KAPPA * axis.width), center.y - axis.height, center.x, center.y - axis.height);
00270 }
00271
00272 CGPathCloseSubpath(path);
00273
00274 return path;
00275 }
00276
00277 function CGPathWithRoundedRectangleInRect(aRect, xRadius, yRadius, ne, se, sw, nw)
00278 {
00279 var path = CGPathCreateMutable(),
00280 xMin = _CGRectGetMinX(aRect),
00281 xMax = _CGRectGetMaxX(aRect),
00282 yMin = _CGRectGetMinY(aRect),
00283 yMax = _CGRectGetMaxY(aRect);
00284
00285 CGPathMoveToPoint(path, nil, xMin + xRadius, yMin);
00286
00287 if (ne)
00288 {
00289 CGPathAddLineToPoint(path, nil, xMax - xRadius, yMin);
00290 CGPathAddCurveToPoint(path, nil, xMax - xRadius, yMin, xMax, yMin, xMax, yMin + xRadius);
00291 }
00292 else
00293 CGPathAddLineToPoint(path, nil, xMax, yMin);
00294
00295 if (se)
00296 {
00297 CGPathAddLineToPoint(path, nil, xMax, yMax - xRadius);
00298 CGPathAddCurveToPoint(path, nil, xMax, yMax - xRadius, xMax, yMax, xMax - xRadius, yMax);
00299 }
00300 else
00301 CGPathAddLineToPoint(path, nil, xMax, yMax);
00302
00303 if (sw)
00304 {
00305 CGPathAddLineToPoint(path, nil, xMin + xRadius, yMax);
00306 CGPathAddCurveToPoint(path, nil, xMin + xRadius, yMax, xMin, yMax, xMin, yMax - xRadius);
00307 }
00308 else
00309 CGPathAddLineToPoint(path, nil, xMin, yMax);
00310
00311 if (nw)
00312 {
00313 CGPathAddLineToPoint(path, nil, xMin, yMin + xRadius);
00314 CGPathAddCurveToPoint(path, nil, xMin, yMin + xRadius, xMin, yMin, xMin + xRadius, yMin);
00315 } else
00316 CGPathAddLineToPoint(path, nil, xMin, yMin);
00317
00318 CGPathCloseSubpath(path);
00319
00320 return path;
00321 }
00322
00323 function CGPathCloseSubpath(aPath)
00324 {
00325 var count = aPath.count;
00326
00327
00328 if (count == 0 || aPath.elements[count - 1].type == kCGPathElementCloseSubpath)
00329 return;
00330
00331 aPath.elements[aPath.count++] = { type:kCGPathElementCloseSubpath, points:[aPath.start] };
00332 }
00333
00334 function CGPathEqualToPath(aPath, anotherPath)
00335 {
00336 if (aPath == anotherPath)
00337 return YES;
00338
00339 if (aPath.count != anotherPath.count || !_CGPointEqualToPoint(aPath.start, anotherPath.start) || !_CGPointEqualToPoint(aPath.current, anotherPath.current))
00340 return NO;
00341
00342 var i = 0,
00343 count = aPath.count;
00344
00345 for (; i < count; ++i)
00346 {
00347 var element = aPath[i],
00348 anotherElement = anotherPath[i];
00349
00350 if (element.type != anotherElement.type)
00351 return NO;
00352
00353 if ((element.type == kCGPathElementAddArc || element.type == kCGPathElementAddArcToPoint) &&
00354 element.radius != anotherElement.radius)
00355 return NO;
00356
00357 var j = element.points.length;
00358
00359 while (j--)
00360 if (!_CGPointEqualToPoint(element.points[j], anotherElement.points[j]))
00361 return NO;
00362 }
00363
00364 return YES;
00365 }
00366
00367 function CGPathGetCurrentPoint(aPath)
00368 {
00369 return _CGPointCreateCopy(aPath.current);
00370 }
00371
00372 function CGPathIsEmpty(aPath)
00373 {
00374 return !aPath || aPath.count == 0;
00375 }
00376