Advertisement
Guest User

bsp

a guest
Nov 17th, 2015
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.85 KB | None | 0 0
  1. package Core;
  2.  
  3. import java.awt.Rectangle;
  4. import java.util.HashSet;
  5. import java.util.Random;
  6. import java.util.Set;
  7. import java.util.Stack;
  8.  
  9. public class BinarySpaceTree{
  10.  
  11.     public class Node{
  12.         public Rectangle rect;
  13.         public Node[] children = new Node[2];
  14.         public Node parent = null;
  15.         public Node(Rectangle rect){
  16.             this.rect = rect;
  17.         }      
  18.         public boolean hasChildren(){
  19.             return children[0] != null || children[1] != null;
  20.         }
  21.         public boolean hasChild(int index){
  22.             return children[index] != null;
  23.         }
  24.         public Stack<Node> getAvailableChildren(){
  25.             Stack<Node> hold = new Stack<>();
  26.             if(hasChild(0)){
  27.                 hold.push(children[0]);
  28.             }
  29.             if(hasChild(1)){
  30.                 hold.push(children[1]);
  31.             }
  32.             return hold;
  33.         }
  34.     }
  35.    
  36.     Node start;
  37.    
  38.     public BinarySpaceTree(Rectangle r){
  39.         start = new Node(r);
  40.     }
  41.     Random r =  new Random();
  42.    
  43.     int size = 4;
  44.    
  45.     public Set<Rectangle> Split(int minWidth, int minHeight){
  46.         size = ((minWidth/8)*2);
  47.         Set<Rectangle> rects = new HashSet<>();
  48.         Stack<Node> ends = new Stack<>();
  49.         Stack<Node> hold = new Stack<>();
  50.         hold.add(start);
  51.        
  52.         while(!hold.isEmpty()){
  53.             Node current;
  54.            
  55.             if((current = hold.pop()).hasChildren()){
  56.                
  57.                 hold.push(current.children[0]);
  58.                 hold.push(current.children[1]);            
  59.                
  60.             }else{
  61.                
  62.                 if(current.rect.width < minWidth && current.rect.height < minHeight){ // change this later to deal with either if able.
  63.                     ends.add(current);
  64.                 }else{
  65.                    
  66.                 Rectangle[] boxes = new Rectangle[2];              
  67.                     if(current.rect.width == 0 || current.rect.height == 0){
  68.                         continue;
  69.                     }
  70.                     if((current.rect.width/current.rect.height > 1.5) && (current.rect.height/current.rect.width < 1.5)){//horizontal
  71.                         int minX = (int)(current.rect.getMinX()+r.nextInt(current.rect.width/size)*size);                      
  72.                         int nWidth = minX-current.rect.x;                  
  73.                         boxes[0] = new Rectangle(current.rect.x,current.rect.y,nWidth,current.rect.height);                    
  74.                         boxes[1] = new Rectangle(minX,current.rect.y,current.rect.width-nWidth,current.rect.height);                   
  75.                                            
  76.                     }else if((current.rect.height/current.rect.width > 1.5) && (current.rect.width/current.rect.height < 1.5)){//vertical
  77.                         int minY = (int)(current.rect.getMinY()+r.nextInt(current.rect.height/size)*size);                     
  78.                         int nHeight = minY-current.rect.y;                 
  79.                         boxes[0] = new Rectangle(current.rect.x,current.rect.y,current.rect.width,nHeight);                    
  80.                         boxes[1] = new Rectangle(current.rect.x,minY,current.rect.width,current.rect.height-nHeight);                  
  81.                        
  82.                     }else{
  83.                         if((current.rect.height < current.rect.width) || (current.rect.height == current.rect.width && r.nextBoolean())){
  84.                             int minX = (int)(current.rect.getMinX()+r.nextInt(current.rect.width/size)*size);                      
  85.                             int nWidth = minX-current.rect.x;                  
  86.                             boxes[0] = new Rectangle(current.rect.x,current.rect.y,nWidth,current.rect.height);                    
  87.                             boxes[1] = new Rectangle(minX,current.rect.y,current.rect.width-nWidth,current.rect.height);               
  88.                         }else{
  89.                             int minY = (int)(current.rect.getMinY()+r.nextInt(current.rect.height/size)*size);                     
  90.                             int nHeight = minY-current.rect.y;                 
  91.                             boxes[0] = new Rectangle(current.rect.x,current.rect.y,current.rect.width,nHeight);                    
  92.                             boxes[1] = new Rectangle(current.rect.x,minY,current.rect.width,current.rect.height-nHeight);  
  93.                         }
  94.                        
  95.                     }
  96.                     //System.out.println(current.rect);
  97.                         if(boxes[0].getSize().equals(boxes[1].getSize())){
  98.                         hold.push(current);                            
  99.                         }else{
  100.                         (current.children[0] = new Node(boxes[0])).parent = current;
  101.                         (current.children[1] = new Node(boxes[1])).parent = current;                   
  102.                         hold.push(current.children[0]);
  103.                         hold.push(current.children[1]);                    
  104.                         }
  105.                 }  
  106.             }
  107.         }
  108.            
  109.        
  110.        
  111.         for(Node n: ends){
  112.             rects.add(n.rect);
  113.         }
  114.        
  115.         return rects;
  116.     }
  117.    
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement