4 Sided Shape drawing function

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: 4 Sided Shape drawing function

Post by LuX » July 3rd, 2012, 8:07 am

I figured there must be a way to perform the function using allocated memory, but I just made all the variables that I use in the function to a struct and then just push that around to go around my problem.

I managed to get it fill about 3/4 of my screen, but then I still get overflow : -/
ʕ •ᴥ•ʔ

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: 4 Sided Shape drawing function

Post by chili » July 3rd, 2012, 8:08 am

I bet you are using recursion aren't you LuX? ;)
Chili

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: 4 Sided Shape drawing function

Post by LuX » July 3rd, 2012, 8:16 am

I don't know, didn't look up the algorithm directly. I just watched the example of flood fill on wikipedia and snapped my fingers and did it all by my self : -D

http://upload.wikimedia.org/wikipedia/c ... aka%29.gif

Basically a function that draws at four points and uses itself again untill it cant be called again...
ʕ •ᴥ•ʔ

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: 4 Sided Shape drawing function

Post by LuX » July 3rd, 2012, 8:35 am

I guess I could try to make that right hand fill method, looks a tad faster and doesn't root in the function too deeply.
ʕ •ᴥ•ʔ

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: 4 Sided Shape drawing function

Post by Asimov » July 3rd, 2012, 10:16 am

Hi Lux,

I wouldn't have thought you could put a function on the heap, but I imagine you could load all your nodes or cooridnates into a memory location which could be on a heap.

Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: 4 Sided Shape drawing function

Post by chili » July 3rd, 2012, 12:50 pm

Even if you did that, you would still be using stack memory. Every time you call a function, at the very least the return address for that call is pushed onto the stack. In addition, only local variable for the function are also pushed (this includes the function parameters). The only real solution is to change your algorithm to reduce the depth of recursion, or better yet, convert the recursion into a loop. You can also simulate recursion in a loop by creating your own stack on the heap. In that case you can make it as big as you like.
Chili

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: 4 Sided Shape drawing function

Post by Asimov » July 3rd, 2012, 6:32 pm

Hi Chilli,

Could you explain recursion. It sounds like a loop within a loop or something,

Or it sounds VERY bad LOL.

Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: 4 Sided Shape drawing function

Post by LuX » July 3rd, 2012, 7:01 pm

Simple, You have four points like a "+" for each four points you check if the bottom pixel is equal to sample pixel you want to fill, if it is, fill it and spawn a new "+" there. Basically it will go on until it cant find new points to spawn at.
Nodes.png
Nodes.png (1.53 KiB) Viewed 3237 times
Each new color is a new node being drawn at all four points of the original red one, as best as they can be. Obviously they would also spawn more nodes on each point, but for this example, its one (red) node performing all its roots.

There might be a better way to direct the spawning, so it wouldn't root down too badly, that's the problem I'm having. Chili said we will be going over something like this eventually. Or maybe another way where we scan lines. I tried to look that up, but all the videos on youtube were some Indian professors.... The accent...
ʕ •ᴥ•ʔ

User avatar
codinitup
Posts: 112
Joined: June 27th, 2012, 7:43 am

Re: 4 Sided Shape drawing function

Post by codinitup » July 4th, 2012, 12:15 am

Asimov wrote:Hi Chilli,

Could you explain recursion. It sounds like a loop within a loop or something,

Or it sounds VERY bad LOL.

Asimov
To sum up what recursion is, it is basicly a function that can call itself, for example

Code: Select all

# include<stdio.h>
int factorial(unsigned int number)
{
    if(number <= 1)
        return 1;
    return number * factorial(number - 1);
}
void main()
{
    int x = 5;
    printf("factorial of %d is %d",x,factorial(x));
In this example factorial(unsigned int number) is calling itself.
MOOOOOO

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: 4 Sided Shape drawing function

Post by Asimov » July 4th, 2012, 12:23 am

Hi codinitup,

Ah thank you. So it is like the Mr No where man in the yellow submarine swallowing himself.

To be honest I think there could be better ways of writing the same routine without using recursion. Recursion sounds too much like a forever goto loop for my liking LOL, with a nasty break; command to get out of the loop heh heh.


Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

Post Reply