Cover
 This is the archive Discourse for the Processing (ALPHA) software. Please visit the new Processing forum for current information.
 Processing 1.0 _ALPHA_    Programming Questions & Help    Programs (Moderators: fry, REAS)    help: coloring leaves « Previous topic | Next topic »

 Pages: 1
 Author Topic: help: coloring leaves  (Read 474 times)
Dren

 help: coloring leaves « on: Feb 10th, 2005, 6:30pm »

Program: creates leaves made of two curves() and places them to random position on the canvas with random size, random direction and random color.

Problem: floodfilling doesn't work when leaves are placed on top of each other. For some reason the floodfill-method I've been using stops at the edge of some other leaf (curve) even though it's suppoused to continue (the color of the pixel isn't the desired one).

I tried loading every single leaf to BIimage before placing them on the canvas but it didn't work either.

Coloring the leaves on the canvas seems to be really hard. I was wondering if anyone could help with some other way to color them.

The floodfill methods I tried where similiar to these fjen posted: http://www.florianjenett.de/p55/flood_fill.zip
TomC

 Re: help: coloring leaves « Reply #1 on: Feb 10th, 2005, 6:46pm »

Are you accessing pixels directly or using set, or point?  Are you in 2D or 3D?

Minimal source code which demonstrates the problem will get more help.  (You don't have to give away your ideas or your design if they are precious...)

Have you tried simple filled polygons for the leaves?
Dren

 Re: help: coloring leaves « Reply #2 on: Feb 11th, 2005, 1:19am »

2D and there's really nothing special in the code. Just trigonometry that calculates curve points since the direction of the leaf is random.

Well, here's the method that generates a single leaf:
Code:

And this is the fill-method from fjens link:
Code:
 void filltool(int x, int y, int fill_color) {   int vanha_color = get(x,y);   if( fill_color != vanha_color ) {    fillCoordinate fc = new fillCoordinate( x, y, fill_color );    fillPino fs = new fillPino(fc);    while (fs.index >= 0)    {      fc = fs.pop();      if (get(fc.x,fc.y) != fill_color & get(fc.x,fc.y) == vanha_color)      {     set(fc.x, fc.y, fc.c);           fs.push(new fillCoordinate( fc.x, fc.y-1, fill_color ));     fs.push(new fillCoordinate( fc.x, fc.y+1, fill_color ));       fs.push(new fillCoordinate( fc.x-1, fc.y, fill_color ));     fs.push(new fillCoordinate( fc.x+1, fc.y, fill_color ));      }    }    } }   class fillCoordinate {   int x; int y; int c;       fillCoordinate (int _x, int _y, int _c)   {     x = _x;     y = _y;     c = _c;   }       fillCoordinate copy ()   {     return new fillCoordinate(x, y, c);   } }   class fillPino {   fillCoordinate[] fills;   int MAX_FILLS = 10000; int index;       fillPino( fillCoordinate fc ) {     fills = new fillCoordinate[MAX_FILLS];     index = 0;     fills[index] = fc.copy();   }       void push (fillCoordinate fc)   {     index++;     if (index < MAX_FILLS) {  fills[index] = fc.copy();     }     else {  print("StackOverFlow  ----");  MAX_FILLS += MAX_FILLS;  fillCoordinate[] fTemp = new fillCoordinate[MAX_FILLS];  System.arraycopy(fills, 0, fTemp, 0, fills.length);  fills = fTemp;  fills[index] = fc.copy();  println("New Stack size: " + MAX_FILLS);     }   }       fillCoordinate pop ()   {     fillCoordinate fr = fills[index].copy();     fills[index] = null;     index--;     return fr;   } }

So if you generate several leaves (say, hundreds or thousands f.e.) and they overlap each other, that floodfill wont work. I've been trying to think of another kind of floodfill but the same problem is still there. If leaves overlap eachother it's pretty hard to color them. And since those leaves are made of curves for which I dont have mathematical functions, it's hard to color those leaves pixel by pixel either. And I wouldn't want to go that way anyway, since it makes adding new types of leaves (new kinds of curves and shapes) harder.

I tried some things with BIimage but I couldn't load the product of that method to it. :\
TomC

 Re: help: coloring leaves « Reply #3 on: Feb 11th, 2005, 11:06am »

You say:

Quote:
 For some reason the floodfill-method I've been using stops at the edge of some other leaf (curve) even though it's supposed to continue (the color of the pixel isn't the desired one).

But look closely at this line in the filltool method:

Code:
 if (get(fc.x,fc.y) != fill_color & get(fc.x,fc.y) == vanha_color)

It's only going to fill things which are the same colour as the first point of the flood (vanha_color).  (Shouldn't that be && not a & though?)

You could try taking out the last half of that if statement and see if that helps - I might be wrong, but I think that means it would ignore the current colour of a pixel unless it was already the target colour, and then it would behave how you describe.
fjen

 Re: help: coloring leaves « Reply #4 on: Feb 11th, 2005, 11:11am »

you could give the shapes a try:
Code:
 // instead of the two curve() and the floodfill()      beginShape(POLYGON);  curveVertex(a,  b);  curveVertex(a,  b);  curveVertex(c,  d);    curveVertex(i,  j);    curveVertex(a_,  b_);    curveVertex(g,  h);  curveVertex(e,  f);    curveVertex(a,  b);    curveVertex(a,  b);     endShape();

floodfilling makes sense if you don't know the shape, for example if you are trying to fill a color in a photo. i makes less sense if used the way you did since it evaluates some pixels a couple of times. that's just wasting processor circles ...

btw, the "line-flood-fill" or in my file the "floodFillSimple2" is the most "economic" one in terms of processing pixels ..

/F
fjen

 Re: help: coloring leaves « Reply #5 on: Feb 11th, 2005, 11:14am »

oh, just realized there's a bug in the POLYGON in version .68. it sometimes looses one or two triangles ... but try it anyway and see if that's relevant for you.
Dren

 Re: help: coloring leaves « Reply #6 on: Feb 12th, 2005, 5:16pm »

TomC: ah indeed. thanks.

fjen: that POLYGON-shape was just what I needed.
And loosing some triangle sometimes doesn't matter.

Thanks a lot fellows
 Pages: 1

 Forum Jump: ----------------------------- Discussion -----------------------------  - Community, Collaboration, Status   - Events, Publications, Propaganda   - General Processing Discussion ----------------------------- Programming Questions & Help -----------------------------  - Syntax => Programs   - Integration ----------------------------- Topics & Contributions -----------------------------  - Tools   - Responsive Form, Games   - Information Visualization   - Simulation, Artificial Life   - Tangible Computing   - Automated Systems   - Sound   - Video, Camera   - Beyond Categories ----------------------------- Suggestions -----------------------------  - Software Suggestions   - Website, Reference, Example Suggestions ----------------------------- Bugs -----------------------------  - Software Bugs   - Website, Reference, Example Bugs   - Bug Fixes, Implemented Suggestions ----------------------------- Teaching -----------------------------  - Course Blueprints   - Theory and Practice « Previous topic | Next topic »