Skip to content
Browse files

Add Shape class (incl. random Shape factory)

  • Loading branch information...
1 parent 401e7b7 commit 30c46627ec2efd36658682f6cd3b661be32924c2 @ole committed
Showing with 152 additions and 0 deletions.
  1. +6 −0 PathHitTesting.xcodeproj/project.pbxproj
  2. +27 −0 PathHitTesting/Shape.h
  3. +119 −0 PathHitTesting/Shape.m
View
6 PathHitTesting.xcodeproj/project.pbxproj
@@ -16,6 +16,7 @@
5D892D4314D6F72C005B3273 /* MainStoryboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5D892D4114D6F72C005B3273 /* MainStoryboard.storyboard */; };
5D892D4614D6F72C005B3273 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D892D4514D6F72C005B3273 /* ViewController.m */; };
5D892D4E14D6F77E005B3273 /* DrawingView.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D892D4D14D6F77E005B3273 /* DrawingView.m */; };
+ 5D892D5414D70066005B3273 /* Shape.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D892D5314D70066005B3273 /* Shape.m */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@@ -34,6 +35,8 @@
5D892D4514D6F72C005B3273 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
5D892D4C14D6F77E005B3273 /* DrawingView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DrawingView.h; sourceTree = "<group>"; };
5D892D4D14D6F77E005B3273 /* DrawingView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DrawingView.m; sourceTree = "<group>"; };
+ 5D892D5214D70066005B3273 /* Shape.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Shape.h; sourceTree = "<group>"; };
+ 5D892D5314D70066005B3273 /* Shape.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Shape.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -88,6 +91,8 @@
5D892D3614D6F72B005B3273 /* Supporting Files */,
5D892D4C14D6F77E005B3273 /* DrawingView.h */,
5D892D4D14D6F77E005B3273 /* DrawingView.m */,
+ 5D892D5214D70066005B3273 /* Shape.h */,
+ 5D892D5314D70066005B3273 /* Shape.m */,
);
path = PathHitTesting;
sourceTree = "<group>";
@@ -170,6 +175,7 @@
5D892D4014D6F72C005B3273 /* AppDelegate.m in Sources */,
5D892D4614D6F72C005B3273 /* ViewController.m in Sources */,
5D892D4E14D6F77E005B3273 /* DrawingView.m in Sources */,
+ 5D892D5414D70066005B3273 /* Shape.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
27 PathHitTesting/Shape.h
@@ -0,0 +1,27 @@
+//
+// Shape.h
+// PathHitTesting
+//
+// Created by Ole Begemann on 30.01.12.
+// Copyright (c) 2012 Ole Begemann. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+typedef enum {
+ ShapeTypeRect = 1,
+ ShapeTypeEllipse,
+ SHAPE_TYPE_COUNT
+} ShapeType;
+
+
+@interface Shape : NSObject
+
++ (id)randomShapeInBounds:(CGRect)maxBounds;
++ (id)shapeWithPath:(UIBezierPath *)path lineColor:(UIColor *)lineColor;
+- (id)initWithPath:(UIBezierPath *)path lineColor:(UIColor *)lineColor;
+
+@property (nonatomic, strong) UIBezierPath *path;
+@property (nonatomic, strong) UIColor *lineColor;
+
+@end
View
119 PathHitTesting/Shape.m
@@ -0,0 +1,119 @@
+//
+// Shape.m
+// PathHitTesting
+//
+// Created by Ole Begemann on 30.01.12.
+// Copyright (c) 2012 Ole Begemann. All rights reserved.
+//
+
+#import "Shape.h"
+
+@interface Shape ()
+
++ (ShapeType)randomShapeType;
++ (CGRect)randomRectInBounds:(CGRect)maxBounds;
++ (UIColor *)randomColor;
++ (CGFloat)randomLineWidth;
+
+@end
+
+
+@implementation Shape
+
+@synthesize path = _path;
+@synthesize lineColor = _lineColor;
+
++ (id)randomShapeInBounds:(CGRect)maxBounds
+{
+ UIBezierPath *path = nil;
+ CGRect bounds = [self randomRectInBounds:maxBounds];
+ ShapeType type = [self randomShapeType];
+ switch (type) {
+ case ShapeTypeRect:
+ path = [UIBezierPath bezierPathWithRect:bounds];
+ break;
+ case ShapeTypeEllipse:
+ path = [UIBezierPath bezierPathWithOvalInRect:bounds];
+ break;
+ default:
+ path = [UIBezierPath bezierPathWithRect:bounds];
+ break;
+ }
+
+ path.lineWidth = [self randomLineWidth];
+ UIColor *lineColor = [self randomColor];
+
+ return [[self alloc] initWithPath:path lineColor:lineColor];
+}
+
++ (id)shapeWithPath:(UIBezierPath *)path lineColor:(UIColor *)lineColor
+{
+ return [[self alloc] initWithPath:path lineColor:lineColor];
+}
+
+- (id)initWithPath:(UIBezierPath *)path lineColor:(UIColor *)lineColor
+{
+ self = [super init];
+ if (self != nil) {
+ _path = path;
+ _lineColor = lineColor;
+ }
+ return self;
+}
+
+- (id)init
+{
+ UIBezierPath *defaultPath = [UIBezierPath bezierPathWithRect:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
+ UIColor *defaultLineColor = [UIColor blackColor];
+ return [self initWithPath:defaultPath lineColor:defaultLineColor];
+}
+
+
++ (ShapeType)randomShapeType
+{
+ return arc4random_uniform(SHAPE_TYPE_COUNT);
+}
+
++ (CGRect)randomRectInBounds:(CGRect)maxBounds
+{
+ uint32_t minWidth = 44;
+ uint32_t minHeight = 44;
+
+ CGRect normalizedBounds = CGRectStandardize(maxBounds);
+ uint32_t minOriginX = normalizedBounds.origin.x;
+ uint32_t minOriginY = normalizedBounds.origin.y;
+ uint32_t maxWidth = normalizedBounds.size.width;
+ uint32_t maxHeight = normalizedBounds.size.height;
+
+ uint32_t originX = arc4random_uniform(maxWidth - minWidth) + minOriginX;
+ uint32_t originY = arc4random_uniform(maxHeight - minHeight) + minOriginY;
+ uint32_t width = arc4random_uniform(maxWidth - minWidth - originX) + minWidth;
+ uint32_t height = arc4random_uniform(maxHeight - minHeight - originY) + minHeight;
+
+ return CGRectMake(originX, originY, width, height);
+}
+
++ (UIColor *)randomColor
+{
+ NSArray *colors = [NSArray arrayWithObjects:
+ [UIColor blueColor],
+ [UIColor redColor],
+ [UIColor greenColor],
+ [UIColor yellowColor],
+ [UIColor magentaColor],
+ [UIColor brownColor],
+ [UIColor purpleColor],
+ [UIColor orangeColor],
+ nil];
+ uint32_t colorIndex = arc4random_uniform([colors count]);
+ return [colors objectAtIndex:colorIndex];
+}
+
++ (CGFloat)randomLineWidth
+{
+ uint32_t maxLineWidth = 15;
+ CGFloat lineWidth = arc4random_uniform(maxLineWidth) + 1.0f; // avoid lineWidth == 0.0f
+ return lineWidth;
+}
+
+@end

0 comments on commit 30c4662

Please sign in to comment.
Something went wrong with that request. Please try again.