0

My objective is to create a function named findList() which holds the given parameter of a point and the screen surface.

My aim is to calculate the colour of that point then return a list of the filled colour.

In example, if one had a screen with a red circle in it and the point was inside the red circle, I would like to be able to return a list holding all the points of that circle.

Basically, the point expands, avoiding all other colours and the edge of the screen until it can expand no further; in which then the function returns the list of all the created points.

Here's what I tried:

def findList(point,screen):
    directions=[(0,0)]
    myList=[]
    myList.append(point)
    startColour=screen.get_at(point)
    i=0
    loop=0
    while True:
        loop=loop+1
        print(loop)
        directions=[]
        print("Length of myList: "+str(len(myList)))
        for i in range(len(myList)):
            if myList[i][0]+1<WINDOWWIDTH and screen.get_at((myList[i][0]+1,myList[i [1]))==startColour and myList[i][0]+1 not in myList and myList[i][0]+1 not in directions:
                directions.append((myList[i][0]+1,myList[i][1]))
            if myList[i][0]-1>0 and screen.get_at((myList[i][0]-1,myList[i][1]))==startColour and myList[i][0]-1 not in myList and myList[i][0]-1 not in directions:
                directions.append((myList[i][0]-1,myList[i][1]))
            if myList[i][1]+1<WINDOWHEIGHT and screen.get_at((myList[i][0],myList[i][1]+1))==startColour and myList[i][1]+1 not in myList and myList[i][1]+1 not in directions:
                directions.append((myList[i][0],myList[i][1]+1))
            if myList[i][1]-1>0 and screen.get_at((myList[i][0],myList[i][1]-1))==startColour and myList[i][1]-1 not in myList and myList[i][1]-1 not in directions:
                directions.append((myList[i][0],myList[i][1]-1))

        print(len(directions))
        for i in directions:
            myList.append(i)
        if len(directions)==0:
            break
    print("Exited loop.")
    return myList

I know the coding style is terrible and much of it is probably useless but the general problem is that the function(probably) works but is painstakingly slow and seems to add previously added pixels a lot until my poor little netbook can no longer cope with the 100 000 points it is working with.

It would really help me with my program if someone could make some suggestions for a better function because, being only 13, I am horribly confused with this messy bit of code (note the 4 conditions in the ifs).

The-IT
  • 660
  • 8
  • 19
user2592835
  • 1,547
  • 5
  • 18
  • 26

1 Answers1

0

Essentially, what you're asking for is the first part of the 'Flood-Fill' algorithm, which is what programs like Microsoft Paint uses to fill in an area with a new color. The algorithm first finds all the connected pixels of the same color, then changes their color. Check out the wikipedia page or this stackoverflow question for a python implementation.

As for your code, there are several things that could be improved. I would suggest:

  1. Use a set() instead of a list. This way you will not have to deal with duplicate points
  2. Use if/elif clauses instead of ifs only. This will cut down on computation time.
  3. Your if statements are very complicated but very redundant. You may want to try breaking them up into a nested group of if/elif statements.

Look around for Flood-Fill algorithms and try rewriting what you have. Looking at some other examples might help you streamline your code significantly.

Community
  • 1
  • 1
wflynny
  • 18,065
  • 5
  • 46
  • 67