the field component.x is not visible

Hey guys, im a beginner in processing. need help very urgently, im trying to use voronoi code and trying to make the points move. I did manage to make the voronoi but im not sure how to make the points move. below is my code , please reply soon :) thanks

Cell[] cellCollection = new Cell[200];

void setup()
{
  size(400, 400);
  for(int i = 0; i < 50; ++i) {
    cells.add(new Cell((int)random(0,width), (int)random(0,height), color(random(0, 255))));
  }
  smooth();
}

void draw()
{
  cells.get(0).loc.x = mouseX;
  cells.get(0).loc.y = mouseY;
  background(0);

  // Draw the voronoi cells
  drawVoronoi();

  // Draw the points
  for(int i = 0; i < cells.size(); ++i) {
    ellipse(cells.get(i).loc.x, cells.get(i).loc.y, 5, 5);
  }
}

void keyPressed()
{
  if(key == 'a')
    cells.add(new Cell(mouseX, mouseY, color(random(0,255), random(0,255), random(0,255))));
  if(key == ' ')
    cells.subList(5, 10).clear();
  if(key == 't')
    use_manhattan = !use_manhattan;
}

class Cell
{ 
  int x;

  Cell(int x) {
    this.x = x;
  }

  Ball(int_x, int_y, int_h, int_w, int_xspeed, int_yspeed) {
   _h = h;
   _w = w;
   _x = x;
   _y = y;
   _xspeed = xspeed;
   _yspeed = yspeed;
  }

  PVector loc;
  color c;
  Cell(int _x, int _y, color _c)
  {
    loc = new PVector(_x, _y);
    c = _c;
  }
}

ArrayList<Cell> cells = new ArrayList<Cell>();
boolean use_manhattan = false;

void drawVoronoi()
{
  loadPixels();
  int offset = 0;

  // Iterate over every pixel
  for(int y = 0; y < height; y++)
  {
    for(int x = 0; x < width; x++)
    {
      float shortest = 1E12;
      int index = -1;

      // Find the closest cell
      for(int i = 0; i < cells.size(); ++i)
      {
        Cell cc = cells.get(i);
        float d;


        if(use_manhattan)
        {
          // Manhattan Distance
          d = abs(x - cc.loc.x) + abs(y - cc.loc.y);
        }
        else
        {
          // Euclidean distance, dont need to sqrt it since actual distance isnt important
          d = sq(x-cc.loc.x) + sq(y-cc.loc.y);
        }

        if(d < shortest)
        {
          shortest = d;
          index = i;
        }
      }
      // Set the pixel to the cells color
      pixels[offset++] = cells.get(index).c;
    }
  }
  updatePixels();
}

Answers

  • edited May 2014

    you have x but probably mean cc.x

    line 89, 94

    also in the class

    you got one x and loc (which also has an x: loc.x) - duplicate? Confusing?

    also in the class

    also in the class this is not in use:

      Ball(int_x, int_y, int_h, int_w, int_xspeed, int_yspeed) {
       _h = h;
       _w = w;
       _x = x;
       _y = y;
       _xspeed = xspeed;
       _yspeed = yspeed;
      }
    

    Advice

    I would also advice to place the properties of the class in one place at the beginning of the class.

    Also the global vars of the sketch at the beginning.

    Now you have

    Cell[] cellCollection = new Cell[200];

    and

    ArrayList<Cell> cells = new ArrayList<Cell>();
    boolean use_manhattan = false;
    

    separately. Are they the same?

    ;-)

Sign In or Register to comment.