Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9187253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:43:25+00:00 2026-06-17T19:43:25+00:00

Im using recursive flood fill algorithm on my ipad painting app and it crashes

  • 0

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?

-(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<1025 && y<705)||(x<1025) ||(y<705)) && (x>0 && y>0) && (offset<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:&r green:&g blue: &b alpha: &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];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:43:26+00:00Added an answer on June 17, 2026 at 7:43 pm

    Breaking the function into the fillRight and fillLeft methods isn’t a long-term solution because if the image gets bigger, overflow may occur again.

    I’d recommend using a faster flood-fill algorithm. The 4-Way flood fill described on Wikipedia is easy to implement, but goes through every point four times.

    I’d recommend using a scanline fill: see http://lodev.org/cgtutor/floodfill.html – Scanline Floodfill Algorithm With Stack. I replaced my 4-way flood fill in my drawing app and it now is much faster and fills a 1024x768px area in less than a second. Of course, the speed may vary depending on your implementation.

    And at last, some notes:

    1.You can use CGPoint to store points in an array.

    CGPoint point=CGPointMake(100, 50); //Declare a point and put a value in it
    

    Then you can get and set the x and y values using point.x and point.y

    2.Use an array to store the points to be checked as deanWombourne suggested.

    NSMutableArray * pointsToCheck=[[NSMutableArray alloc]init];//Initialise the array
    [pointsToCheck addObject:[NSValue valueWithCGPoint:start]];//Assuming you have a CGPoint start, you need to convert it to a NSValue before you put it into the array like [NSValue valueWithCGPoint:point]
    
    while ([pointsToCheck count]>0){//While there are points to be checked:
        NSValue * pointValue=[pointsToCheck objectAtIndex:0];//Get the point: It doesn't matter if you get the first or last item
        [pointsToCheck removeObjectAtIndex:0];//Remove the point from the queue so we won't go through it again
    
        CGPoint point=[pointValue CGPointValue];//Get the CGPoint from the NSValue object
    
        //Implement your algorithm here: check for neighbour points, paint the current point or whatever. I'd recommend  using scanline fill - see above
        //When you need to add a point to the queue (If it was a recursive function, then you'd call the function from itself with different parameters) use:
        [pointsToCheck addObject:[NSValue valueWithCGPoint:newPoint];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've heard that any recursive algorithm can always be expressed by using a stack.
I am trying to solve Knight Tour Problem using recursive Backtracking. Can someone help
I have been using a floodfill algorithm of the stack-based non-recursive variety and it
How can I speed up a recursive code using OpenMP? Basically, I have to
I am using a recursive algorithm to list all possible permutations of elements of
Does anyone know a iterative and efficient algorithm for flood-fill? Or is there any
I wrote a recursive flood fill method in objective c that I am currently
I Have a recursive flood fill of legal neighbours in matrix ( legal neighbour
I'm using a recursive tree of hashmaps, specifically Hashmap map where Object is a
This question is related to this SO post Rather than using a recursive CTE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.