FAQ
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

WWW
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:

//suunta = direction, z = size of the leaf, filli = color of the leaf, a,b = starting point
void leaf(int a, int b, int filli, int suunta, int z) {
  float a_,b_,c,d,e,f,g,h,i,j;
  stroke(filli);
  int k,l;
  int suunta_ = (suunta)%360;  
 
    a_ = a+z*cos(radians(suunta_));
    b_ = b-z*sin(radians(suunta_));
    k = a+int(z/2*cos(radians(suunta_)));
    l = b-int(z/2*sin(radians(suunta_)));
   
  float pikkutangentti = sqrt(2)*(z/5);
  float isotangentti = sqrt((z/5)*(z/5)+(7*z/10)*(7*z/10));
 
  //just calculating points for the curves  
  suunta_ = suunta-45;
    c = a+pikkutangentti*cos(radians(suunta_));
    d = b-pikkutangentti*sin(radians(suunta_));
  suunta_ = suunta+45;
    e = a+pikkutangentti*cos(radians(suunta_));
    f = b-pikkutangentti*sin(radians(suunta_));
  suunta_ = suunta+16;  
    g = a+isotangentti*cos(radians(suunta_));
    h = b-isotangentti*sin(radians(suunta_));
  suunta_ = suunta-16;
    i = a+isotangentti*cos(radians(suunta_));
    j = b-isotangentti*sin(radians(suunta_));
     
    curve(a, b, c, d, i, j, a_, b_);
    curve(a, b, e, f, g, h, a_, b_);
    filltool(k,l,filli);
}

 
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

WWW
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

WWW
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

WWW
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 

« Previous topic | Next topic »