0

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

Community
  • 1
  • 1
Prathamesh Saraf
  • 709
  • 4
  • 14
  • so if the user swipes up/down you would draw a vertical line in the corresponding segment, same if the user swipes left/right you would draw a horizontal line in the corresponding segment? So the horizontal lines would never cross each other, and the vertical lines would never cross each other. The question then is, if i understand it correctly: A way to detect if the new vertical line would cross a horizontal line and vice versa? – Danilo Jul 05 '13 at 13:50
  • firstly,sorry for the late reply.The user can draw multiple horizontal lines on a given segment,starting from any point on the segment to any point.thus he can draw two distinct horizontal lines in a seg and a third one in the middle and extend it to the second line.I was to avoid this.also as you understood correctly,avoid vert and horizontal lines to cross each other. – Prathamesh Saraf Jul 09 '13 at 06:06

0 Answers0