-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathShape.java
More file actions
137 lines (118 loc) · 2.97 KB
/
Shape.java
File metadata and controls
137 lines (118 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package packalg;
/**
* PackAlg
* By Arwid Bancewicz, October 19, 2009
*/
// Imports
import java.util.Comparator;
import java.util.List;
/**
* Shape.java - A class representing a 3-dimensional shape with position but
* empty dimensions.
*
* @author Arwid Bancewicz
* @version 1.1, (09/19/09)
*/
public abstract class Shape {
// Fields
protected int iiX, iiY, iiZ; // position (default 0)
/**
* Sets the position according to the specified parameters
*/
public final void setPosition(int aiX, int aiY, int aiZ) {
this.iiX = aiX;
this.iiY = aiY;
this.iiZ = aiZ;
}
/**
* Sets the position equal to that of the specified shape.
*/
public final void matchPositionOf(Shape aoShape) {
this.setPosition(aoShape.iiX, aoShape.iiY, aoShape.iiZ);
}
/**
* Returns the object's bottom area (along XY plane).
*/
public abstract int getArea();
/**
* Returns the object's volume.
*/
public abstract int getVolume();
/**
* Rotate along the XY plane.
*/
public abstract void rotateXY();
/**
* Whether this can contain the specified shape at its current rotation.
*/
public abstract boolean canContain(Shape aoShape);
/**
* Whether this can contain the specified shape at any rotation. The said
* shape will be left using the fitted rotation. Otherwise, it's left as
* before.
*/
public abstract boolean attemptToContain(Shape aoShape);
/**
* Break this object up into unoccupied regions around the specified shape
* (The shape must fit this object).
* @param aoOccupied the occupied region
* @return the unoccupied regions
*/
public abstract List<Shape> breakUp(Shape aoOccupied);
/**
* Full string which includes toString() of super class.
*/
public abstract String toFullString();
/**
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "[X=" + iiX + ", Y=" + iiY + ", Z=" + iiZ + "]";
}
/**
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
Shape other = (Shape) obj;
if (iiX != other.iiX || iiY != other.iiY || iiZ != other.iiZ)
return false;
return true;
}
// Other Members
/**
* Compute total volume of specified shapes.
*
* @param aoShapes the shapes to compute
* @return the total volume of the shapes
*/
public static final int getTotalVolume(List<Shape> aoShapes) {
int liTotalVolume = 0;
for (Shape loShape : aoShapes) {
liTotalVolume += loShape.getVolume();
}
return liTotalVolume;
}
}
// Other Classes
/**
* Dimension comparator for Shape area sort (ascending)
* @see Comparator
*/
class shapeAreaComparator implements Comparator<Shape> {
@Override
public int compare(Shape o1, Shape o2) {
return (o1.getArea() - o2.getArea());
}
}
/**
* Dimension comparator for Shape volume sort (ascending)
* @see Comparator
*/
class shapeVolumeComparator implements Comparator<Shape> {
@Override
public int compare(Shape o1, Shape o2) {
return (o1.getVolume() - o2.getVolume());
}
}