View Javadoc
1   package com.jed.actor;
2   
3   import com.jed.util.Vector2f;
4   import org.lwjgl.opengl.GL11;
5   
6   import javax.annotation.Nonnull;
7   
8   /**
9    *
10   * Concrete implementation of {@link com.jed.actor.Boundary} which represents
11   * a polygon with an arbitrary number of sides. Use {@link com.jed.actor.RectangleBoundary}
12   * if you want a concrete implementation for a rectangle.
13   *
14   * @author jlinde, Peter Colapietro
15   * @since 0.1.0
16   *
17   */
18  public final class PolygonBoundary extends Boundary {
19  
20      /**
21       * 
22       */
23      private double rightBound = 0;
24      
25      /**
26       * 
27       */
28      private double leftBound = 0;
29      
30      /**
31       * 
32       */
33      private double upperBound = 0;
34      
35      /**
36       * 
37       */
38      private double lowerBound = 0;
39  
40      /**
41       * 
42       * @param position position vector
43       * @param vertices array of vertices
44       */
45      public PolygonBoundary(final Vector2f position, @Nonnull final Vector2f[] vertices) {
46          super(position, vertices);
47  
48          //Find Max Bounds for quad tree
49          for (final Vector2f vertex: vertices) {
50              if (vertex.x > rightBound) {
51                  rightBound = vertex.x;
52              }
53              if (vertex.x < leftBound) {
54                  leftBound = vertex.x;
55              }
56              if (vertex.y < upperBound) {
57                  upperBound = vertex.y;
58              }
59              if (vertex.y > lowerBound) {
60                  lowerBound = vertex.y;
61              }
62          }
63      }
64  
65      @Override
66      public double getRightBound() {
67          return getOwner().getPosition().x + getPosition().x + rightBound;
68      }
69  
70      @Override
71      public double getLeftBound() {
72          return getOwner().getPosition().x + getPosition().x + leftBound;
73      }
74  
75      @Override
76      public double getUpperBound() {
77          return getOwner().getPosition().y + getPosition().y + upperBound;
78      }
79  
80      @Override
81      public double getLowerBound() {
82          return getOwner().getPosition().y + getPosition().y + lowerBound;
83      }
84  
85      @Override
86      public int getWidth() {
87          return (int) (rightBound - leftBound);
88      }
89  
90      @Override
91      public int getHeight() {
92          return (int) (lowerBound - upperBound);
93      }
94  
95      @Override
96      public void render() {
97          //Bounding Box
98          GL11.glColor3f(1f, 0, 0);
99  
100         GL11.glBegin(GL11.GL_LINE_LOOP);
101         for (final Vector2f vertex : vertices) {
102             getOwner().drawChildVertex2f(getPosition().x + vertex.x, getPosition().y + vertex.y);
103         }
104         GL11.glEnd();
105     }
106 
107 }