LinkedList vs ArrayList

I’ve added a working Rosetta task here.
I want to add this code to OpenProcessing. Since imports there don’t work I’m trying to solve it this way. But maybe I’ve the whole Queue thing wrong, because now I can’t pass

if (test_tolerance) println(“Passed test_tolerance”);

which has nothing to do with the Queue, and works perfectly well in the original code. I hope you can look into this code, because my head is spinning. (An image here.)

PImage img;
int tolerance;
color fill_color;
boolean allowed;

void setup() {
  size(600, 400);
  img = loadImage("image.png");
  fill_color = color(250, 0, 0);  
  fill(0, 0, 100);
  tolerance = 15;
  image(img, 0, 0, width, height);
  textSize(18);
  text("Tolerance = "+tolerance+"  (Use mouse wheel to change)", 100, height-30);
  text("Right click to reset", 100, height-10);
}

void draw() { 
  if (allowed) {
    image(img, 0, 0, width, height);
    text("Tolerance = "+tolerance+"  (Use mouse wheel to change)", 100, height-30);
    text("Right click to reset", 100, height-10);
    allowed = false;
  }
}

void mousePressed() {
  img.loadPixels(); // A continious bitmap array with no reference to width
  flood(mouseX, mouseY);
  img.updatePixels(); 
  allowed = true;
  if (mouseButton == RIGHT) img = loadImage("image.png");
}

void mouseWheel(MouseEvent event) {
  float e = event.getCount();
  tolerance += 2*e;
  if (tolerance > 256) tolerance = 256;
  if (tolerance < 0) tolerance = 0;
  allowed = true;
}


void flood(int x, int y) {
  color target_color = img.pixels[pixel_position(mouseX, mouseY)];   
  boolean[][]p_control = new boolean[img.height][img.width];
  PVectorQueue queue = new PVectorQueue();
  float i = 0;
  queue.add(new PVector(i, i));
  while (!queue.isEmpty()) {
    println(queue.peek());
    PVector p = queue.remove();
      if (check(p_control, int(p.x), int(p.y), target_color)) {     
      println("check");
      queue.add(new PVector(p.x, p.y-1)); 
      queue.add(new PVector(p.x, p.y+1)); 
      queue.add(new PVector(p.x-1, p.y)); 
      queue.add(new PVector(p.x+1, p.y));
      //println(queue.peek());
    }
  }
}

int pixel_position(int x, int y) { // Returns position of clicked coordinates width / height of the image 
  return x + (y * img.width);
}

boolean check(boolean[][]p_control, int x, int y, color target_color) {
  if (x < 0 || y < 0 || y >= img.height || x >= img.width || p_control[y][x]) return false;
  int pp = img.pixels[pixel_position(x, y)];
  boolean test_tolerance = (abs(green(target_color)-green(pp)) < tolerance
                         && abs(  red(target_color)-  red(pp)) < tolerance
                         && abs( blue(target_color)- blue(pp)) < tolerance);
  if (test_tolerance) println("Passed test_tolerance");
  if (!test_tolerance) return false; 
  img.pixels[pixel_position(x, y)] = fill_color;
  p_control[y][x] = true;
  return true;
}

public class PVectorQueue {
  int front; 
  ArrayList<PVector> myArray = new ArrayList<PVector>();

  PVectorQueue() {
    front = 0;
  }

  public PVector add(PVector pv) {
    myArray.add(pv);
    return pv;
  }

  public PVector remove() {
    PVector res = myArray.get(front);
    myArray.remove(0);    
    return res;
  } 

  public PVector peek() {
    if (isEmpty()) {
      println("UnderFlow\nProgram Terminated");
      exit();
    } 
    return (PVector) myArray.get(0);
  }

  public int size() {
    return myArray.size();
  }

  public Boolean isEmpty() {
    return (size() == 0);
  }
}
1 Like