Created
March 25, 2014 13:55
-
-
Save trengrj/9762352 to your computer and use it in GitHub Desktop.
objective-c example of detecting way points
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// A sample from DrawPane.m/h | |
// Created by John Trengrove | |
// | |
#import <UIKit/UIKit.h> | |
@class Letter; | |
@class Dot; | |
@class AppDelegate; | |
@interface DrawPane : UIView { | |
NSMutableArray *dots; | |
UIBezierPath *bezier; | |
Letter *letter; | |
AppDelegate *appDelegate; | |
Dot *prev1; | |
Dot *prev2; | |
} | |
- (BOOL)goodDrawing; | |
- (void)clearDots; | |
- (void)initialise; | |
- (void)addStartDot:(CGPoint)location; | |
- (void)addDot:(CGPoint)location; | |
CGPoint midPoint(Dot *p1, Dot *p2); | |
@property(readwrite, nonatomic, retain) Letter *letter; | |
@property(nonatomic, retain) NSMutableArray *dots; | |
@property(nonatomic, retain) UIBezierPath *bezier; | |
@property(nonatomic,retain) AppDelegate *appDelegate; | |
@end | |
// ... snip ... | |
// goodDrawing matches an array of Strokes generated from a user | |
// against a set of Guide Points and determines if the Strokes | |
// satisfy the Guide Points. | |
- (BOOL)goodDrawing { | |
int strokeIndex = 0; | |
// User entered dots | |
int dotIndex = 0; | |
int guideDotIndex = 0; | |
Stroke *currentStroke; | |
Dot * guideDot; | |
Dot * dot; | |
// NSLog(@"Detected %d strokes", [self.letter.strokes count]); | |
// Iterate through dot array and attempt to find match | |
while (dotIndex < [self.dots count]) { | |
// get the next guide dot | |
if (strokeIndex < [self.letter.strokes count]) { | |
currentStroke = [self.letter.strokes objectAtIndex:strokeIndex]; | |
if (guideDotIndex < [currentStroke.dots count]) { | |
guideDot = [currentStroke.dots objectAtIndex:guideDotIndex]; | |
// NSLog(@"using guide dot x:%f y:%f",guideDot.x, guideDot.y); | |
} | |
// Move onto next stroke | |
else { | |
strokeIndex += 1; | |
guideDotIndex = 0; | |
// NSLog(@"Stroke index: %d", strokeIndex); | |
continue; | |
} | |
} | |
// Exit FAILURE | |
else { | |
return NO; | |
} | |
dot = [self.dots objectAtIndex:dotIndex]; | |
if ([guideDot contains:dot]) { | |
// NSLog(@"Matched guide dot"); | |
guideDotIndex += 1; | |
// Exit YES if all guide dots have been matched | |
if (guideDotIndex == [currentStroke.dots count] && strokeIndex == [self.letter.strokes count]-1) { | |
return YES; | |
} | |
} | |
dotIndex += 1; | |
} | |
return NO; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment