-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixUtils.java
More file actions
97 lines (85 loc) · 3.04 KB
/
Copy pathMatrixUtils.java
File metadata and controls
97 lines (85 loc) · 3.04 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
package ru.cwcode.cwutils.matrix;
import lombok.experimental.UtilityClass;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@UtilityClass
public class MatrixUtils {
/**
* @param array массив [y][x][z]
* <br>где первый элемент (y) - ось вращения
* @param angle угол поворота, кратный 90
* @return повернутый массив
*/
@SuppressWarnings("unchecked")
public <T> T[][][] rotate(T[][][] array, int angle) {
int ySize = array.length;
int xSize = array[0].length;
int zSize = array[0][0].length;
T[][][] rotated = array;
switch (angle) {
case 90 -> {
rotated = (T[][][]) Array.newInstance(array.getClass().getComponentType().getComponentType().getComponentType(), ySize, zSize, xSize);
for (int y = 0; y < ySize; y++) {
for (int x = 0; x < xSize; x++) {
for (int z = 0; z < zSize; z++) {
rotated[y][z][xSize - 1 - x] = array[y][x][z];
}
}
}
}
case 180 -> {
rotated = (T[][][]) Array.newInstance(array.getClass().getComponentType().getComponentType().getComponentType(), ySize, xSize, zSize);
for (int y = 0; y < ySize; y++) {
for (int x = 0; x < xSize; x++) {
for (int z = 0; z < zSize; z++) {
rotated[y][xSize - 1 - x][zSize - 1 - z] = array[y][x][z];
}
}
}
}
case 270 -> {
rotated = (T[][][]) Array.newInstance(array.getClass().getComponentType().getComponentType().getComponentType(), ySize, zSize, xSize);
for (int y = 0; y < ySize; y++) {
for (int x = 0; x < xSize; x++) {
for (int z = 0; z < zSize; z++) {
rotated[y][zSize - 1 - z][x] = array[y][x][z];
}
}
}
}
}
return rotated;
}
/**
* @param array массив[y][x][z]
* @param element искомый элемент
* @return массив [y][x][z] - координаты элемента
*/
public Optional<Integer[]> findFirstPosition(Object[][][] array, Object element) {
List<Integer[]> positions = findAllPositions(array, element);
if (positions.isEmpty()) return Optional.empty();
return Optional.of(positions.get(0));
}
/**
* @param array массив[y][x][z]
* @param element искомый элемент
* @return массив [y][x][z] - координаты элемента
*/
public List<Integer[]> findAllPositions(Object[][][] array, Object element) {
int ySize = array.length;
int xSize = array[0].length;
int zSize = array[0][0].length;
List<Integer[]> positions = new ArrayList<>();
for (int y = 0; y < ySize; y++) {
for (int x = 0; x < xSize; x++) {
for (int z = 0; z < zSize; z++) {
if (!array[y][x][z].equals(element)) continue;
positions.add(new Integer[]{y, x, z});
}
}
}
return positions;
}
}