title: Recursive flood fill on an iPad paint app
body: <p>Im using recursive flood fill algorithm on my ipad painting app and it crashes  with stack overflow i think. Can someone help me solve this problem with example code or good advice because im a noob?</p>
  
  <pre><code>-(void)paintingBucket:(int)point point2:(int)point2 width:(int)width colorAtPoint:(UIColor *)color {
  
  int offset = 0;
  int x = point;
  int y = point2;
  offset = 4*((width*round(y))+round(x));
  
  if (((x&lt;1025 &amp;&amp; y&lt;705)||(x&lt;1025) ||(y&lt;705)) &amp;&amp; (x&gt;0 &amp;&amp; y&gt;0) &amp;&amp; (offset&lt;2887648)) {
  
      int alpha = data[offset];
      int red = data[offset + 1];
      int green = data[offset + 2];
      int blue = data[offset + 3];
      color1 = [UIColor colorWithRed:(green/255.0f) green:(red/255.0f) blue:(alpha/255.0f) alpha:(blue/255.0f)];
  
          if (![color1 isEqual: color] ) {
              return;
          } else {
              color3 = self.currentColor ;
              CGFloat r,g,b,a;
              [color3 getRed:&amp;r green:&amp;g blue: &amp;b alpha: &amp;a];
              int reda = (int)(255.0 * r);
              int greena = (int)(255.0 * g);
              int bluea = (int)(255.0 * b);
              int alphaa = (int)(255.0 * a);
              // NSLog(@" red: %u green: %u blue: %u alpha: %u", reda, greena, bluea, alphaa);
  
              data[offset + 3] = alphaa;
              data[offset + 2] = reda;
              data[offset + 1] = greena;
              data[offset] = bluea;
          }
      }
  
      [self paintingBucket:x+1 point2:y    width:width colorAtPoint:color];
      [self paintingBucket:x-1 point2:y    width:width colorAtPoint:color];
      [self paintingBucket:x   point2:y+1  width:width colorAtPoint:color];
      [self paintingBucket:x   point2:y-1  width:width colorAtPoint:color];
  }
  </code></pre>