Remove image from array when use

edited January 2016 in Questions about Code

Hello all! I have loaded 7 image in an array and create a function that chose and places that image ImagePosition(); . I use the function 7 times and I will like the image does not repeat, ex. 3.png is chosen first I will like that it is not coming back for the next 6 choice. Same thing for the other image 2,3,4,5,6 and 7.

My code at the moment look like that maybe I am not in the good pats to accomplish what I want. Thank in advance for information.

PImage [] icone = new PImage[6]; 
float im1;
float im2;
float x;
float y; 
float s; 
float s2;
int ra;

int rx;
int ry;
int rs;

void setup() {
  frameRate(1);
  size(1000, 1000);
  imageMode(CENTER);
  for (int i = 0; i < icone.length; i++) {
    icone[i] = loadImage (i+".png");
  }
} 

void draw() {
  background(200);

  ImagePosition();
  ImagePosition();
  ImagePosition();
  ImagePosition();
  ImagePosition();
  ImagePosition();
  ImagePosition();
}

void ImagePosition() {
  ra=int(random(6));
  x=(random (0, 10))*100;
  rx = round(x);
  y=(random (0, 10))*100;
  ry = round(y);
  s=(random (1, 5))*100;
  rs = round(s);
  image(icone[ra], rx, ry, rs, rs);

  if (s< 200 ) {
    float i=0;
    float rx2; 
    float ry2; 
    i=0;
    rx2=rs;
    ry2=rs;

    while (i < 9) {
      image(icone[ra], rx+rx2, ry+ry2, rs, rs);
      i= random(0, 10);

      float[] a = {-rs, 0, rs};
      float rb= ( random(0, 2));
      int rb2 = round(rb);
      rx2= rx2+ a[rb2];
      float[] b = {-rs, 0, rs};
      float rby= ( random(0, 2));
      int rb3 = round(rby);
      ry2= ry2+ b[rb3];
    }
  }
}

