-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlane.java
More file actions
176 lines (151 loc) · 4.91 KB
/
Plane.java
File metadata and controls
176 lines (151 loc) · 4.91 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Owen Gregson
// Artificial Intelligence
// TTT Checkpoint #3
// Dec 18, 2024
public class Plane {
private long positions;
private String name;
private Plane(String name) {
this.positions = 0;
this.name = name;
}
private Plane(long positions) {
this.positions = positions;
this.name = "";
}
public long positions() {
return this.positions;
}
public String name() {
return this.name;
}
public boolean contains(int position) {
return Bit.isSet(this.positions, position);
}
public boolean contains(Line line) {
return Bit.countOnes(this.positions & line.positions()) == N;
}
public boolean intersects(Plane plane) {
return (this.positions & plane.positions) != 0;
}
public boolean intersects(Line line) {
return (this.positions & line.positions()) != 0;
}
public Line intersection(Plane plane) {
// Returns the Line of intersection of two planes
// Provided that they are not parallel
if (this.intersects(plane) && !this.equals(plane)) {
return Line.find(this.positions & plane.positions);
} else {
return null;
}
}
public int intersection(Line line) {
// Returns the position of the intersection of this plane and a line
// provided that the line does not lie within the plane
if (this.intersects(line) && !this.contains(line)) {
return Bit.leadingOne(line.positions() & this.positions);
} else {
return -1;
}
}
public boolean equals(Plane other) {
return this.positions == other.positions;
}
@Override
public boolean equals(Object other) {
if (other != null && other instanceof Plane) {
return this.equals((Plane) other);
} else {
return false;
}
}
@Override
public String toString() {
String result = "{";
String separator = "";
for (int position = 0; position < Coordinate.NCubed; position++) {
if (this.contains(position)) {
result += separator;
result += position;
separator = ", ";
}
}
result += "}";
return result;
}
private static enum Axis { X, Y, Z }
private static final int N = Coordinate.N;
private void set(int x, int y, int z) {
this.positions = Bit.set(this.positions, Coordinate.position(x, y, z));
}
private static Plane Straight(Axis axis, int value) {
String name = "";
switch (axis) {
case X: name = "YZ-Plane, X = " + value; break;
case Y: name = "XZ-Plane, Y = " + value; break;
case Z: name = "XY-Plane, Z = " + value; break;
}
Plane plane = new Plane(name);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
switch (axis) {
case X: plane.set(value, i, j); break;
case Y: plane.set(i, value, j); break;
case Z: plane.set(i, j, value); break;
}
}
}
return plane;
}
private static Plane ForwardDiagonal(Axis axis) {
String name = "";
switch (axis) {
case X: name = "YZ-Forward Diagonal"; break;
case Y: name = "XZ-Forward Diagonal"; break;
case Z: name = "XY-Forward Diagonal"; break;
}
Plane plane = new Plane(name);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
switch (axis) {
case X: plane.set(i, j, j); break;
case Y: plane.set(j, i, j); break;
case Z: plane.set(j, j, i); break;
}
}
}
return plane;
}
private static Plane ReverseDiagonal(Axis axis) {
String name = "";
switch (axis) {
case X: name = "YZ-Reverse Diagonal"; break;
case Y: name = "XZ-Reverse Diagonal"; break;
case Z: name = "XY-Reverse Diagonal"; break;
}
Plane plane = new Plane(name);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
switch (axis) {
case X: plane.set(i, j, N-1-j); break;
case Y: plane.set(j, i, N-1-j); break;
case Z: plane.set(j, N-1-j, i); break;
}
}
}
return plane;
}
public static final Plane[] planes = new Plane[18];
static {
int count = 0;
for (Axis axis : Axis.values()) {
for (int value = 0; value < N; value++) {
planes[count++] = Straight(axis, value);
}
planes[count++] = ForwardDiagonal(axis);
planes[count++] = ReverseDiagonal(axis);
}
assert count == 18;
}
}