Guest User

blakeman8192

a guest
Feb 27th, 2010
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.03 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2010 Blake Beaupain
  3.  *
  4.  * This program is free software: you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation, either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17.  
  18. package quadtree;
  19.  
  20. import java.util.List;
  21. import java.util.ArrayList;
  22.  
  23. /**
  24.  * A node for the Quadtree.
  25.  *
  26.  * @author blakeman8192
  27.  *
  28.  * @param <E>
  29.  *            An Object that extends {@link Locatable}.
  30.  */
  31. public final class QuadtreeNode<E extends Locatable>
  32. {
  33.  
  34.     private final int minX, minY, maxX, maxY;
  35.     private final List<E> locatables = new ArrayList<E>();
  36.     private QuadtreeNode<E> parent, northwest, northeast, southwest, southeast;
  37.  
  38.     /**
  39.      * Creates a new <code>QuadtreeNode</code>.
  40.      *
  41.      * @param parent
  42.      *            The parent node.
  43.      * @param minX
  44.      *            The minimum X coordinate.
  45.      * @param minY
  46.      *            The minimum Y coordinate.
  47.      * @param maxX
  48.      *            The maximum X coordinate.
  49.      * @param maxY
  50.      *            The maximum Y coordinate.
  51.      */
  52.     public QuadtreeNode(QuadtreeNode<E> parent, int minX, int minY, int maxX, int maxY)
  53.     {
  54.         this.parent = parent;
  55.         this.minX = minX;
  56.         this.minY = minY;
  57.         this.maxX = maxX;
  58.         this.maxY = maxY;
  59.     }
  60.  
  61.     /**
  62.      * Gets the subnode of this node that contains the argued {@link Locatable}.
  63.      *
  64.      * @param locatable
  65.      *            The {@link Loctable} to search for.
  66.      * @return The subnode of this node which contains the argued
  67.      *         {@link Locatable}, or <code>null</code> if no subnode of this
  68.      *         node contains the argued {@link Locatable}.
  69.      */
  70.     public QuadtreeNode<E> subnodeContaining(E locatable)
  71.     {
  72.         if (isLeaf())
  73.             return null;
  74.  
  75.         if (northwest.isWithin(locatable))
  76.             return northwest;
  77.         else if (northeast.isWithin(locatable))
  78.             return northeast;
  79.         else if (southwest.isWithin(locatable))
  80.             return southwest;
  81.         else if (southeast.isWithin(locatable))
  82.             return southeast;
  83.  
  84.         return null;
  85.     }
  86.  
  87.     /**
  88.      * Splits this node into four equal sub-nodes, and adds the
  89.      * {@link Locatable} objects of this node into each appropriate sub-node.
  90.      */
  91.     public void split()
  92.     {
  93.         // Split this node into four equal smaller nodes.
  94.         southwest = new QuadtreeNode<E>(this, minX, minY, maxX / 2, maxY / 2);
  95.         southeast = new QuadtreeNode<E>(this, maxX / 2, maxY / 2, maxX, maxY);
  96.         northwest = new QuadtreeNode<E>(this, minX, maxY / 2, maxX / 2, maxY);
  97.         northeast = new QuadtreeNode<E>(this, maxX / 2, maxY / 2, maxX, maxY);
  98.  
  99.         // Add our locatables into the appropriate new nodes.
  100.         for (E locatable : locatables)
  101.         {
  102.             if (southwest.isWithin(locatable))
  103.                 southwest.getLocatables().add(locatable);
  104.             if (southeast.isWithin(locatable))
  105.                 southeast.getLocatables().add(locatable);
  106.             if (northwest.isWithin(locatable))
  107.                 northwest.getLocatables().add(locatable);
  108.             if (northeast.isWithin(locatable))
  109.                 northwest.getLocatables().add(locatable);
  110.         }
  111.     }
  112.  
  113.     /**
  114.      * Merges each sub-node and every sub-node of each sub-node into this single
  115.      * node, effectively turning this node into a leaf.
  116.      */
  117.     public void merge()
  118.     {
  119.         southwest = null;
  120.         southeast = null;
  121.         northwest = null;
  122.         northeast = null;
  123.     }
  124.  
  125.     /**
  126.      * Checks if the argued {@link Locatable} is within this node.
  127.      *
  128.      * @param l
  129.      *            The {@link Locatable} to check.
  130.      * @return <code>true</code> if the argued {@link Locatable} is within this
  131.      *         node, <code>false</code> otherwise.
  132.      */
  133.     public boolean isWithin(Locatable l)
  134.     {
  135.         return l.getX() > minX && l.getY() > minY && l.getX() <= maxX && l.getY() <= maxY;
  136.     }
  137.  
  138.     /**
  139.      * Checks if this node is a leaf node. A leaf node is a node that has not
  140.      * been split.
  141.      *
  142.      * @return <code>true</code> if this node is a leaf node, <code>false</code>
  143.      *         otherwise.
  144.      */
  145.     public boolean isLeaf()
  146.     {
  147.         // If northwest is null, all of them are and this node is a leaf.
  148.         return northwest == null;
  149.     }
  150.  
  151.     /**
  152.      * Checks if this node is a root node.
  153.      *
  154.      * @return <code>true</code> if this node is a root node, <code>false</code>
  155.      *         otherwise.
  156.      */
  157.     public boolean isRoot()
  158.     {
  159.         return parent == null;
  160.     }
  161.  
  162.     /**
  163.      * Gets a {@link List} containing the {@link Locatable} objects in this
  164.      * node.
  165.      */
  166.     public List<E> getLocatables()
  167.     {
  168.         return locatables;
  169.     }
  170.  
  171.     /**
  172.      * Gets the minimum X coordinate of this node.
  173.      */
  174.     public int getMinX()
  175.     {
  176.         return minX;
  177.     }
  178.  
  179.     /**
  180.      * Gets the minimum Y coordinate of this node.
  181.      */
  182.     public int getMinY()
  183.     {
  184.         return minY;
  185.     }
  186.  
  187.     /**
  188.      * Gets the maximum X coordinate of this node.
  189.      */
  190.     public int getMaxX()
  191.     {
  192.         return maxX;
  193.     }
  194.  
  195.     /**
  196.      * Gets the maximum Y coordinate of this node.
  197.      */
  198.     public int getMaxY()
  199.     {
  200.         return maxY;
  201.     }
  202.  
  203.     /**
  204.      * Gets the width of this node.
  205.      */
  206.     public int getWidth()
  207.     {
  208.         return maxX - minX;
  209.     }
  210.  
  211.     /**
  212.      * Gets the height of this node.s
  213.      */
  214.     public int getHeight()
  215.     {
  216.         return maxY - minY;
  217.     }
  218.  
  219.     /**
  220.      * Gets the parent node.
  221.      *
  222.      * @return The parent <code>QuadtreeNode</code>, or <code>null</code> if
  223.      *         this is a root node.
  224.      */
  225.     public QuadtreeNode<E> getParent()
  226.     {
  227.         return parent;
  228.     }
  229.  
  230.     /**
  231.      * Gets the Northwest node.
  232.      */
  233.     public QuadtreeNode<E> getNorthwest()
  234.     {
  235.         return northwest;
  236.     }
  237.  
  238.     /**
  239.      * Gets the Northeast node.
  240.      */
  241.     public QuadtreeNode<E> getNortheast()
  242.     {
  243.         return northeast;
  244.     }
  245.  
  246.     /**
  247.      * Gets the Southwest node.
  248.      */
  249.     public QuadtreeNode<E> getSouthwest()
  250.     {
  251.         return southwest;
  252.     }
  253.  
  254.     /**
  255.      * Gets the Southeast node.
  256.      */
  257.     public QuadtreeNode<E> getSoutheast()
  258.     {
  259.         return southeast;
  260.     }
  261.  
  262. }
Advertisement
Add Comment
Please, Sign In to add comment