1 | package org.expeditee.core.bounds;
|
---|
2 |
|
---|
3 | import java.util.HashSet;
|
---|
4 | import java.util.Set;
|
---|
5 |
|
---|
6 | import org.expeditee.core.Point;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * The Bounds interface represents objects that denote a subset of points in the
|
---|
10 | * 2-dimensional plane. All points in the plane are either inside or outside the bounds,
|
---|
11 | * and methods are given for determining which category individual or groups of points
|
---|
12 | * belong to.
|
---|
13 | *
|
---|
14 | * @author cts16
|
---|
15 | */
|
---|
16 | public abstract class Bounds {
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Whether the given (x,y) coordinate is inside the borders of this bounds.
|
---|
20 | */
|
---|
21 | public abstract boolean contains(int x, int y);
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Whether the given point is inside the borders of this bounds.
|
---|
25 | */
|
---|
26 | public boolean contains(Point p)
|
---|
27 | {
|
---|
28 | return contains(p.x, p.y);
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Whether there exists a point that is within this bounds and the other bounds.
|
---|
33 | */
|
---|
34 | public boolean intersects(Bounds other)
|
---|
35 | {
|
---|
36 | if (other == null) return false;
|
---|
37 |
|
---|
38 | if (other instanceof AxisAlignedBoxBounds) {
|
---|
39 | return intersects((AxisAlignedBoxBounds) other);
|
---|
40 | } else if (other instanceof CombinationBounds) {
|
---|
41 | return intersects((CombinationBounds) other);
|
---|
42 | } else if (other instanceof EllipticalBounds) {
|
---|
43 | return intersects((EllipticalBounds) other);
|
---|
44 | } else if (other instanceof PolygonBounds) {
|
---|
45 | return intersects((PolygonBounds) other);
|
---|
46 | }
|
---|
47 |
|
---|
48 | return false;
|
---|
49 | }
|
---|
50 | public abstract boolean intersects(AxisAlignedBoxBounds other);
|
---|
51 | public abstract boolean intersects(CombinationBounds other);
|
---|
52 | public abstract boolean intersects(EllipticalBounds other);
|
---|
53 | public abstract boolean intersects(PolygonBounds other);
|
---|
54 |
|
---|
55 | /** Gets the x-coordinate of the right-most point in this bounds. */
|
---|
56 | public abstract int getMaxX();
|
---|
57 | /** Gets the x-coordinate of the left-most point in this bounds. */
|
---|
58 | public abstract int getMinX();
|
---|
59 | /** Gets the y-coordinate of the bottom-most point in this bounds. */
|
---|
60 | public abstract int getMaxY();
|
---|
61 | /** Gets the y-coordinate of the top-most point in this bounds. */
|
---|
62 | public abstract int getMinY();
|
---|
63 |
|
---|
64 | public Point getCentre()
|
---|
65 | {
|
---|
66 | return new Point((getMaxX() + getMinX()) / 2, (getMaxY() + getMinY()) / 2);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /** Whether the set of points covered by this bounds is the same as the other bounds. */
|
---|
70 | public abstract boolean equals(Bounds other);
|
---|
71 |
|
---|
72 | /** Gets the total area covered by this bounds. */
|
---|
73 | public abstract double getArea();
|
---|
74 |
|
---|
75 | /** Whether the given point lies on the border of this bounds (within the given area). */
|
---|
76 | public abstract boolean perimeterContains(Point p, double error);
|
---|
77 |
|
---|
78 | /** Whether the other bounds is entirely inside this bounds. */
|
---|
79 | public boolean completelyContains(Bounds other)
|
---|
80 | {
|
---|
81 | if (other == null) return false;
|
---|
82 |
|
---|
83 | if (other instanceof AxisAlignedBoxBounds) {
|
---|
84 | return completelyContains((AxisAlignedBoxBounds) other);
|
---|
85 | } else if (other instanceof CombinationBounds) {
|
---|
86 | return completelyContains((CombinationBounds) other);
|
---|
87 | } else if (other instanceof EllipticalBounds) {
|
---|
88 | return completelyContains((EllipticalBounds) other);
|
---|
89 | } else if (other instanceof PolygonBounds) {
|
---|
90 | return completelyContains((PolygonBounds) other);
|
---|
91 | }
|
---|
92 |
|
---|
93 | return false;
|
---|
94 | }
|
---|
95 | public abstract boolean completelyContains(AxisAlignedBoxBounds other);
|
---|
96 | public abstract boolean completelyContains(CombinationBounds other);
|
---|
97 | public abstract boolean completelyContains(EllipticalBounds other);
|
---|
98 | public abstract boolean completelyContains(PolygonBounds other);
|
---|
99 |
|
---|
100 | /** Translates the bounds by the given x/y amounts. */
|
---|
101 | public abstract Bounds translate(int x, int y);
|
---|
102 |
|
---|
103 | /** Gets all points contained by the bounds. */
|
---|
104 | public Set<Point> getAllContainedPoints()
|
---|
105 | {
|
---|
106 | Set<Point> ret = new HashSet<Point>();
|
---|
107 | for (int x = getMinX(); x <= getMaxX(); x++) {
|
---|
108 | for (int y = getMinY(); y <= getMaxY(); y++) {
|
---|
109 | if (contains(x, y)) ret.add(new Point(x, y));
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | return ret;
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | public abstract Bounds clone();
|
---|
118 | }
|
---|