BACKGROUND
I have an application in which the user is able to draw a chart.The user has to draw this chart by swiping on the chart.The chart should draw vertical and horizontal line as per the users swipe.I have got this working.
PROBLEM
I have a UIImageView
on which the drawing is done.My problem is i have to restrict any of the line that the user draws so as to not overlap each other.in simple words i do not want the lines to overlap or cross each other . I have implemented the drawing by using the view touch events like below.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
fromPoint = [touch locationInView:_drawingPad];
/*This is for my segment detection .i need to start from the
middle of any given segment irrespective of the user point
of touch in that segment.*/
if (fromPoint.y < firstSegment.y) {
fromPoint.y = firstSegment.y - 30.5;
}else if (fromPoint.y < seconSegment.y){
fromPoint.y = seconSegment.y - 30.5;
} else if (fromPoint.y < thirdSegment.y){
fromPoint.y = thirdSegment.y - 30.5;
}else{
fromPoint.y = fourthSegment.y - 30.5;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
toPoint = [touch locationInView:_drawingPad];
CGPoint currentPoint = [touch locationInView:_drawingPad];
/*This to restrict lines to only being vertical
and horizontal Code courtesy stack overflow*/
double slope = fabs((toPoint.y - fromPoint.y) / (toPoint.x - fromPoint.x));
if (fromPoint.y == toPoint.y)
{
lineRot = horizontal;//lineRot is an enum
}
if (slope > 1){
lineRot = vertical;
}
else if(slope < 1){
lineRot = horizontal;
}
if (toPoint.y < firstSegment.y) {
toPoint.y = firstSegment.y - 30.5;
}else if (toPoint.y < seconSegment.y){
toPoint.y = seconSegment.y - 30.5;
} else if (toPoint.y < thirdSegment.y){
toPoint.y = thirdSegment.y - 30.5;
}else{
toPoint.y = fourthSegment.y - 30.5;
}
if (toPoint.x < x) {//This is to avoid a back trace,x is initialized to zero in viewDidLoad
return;
} else {
x = toPoint.x;
if (!CGPointEqualToPoint(fromPoint, CGPointZero)) {
UIGraphicsBeginImageContext(_drawingPad.frame.size);
[_drawingPad.image drawInRect:CGRectMake(0, 0, _drawingPad.frame.size.width, _drawingPad.frame.size.height)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context,fromPoint.x, fromPoint.y);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 1.0, 1.0);
if (lineRot == horizontal) {
CGContextAddLineToPoint(context, toPoint.x, fromPoint.y);
} else{
CGContextAddLineToPoint(context, fromPoint.x, toPoint.y);
}
CGContextClosePath(context);
CGContextStrokePath(context);
_drawingPad.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
}
What I have Tried
Unfortunately nothing.The fact is i do not know where to begin. I saw this Hit detection when drawing lines in iOS but couldn't get what was going on and how to implement.Any help is highly appreciated