Advertisement
Guest User

Quadtree.java

a guest
Apr 10th, 2013
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. public class QuadTree {
  2.        
  3.         /** Attributes **/
  4.         MyPicture pic;
  5.         QuadNode root;
  6.         int maxLevel;
  7.         double RedT;
  8.         double GreenT;
  9.         double BlueT;
  10.         QuadNode northWest;
  11.         QuadNode southWest;
  12.         QuadNode northEast;
  13.         QuadNode southEast;
  14.        
  15.        
  16.         /**
  17.          * Constructor for QuadTree
  18.          * @param pic - the picture object
  19.          * @param root - the root
  20.          * @param maxLevel - the maximum level
  21.          * @param RedT - the red T
  22.          * @param GreenT - the green T
  23.          * @param BlueT - the blue T
  24.          */
  25.         public QuadTree (MyPicture pic, QuadNode root, int maxLevel, double RedT, double GreenT, double BlueT){
  26.                 this.pic = pic;
  27.                 this.root = root;
  28.                 this.maxLevel = maxLevel;
  29.                 this.RedT = RedT;
  30.                 this.GreenT = GreenT;
  31.                 this.BlueT = BlueT;
  32.                 this.split(root);
  33.         }
  34.        
  35.         /**
  36.          * Split helper method that splits the segments represented by it into 4 QuadNode nodes
  37.          * One for each of northEast, northWest, southEast, and southWest
  38.          * @param QuadNode node
  39.          */
  40.         public void split(QuadNode node) {
  41.             if (homogeneous(node) != true) {
  42.                 node.northWest = new QuadNode(pic, node.getX(), node.getY(), node.getSideLength()/2,node.getLevel()+1, pic.simpleStatistics(node.getX(),node.getY(), node.getSideLength()));
  43.                 split(northWest);
  44.                 node.northEast = new QuadNode(pic, node.getX()+node.getSideLength()/2, node.getY(), node.getSideLength()/2,node.getLevel()+1, pic.simpleStatistics(node.getX(),node.getY(), node.getSideLength()));
  45.                 split(northEast);
  46.                 node.southWest = new QuadNode(pic, node.getX(), node.getY()+node.getSideLength()/2, node.getSideLength()/2,node.getLevel()+1, pic.simpleStatistics(node.getX(),node.getY(), node.getSideLength()));
  47.                 split(southWest);
  48.                 node.southEast = new QuadNode(pic, node.getX()+node.getSideLength()/2, node.getY()+node.getSideLength()/2, node.getSideLength()/2,node.getLevel()+1, pic.simpleStatistics(node.getX(),node.getY(), node.getSideLength()));
  49.                 split(southEast);
  50.                
  51.             }
  52.         }
  53.        
  54.        
  55.         public boolean homogeneous(QuadNode node){
  56.             if ((node.sigmaRed <= RedT && node.sigmaGreen <= GreenT && node.sigmaBlue <= BlueT || node.level==maxLevel || node.sideLength <= 8))
  57.             return true;   
  58.         }
  59.        
  60.         public boolean isLeaf() {
  61.             return northEast == null;
  62.         }
  63.        
  64.         private void preorder (QuadNode root, LinkedQueue<T> queue)
  65.         {
  66.  
  67.            if (root != null)
  68.          {
  69.               queue.enqueue(node);
  70.               preorder (root.getNorthEast(), queue);
  71.               preorder (root.getSouthWest(), queue);
  72.               preorder (root.getNorthWest(), queue);
  73.               preorder (root.getSouthEast(), queue);
  74.            }//if
  75.  
  76.         }  // method preorder
  77.        
  78.         public void drawSegmentation(MyPicture pic, LinkedQueue<T> queue){
  79.             dequeue(queue);
  80.         }
  81.        
  82.         public void paintSquares(){
  83.             paintSegment(int x,int y,int sideLength, double red,double green,double blue);
  84.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement