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)
   Triangles and Quads not drawing properly
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Triangles and Quads not drawing properly  (Read 688 times)
miki

Email
Triangles and Quads not drawing properly
« on: Apr 7th, 2003, 4:49pm »

Hi there,
 
I've just started using proce55ing and am having lots of fun with it!
But I'm a bit puzzled about a bug in my "pixelate" program (below). It draws my triangles and quads without a fill colour! Rectangles and ellipses work fine though.  
 
Can anyone help?
 
Thanks in advance!
 
Miki
 
Code:
//"Pixelate" by Miki Grahame, April 2003
//Press any key to do the next iteration
 
int squaresize=400;
BImage a;
int[] aPixels = new int[squaresize*squaresize];
//int reps;
int level = 0;  //starting level for iteration
boolean strokes = false; //If true, draw strokes. //play with this!
int shape = 2; //0=rectangles, 1=circles, 2=triangles, 3=quads    //play with this!
 
void setup()  
{
  //colorMode(RGB);  //weird window shrinkage bug when I put this in
  size(squaresize, squaresize);
  noBackground();
  if (!strokes) noStroke();    
  a = loadImage("uni2_400.jpg");
  for(int i=0; i<width*height; i++) {
    aPixels[i] = a.pixels[i];
  }
   
  //do first iteration
  pixelate(level);
}
 
void loop()  
{
  if (keyPressed)
  {
    level++;
    pixelate(level);  
    delay(100);  //Small delay to avoid repeats
  }
}
 
void pixelate(int input_level)  
//main function. does one pass of "pixelating" the image
{
  //NB currently working will square input images, so assuming width=height  
  //  to simplify things
  int reps = int(pow(2,input_level)); //num of repetitions along height/width of image.  
  //total number of "pixels" to be drawn will be reps*reps
  int f = int(width/reps); //focus width - width/height of each output "pixel"
  if (f==0) {level=1; return;} //if the focus width has got so small, go back to starting level
  if (f<reps) reps = int(width/f); //if f is small, recalculate reps to make more of the full picture width
   
  //Loop to create each output "pixel". (i corresponds to x, j corresponds to y)
   for (int i=0; i<reps; i++)
   {
     for (int j=0; j<reps; j++)
     {
  float collect_red = 0;  //store the sum of all red components
  float collect_green = 0; //store the sum of all green components
  float collect_blue = 0; //store the sum of all blue components
   
  //loop through all the pixels of the input image that are within the current focus area
  //  and calculate the average rgb value for that area.
  //(The current focus area is a square of width f, with top corner at coord (i*f, j*f)  
  for (int jj=0; jj<f; jj++)
  {
     for (int ii=0; ii<f; ii++)
     {
  collect_red = collect_red + red(aPixels[(j*f+jj)*width+(i*f+ii)]);
  collect_green = collect_green + green(aPixels[(j*f+jj)*width+(i*f+ii)]);
  collect_blue = collect_blue + blue(aPixels[(j*f+jj)*width+(i*f+ii)]);
      }
   }
  int avg_red = int(collect_red/(f*f));
  int avg_green = int(collect_green/(f*f));
  int avg_blue = int(collect_blue/(f*f));
  fill( avg_red, avg_green, avg_blue);   //Set fill colour to the average colour of the focus area
   
  //Some stuff to make the output "pixels" more random
  int max_offset = 8;  //Play with this!
  int x_offset = int(random(-max_offset,max_offset));
  int y_offset = int(random(-max_offset,max_offset));
  int max_size_offset = 7; //Play with this!
  int min_size_offset = -4;    //Play with this!
  int w_offset = int(random(min_size_offset,max_size_offset));
  int h_offset = int(random(min_size_offset,max_size_offset));
   
  if (strokes)
  {
    //Set the stroke colour to the colour of the on-screen pixel in the middle of the current focus area  
    color stroke_color;
    int ax = (i*f+int(f/2));
    int ay = (j*f+int(f/2));
   
    stroke_color = getPixel(ax, ay);
    stroke(stroke_color);
    if (f<2) noStroke();  //if the "pixel" size is so small don't stroke
  }
   
  //Draw shape
  if (shape == 0)
  {
    //Rectangle
    rect(i*f+x_offset, j*f+y_offset, f+w_offset, f+h_offset);
  }
  else if (shape == 1)
  {
    //Ellipse
    ellipse(i*f+x_offset, j*f+y_offset, f+w_offset, f+h_offset);
  }
  else if (shape == 2)
  {
    //Triangle
    int t1x = i*f+x_offset;
    int t1y = (j+1)*f+y_offset+h_offset;
    int t2x = (i+1)*f+x_offset+w_offset;
    int t2y = (j+1)*f+y_offset+h_offset;
    int t3x = int((t1x+t2x)/2);
    int t3y = j*f+y_offset;
    fill(150);
    stroke(0);
    triangle(t1x, t1y, t2x, t2y, t3x, t3y);
    //** WHY DOESNT THIS WORK? **
  }
  else if (shape == 3)
  {
    //Quad
    int q1x = i*f+x_offset;
    int q1y = j*f+y_offset+f+h_offset;
    int q2x = i*f+x_offset;
    int q2y = j*f+y_offset;
    int q3x = i*f+x_offset+f+w_offset;
    int q3y = j*f+y_offset;
    int q4x = i*f+x_offset+f+w_offset;
    int q4y = j*f+y_offset+f+h_offset;
    stroke(0);
    fill(150);
    quad(q1x, q1y, q2x, q2y, q3x, q3y, q4x, q4y);
    //** WHY DOESNT THIS WORK? **
  }
 }
    }
 
}
 
Pages: 1 

« Previous topic | Next topic »