Permalink
Browse files

Add another custom shape named "arc"

  • Loading branch information...
1 parent badf32d commit bde777aea31454f8991feee1a37b52492dc161c4 @ole committed Feb 3, 2012
Showing with 29 additions and 0 deletions.
  1. +1 −0 PathHitTesting/Shape.h
  2. +28 −0 PathHitTesting/Shape.m
@@ -12,6 +12,7 @@ typedef enum {
ShapeTypeRect,
ShapeTypeEllipse,
ShapeTypeHouse,
+ ShapeTypeArc,
SHAPE_TYPE_COUNT
} ShapeType;
@@ -19,6 +19,7 @@ + (CGRect)randomRectInBounds:(CGRect)maxBounds;
+ (UIColor *)randomColor;
+ (CGFloat)randomLineWidth;
+ (UIBezierPath *)houseInRect:(CGRect)bounds;
++ (UIBezierPath *)arcInRect:(CGRect)bounds;
@end
@@ -46,6 +47,9 @@ + (id)randomShapeInBounds:(CGRect)maxBounds
case ShapeTypeHouse:
path = [self houseInRect:bounds];
break;
+ case ShapeTypeArc:
+ path = [self arcInRect:bounds];
+ break;
default:
path = [UIBezierPath bezierPathWithRect:bounds];
break;
@@ -222,4 +226,28 @@ + (UIBezierPath *)houseInRect:(CGRect)bounds
return path;
}
++ (UIBezierPath *)arcInRect:(CGRect)bounds
+{
+ CGPoint center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
+ CGPoint centerRight = CGPointMake(CGRectGetMaxX(bounds), CGRectGetMidY(bounds));
+ CGFloat radius = CGRectGetWidth(bounds) / 2.0f;
+ CGPoint center2 = CGPointMake(center.x, center.y - radius / 2.0f);
+
+ UIBezierPath *path = [UIBezierPath bezierPath];
+ [path moveToPoint:centerRight];
+ [path addArcWithCenter:center radius:radius startAngle:0.0f endAngle:M_PI * 1.5f clockwise:YES];
+ [path addArcWithCenter:center2 radius:radius / 2.0f startAngle:M_PI * 1.5f endAngle:M_PI clockwise:YES];
+
+ path.lineJoinStyle = kCGLineJoinRound;
+
+ CGAffineTransform transform = CGAffineTransformIdentity;
+ transform = CGAffineTransformTranslate(transform, bounds.origin.x, bounds.origin.y);
+ transform = CGAffineTransformTranslate(transform, 0.0, bounds.size.height);
+ transform = CGAffineTransformScale(transform, 1.0f, -1.0f);
+ transform = CGAffineTransformTranslate(transform, -bounds.origin.x, -bounds.origin.y);
+ [path applyTransform:transform];
+
+ return path;
+}
+
@end

0 comments on commit bde777a

Please sign in to comment.