Answers

  • Ah, the old "how do I generate a sequence of random numbers without any repeats?" question.

    The answer is to have an array, where each element is one of the numbers you want. Then swap each index with another index at random.

    For example:

    int[] a = {1,2,3,4,5};
    for(int i=0;i<a.length;i++){
      int r = int(random(a.length));
      int t = a[i];
      a[i] = a[r];
      a[r] = t;
    }
    
  • edited January 2016

    Thanks for that! I have try replace line 43

     ra=int(random(6));
      image(icone[ra], rx, ry, rs, rs);
    

    with your solution

    int[] a = {0, 1, 2, 3, 4, 5, 6};
      for (int i=0; i<a.length; i++) {
        int r = int(random(a.length));
        int t = a[i];
        a[i] = a[r];
        a[r] = t;
      }
    
      image(icone[a], rx, ry, rs, rs);
    

    But I get: cannot convert from int[] to int.

  • there's a shuffle in the java standard library which is probably better than coding your own (no offence!).

    it does use lists though.

    http://www.tutorialspoint.com/java/util/collections_shuffle.htm

  • Thanks, it looks interesting, Maybe I can include it in the draw function , so it will be shuffling each time the programme run and in ImagePosition() I do ra= ra+1 or something like this.

    How to I integrate this code into processing, just to let you know I am a beginner level in coding don't know that much.

  • edited January 2016
    // forum.Processing.org/two/discussion/14310/remove-image-from-array-when-use
    // StackOverflow.com/questions/1519736/random-shuffling-of-an-array
    // GitHub.com/processing/p5.js/pull/528
    
    // 2016-Jan-07
    
    int[] shuffleIntArray(int... arr) {
      for (int rnd, tmp, idx = arr.length; idx > 1; 
        rnd = (int) random(idx--), 
        tmp = arr[idx], arr[idx] = arr[rnd], arr[rnd] = tmp);
    
      return arr;
    }
    
    void setup() {
      println(shuffleIntArray(-5, 10, 50, 100, 2000, -9000));
      exit();
    }
    

    Compatible w/ JavaScript Mode (PJS) too: :D
    http://ProcessingJS.org/tools/processing-helper.html

  • edited January 2016 Answer ✓

    This pumped up version can shuffle() any object array instead of primitive 1s: \m/

    // forum.Processing.org/two/discussion/14310/remove-image-from-array-when-use
    // StackOverflow.com/questions/1519736/random-shuffling-of-an-array
    // GitHub.com/processing/p5.js/pull/528
    
    // 2016-Jan-07
    
    <T> T[] shuffleArray(T... arr) {
      T tmp;
    
      for (int rnd, idx = arr.length; idx > 1; 
        rnd = (int) random(idx--), 
        tmp = arr[idx], arr[idx] = arr[rnd], arr[rnd] = tmp);
    
      return arr;
    }
    
    static final int ICONS = 6;
    PImage[] icons = new PImage[ICONS];
    
    void setup() {
      for (int i = 0; i != ICONS; icons[i++] = createImage(i*4, i*3, ARGB));
    
      printArray(icons); // use println() for PJS
      println();
    
      printArray(shuffleArray(icons)); // use println() for PJS
      exit();
    }
    
  • edited January 2016

    @GoToLoop I am trying your code, I just realize I have 7 image (0.png to 6.png) I try to change static final int ICONS = 6; but it does not load 6.png

    <T> T[] shuffleArray(T... arr) {
      T tmp;
    
      for (int rnd, idx = arr.length; idx > 1; 
        rnd = (int) random(idx--), 
        tmp = arr[idx], arr[idx] = arr[rnd], arr[rnd] = tmp);
    
      return arr;
    }
    
    static final int ICONS = 7;
    PImage[] icons = new PImage[ICONS];
    
    void setup() {
      size(1000, 1000);
      for (int i = 0; i != ICONS; icons[i++] = loadImage (i+".png"));
    
      printArray(icons); // use println() for PJS
      println();
    
      printArray(shuffleArray(icons)); // use println() for PJS
    
    }
    void draw(){
    image(icons[0],0,0);
    image(icons[1],0,0);
    image(icons[2],0,0);
    image(icons[3],0,0);
    image(icons[4],0,0);
    image(icons[5],0,0);
    //image(icons[6],0,0);  this print null
    }
    
  • edited January 2016

    The post increment operator ++: https://Processing.org/reference/increment.html
    @ icons[i++] = loadImage(i + ".png") increases the iterator i.
    So when it reaches loadImage(), it's 1 more than what it was inside [].
    Just move it to the last i: icons[i] = loadImage(i++ + ".png") O:-)

  • edited January 2016

    You can also shorten your image() statements: *-:)

    void draw() {
      int y = 0;
      for (PImage img : icons)  set(0, y++ * 80, img);
    }
    
  • I change toicons[i] = loadImage(i++ + ".png") all image got null

  • edited January 2016 Answer ✓
    for (int i = 0; i < ICONS; i++) {
        icons[i] = loadImage (i+".png");
    }
    

    a for command has parts for counter initialisation, checking and incrementation. just use those. anything else is asking for trouble.

  • thanks for your help Koogs and Gotoloop,Now it work

    // https://forum.processing.org/two/discussion/comment/59037#Comment_59037
    <T> T[] shuffleArray(T... arr) {
      T tmp;
    
      for (int rnd, idx = arr.length; idx > 1; 
        rnd = (int) random(idx--), 
        tmp = arr[idx], arr[idx] = arr[rnd], arr[rnd] = tmp);
    
      return arr;
    }
    
    static final int ICONS = 7;
    PImage[] icons = new PImage[ICONS];
    
    void setup() {
      size(1000, 1000);
      for (int i = 0; i < ICONS; i++) {
        icons[i] = loadImage (i+".png");
    }
    
    
    
      printArray(icons); // use println() for PJS
      println();
    
      printArray(shuffleArray(icons)); // use println() for PJS
    
    }
    void draw(){
      shuffleArray(icons);
    image(icons[0],0,0);
    image(icons[1],0,0);
    image(icons[2],0,0);
    image(icons[3],0,0);
    image(icons[4],0,0);
    image(icons[5],0,0);
    image(icons[6],0,0);
    }
    
Sign In or Register to comment.