Im new to Objective-C so please bear with me. Im in the process of creating an iPhone app which has user drawn on the screen while tracing the shape of letter.
So far, I have the TouchesBegan/Moved/Ended and point/point location to draw onto a buffer on the screen. I have also drawn the letter “A” to the screen using line coordinates:
- (void)drawRect:(CGRect)rect
{
//letter to draw on
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c, 12.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(c, color);
CGContextSetLineCap(c, kCGLineCapRound);
CGContextSetLineJoin(c, kCGLineCapRound);
//1
CGContextMoveToPoint(c, 60, 400);
//2
CGContextAddLineToPoint(c, 160, 100);
//3
CGContextAddLineToPoint(c, 260, 400);
//4
CGContextMoveToPoint(c, 100, 280);
//5
CGContextAddLineToPoint(c, 220, 280);
CGContextStrokePath(c);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
}
The next step that I am trying to figure out is how to see when the user has gone outside the coordinates (or constraints) of the letter which is on the screen. I know I have to find the shortest distance between the point and the line but I dont know how to do this.
Ive also tried to write out some of it in Java (as im more familiar with it) but Im still unsure of whether im taking the correct steps.
static double ERROR_BOUNDS= 0.0;
double dRes = lines.distanceToX(point.location.x, point.location.y);
if (dRes>= ERROR_BOUNDS)
{
Ive really hit a brick wall with this and i’ll post more code if anyone needs it to clarify anything. Any help would be much appreciated.
You might try making the path an object using CGPath logic and then using CGPathContainsPoint to evaluate if the touch was or wasn’t on the path.