Static reference to the non-static method

Hi, I’ve been reading about The Salesman Problem and I’ve seen TSP art, which I’m interested in creating.

I found an existing sketch in gihub and I’m trying to adapt it to randomly read pixels from a black and white image and if the pixel is black it would add it to the “cities” array.

I’m getting the following error:
“Cannot make a static reference to the non-static method add(PVector) from the type PVector”
Is there any other way to add an x/y value to a PVector?

Thanks in advance.


PVector[] cities;
int no_of_cities=100;
PImage img;
int xc, yc;
color c;
float recorddistance;
PVector[] bestever;



void setup()
{
  size(1024,767);
  
  img = loadImage("test.png");
  //image(img, 0, 0);
  cities = new PVector[no_of_cities];
  bestever = new PVector[no_of_cities];
  for(int i=0; i<no_of_cities; )
  {
    xc=int(random(width));
    yc=int(random(height));
    
    c = img.get(xc,yc);
    println(xc,yc,brightness(c),i);
    PVector v = new PVector(xc, yc);  //create points at random position
      if (brightness(c) < 10){
        cities = PVector.add(v);  //and store it in an array
        i++;
        }  
 
}
//print(cities); 
 }
 
 void draw()
{
  
  background(255);
  for(int i=0; i<no_of_cities-1; i++)
  {
    ellipse(cities[i].x,cities[i].y,4,4);  //plot the points
  }
}

Hi @PixelMx,

I think the add() method adds the vector component, not the vector to the array:

Instead of an array, why not use an arrayList?

That way you can append/add elements to the arrayList. A small example is found below:

So your sketch would become something like this:

ArrayList<PVector> cities;
int no_of_cities=100;
PImage img;
int xc, yc;
color c;
float recorddistance;
PVector[] bestever;



void setup()
{
  size(1024, 767);

  img = loadImage("test.png");
  //image(img, 0, 0);
  cities = new ArrayList<PVector>(no_of_cities);
  bestever = new PVector[no_of_cities];
  for (int i=0; i<no_of_cities; )
  {
    xc=int(random(width));
    yc=int(random(height));

    c = img.get(xc, yc);
    println(xc, yc, brightness(c), i);
    PVector v = new PVector(xc, yc);  //create points at random position
    if (brightness(c) < 10) {
      cities.add(v);  //and store it in an array
      i++;
    }
  }
  //print(cities);
}

void draw()
{

  background(255);
  for (int i=0; i<no_of_cities-1; i++)
  {
    ellipse(cities.get(i).x, cities.get(i).y, 4, 4);  //plot the points
  }
}

I think that you can still improve your sketch using pixels to retrieve all pixels of the image and then randomly select which ones you want :slight_smile:

Hope it helps! :slight_smile:

1 Like

Thanks a lot @PhySan. This worked perfectly!

1 Like