title: Xcode - User drawn lines collision detection
body: <p>Im making a game in which a UIImageView(ball) randomly bounces around the edges of the iphone screen. The user then must draw lines with their finger to guide the ball into a hole/target. I have managed to implement the randomly bouncing ball and the ability to draw onto the screen. My problem is that I cant for the life of me get a collision detected between the ball and the user drawn line. The ball just passes through it. I have tried to use a CGRectIntersectsRect but i'm pretty sure that this method wont work. What i'm basically wanting is some way of getting the ball to bounce off of the user drawn line. If anyone has any idea on how to do this and could help I would be very grateful. </p>
  
  <p>Thanks in advance </p>
  
  <p>Ive added some code to try and help you guys understand what my set up is.</p>
  
  <p>touches began is where the user first puts their finger on the screen</p>
  
  <pre><code>- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
  
  fingerSwiped = NO;
  UITouch *touch = [touches anyObject];
  lastPoint = [touch locationInView:self.view];
  startPoint = [touch locationInView:self.view];
  lastPoint.y -= 0;
  
  }
  </code></pre>
  
  <p>touches moved is when the finger is moved across the screen</p>
  
  <pre><code>-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  
  fingerSwiped = YES;
  UITouch *touch = [touches anyObject];
  CGPoint currentPoint = [touch locationInView:self.view];
  currentPoint.y -= 0;
  UIGraphicsBeginImageContext(self.view.frame.size);
  
  [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width,                    self.view.frame.size.height)];
  
  CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
  
  // sets the width of the line
  CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
  //sets the colour of the line drawn
  
  //red
  //CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
  //green
  //CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 1.0, 0.0, 1.0);
  //blue
  //CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 1.0, 1.0);
  //black
  CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
  CGContextBeginPath(UIGraphicsGetCurrentContext());
  CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
  //draws a line to the point where the user has touched the screen
  CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
  CGContextStrokePath(UIGraphicsGetCurrentContext());
  drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  
  lastPoint = currentPoint;
  
  fingerMoved++;
  
  }
  </code></pre>
  
  <p>Heres what i'm currently trying to use for the actual collision detection...</p>
  
  <pre><code>float a = lastPoint.y - startPoint.y;
  float b = startPoint.x - lastPoint.x;
  float c = lastPoint.x * startPoint.y - startPoint.x * lastPoint.y;
  float d = abs(a * ball.center.x + b *ball.center.y + c)/sqrtf(powf(a,2)+ powf(b,2));
  
  if(d ==0){
      NSLog(@"collision detected");
  
  }
  </code></pre>