My for-loop doesn't filter, it either cuts all or none


#1

Hi
I’m trying to filter through an arraylist (rectangles) by comparing the locations of the objects in it with the locations of the objects of another arraylist (windows).
The goal eventually is to make different kinds of rectangles depending on the fact that they’re above a window or not.

At the moment it either cuts all rectangles or it cuts none.

class Facade{
  
....
  
  boolean aboveWindow;
  
  ArrayList< Window> windows;
  ArrayList<Rectangle> rectangles;
  
  Facade ( PVector locBuild,PVector dimBuild, int numWindowsHorizontal, int numWindowsVertical, float windowSpacing){
    
    ...
    
    aboveWindow=true;
    
    
  }
  
  void update(){
    
    
  }
  
  
  void calculateWindows(float value){
    
    windows = new ArrayList<Window>();
    
..
    
    for(int i=0; i< numWindowsHor; i++){
      for (int j=0; j< numWindowsVer; j++){
        
        float windowX= -(buildingWidth)/2+windowSpacing+(i*(windowWidth +windowSpacing))+windowWidth/2;
        float windowY= -(buildingHeight)/2+windowSpacing+(j*(windowHeight+windowSpacing))+windowHeight/2;
        if (random(100)<value){
        windows.add(new Window(windowX, windowY, windowWidth, windowHeight));
        }
        //println(" raam nr"+i+"-"+j+":"+ windowX +";"+ windowY +";"+ windowWidth +";"+ windowHeight+";");
      }
    }

  }
  void calculateRectangles(){
    
    rectangles = new ArrayList<Rectangle>();
    
    numRectVer=int(buildingHeight/rectSpacing);
    numRectHor=int(buildingWidth/rectSpacing);
    
    for(int i=0;i<numRectHor; i++){
      for(int j=0;j<numRectVer; j++){
        
        float rectX=-(buildingWidth)/2+(i*rectSpacing)+rectSpacing/2;
        float rectY=-(buildingHeight)/2+(j*rectSpacing)+rectSpacing/2;
        
        for (Window w: windows){
          if (rectX>(w.loc.x-windowWidth/2) && rectX<(w.loc.x+windowWidth/2)&&
              rectY>(w.loc.y-windowHeight/2) && rectY<(w.loc.y+windowHeight/2)){
                aboveWindow=false;
              }
        }
        if (aboveWindow){
           rectangles.add(new Rectangle(rectX, rectY));
        }
        //println("rectangle "+i+"-"+j+":"+rectX+";"+rectY+";");      
      }
    }
    println("# rectangles:"+rectangles.size());
  }
  
  void render(){
    
    for (Window w: windows){
      w.render();
      }
    for (Rectangle r: rectangles){
      r.render();
    }
  }
}
````Preformatted text`

Thanks in advance x

#2

Please format your code :blush:

It consist on these two steps:

  1. In your code editor (PDE, VS code, Eclipse, etc) ensure you execute the beautifier function. This function automatically indents your code. Auto-indenting makes your code easier to read and helps catching bugs due to mismatch parenthesis, for instance. In the PDE, you use the key combination: ctrl+t
  2. You copy and paste your code in the forum. Then you select the code and you hit the formatting button aka. the button with this symbol: </>

That’s it! Please notice you do not create a new post in case you need to format something you already posted. You can edit your post, copy the code to the PDE, indent the code properly there and then past it back here, format the code and >> save << the edits.

Extra info:

Formatting your code makes everybody’s life easier, your code looks much better plus it ensures your code integrity is not affected by the forum’s formatting (Do you know the forum processes markup code?) Please visit the sticky posts or the FAQ section/post to learn about this, other advantages and super powers you can get in this brand new forum.

Kf


#3

Thanks a lot! I dindn’t know how to format it :slight_smile:


#4

It is better if you provide a code that it can be run so you show what it does. It is not clear what the problem is.

Kf