-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathVec3i.java
More file actions
134 lines (123 loc) · 3.6 KB
/
Vec3i.java
File metadata and controls
134 lines (123 loc) · 3.6 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
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2025 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, only version 3 of the License.
*
* FalsePatternLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.compat;
import com.falsepattern.lib.util.MathUtil;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.val;
import javax.annotation.concurrent.Immutable;
/**
* A functional equivalent to Vec3i present in Minecraft 1.12.
*/
@Getter
@Immutable
@EqualsAndHashCode
@AllArgsConstructor
public class Vec3i implements Comparable<Vec3i> {
/**
* A static zero vector
*/
public static final Vec3i NULL_VECTOR = new Vec3i(0, 0, 0);
protected final int x;
protected final int y;
protected final int z;
/**
* Instantiates a new vector.
*
* @param x the x
* @param y the y
* @param z the z
*/
public Vec3i(double x, double y, double z) {
this(MathUtil.floor(x), MathUtil.floor(y), MathUtil.floor(z));
}
@Override
public int compareTo(@NonNull Vec3i vec) {
return y == vec.getY() ? z == vec.getZ() ? x - vec.getX() : z - vec.getZ() : y - vec.getY();
}
/**
* Cross product between two vectors.
*
* @param vec the other vector
*
* @return the new resulting vector
*/
public Vec3i crossProduct(@NonNull Vec3i vec) {
return new Vec3i(y * vec.getZ() - z * vec.getY(),
z * vec.getX() - x * vec.getZ(),
x * vec.getY() - y * vec.getX());
}
/**
* Gets distance to the vector.
*
* @param x the other x
* @param y the other y
* @param z the other z
*
* @return the distance
*/
public double getDistance(int x, int y, int z) {
return Math.sqrt(distanceSq(x, y, z));
}
/**
* Square root distance to a point.
*
* @param x the other x
* @param y the other y
* @param z the other z
*
* @return the square distance
*/
public double distanceSq(int x, int y, int z) {
val dX = this.x - x;
val dY = this.y - y;
val dZ = this.z - z;
return dX * dX + dY * dY + dZ * dZ;
}
/**
* Square distance between vectors.
*
* @param vec the other vector
*
* @return the square distance
*/
public double distanceSq(@NonNull Vec3i vec) {
return distanceSq(vec.getX(), vec.getY(), vec.getZ());
}
/**
* Square distance between point to center of this Block.
*
* @param x the other x
* @param y the other y
* @param z the other z
*
* @return the square distance to center
*/
public double distanceSqToCenter(double x, double y, double z) {
val dX = this.x + 0.5D - x;
val dY = this.y + 0.5D - y;
val dZ = this.z + 0.5D - z;
return dX * dX + dY * dY + dZ * dZ;
}
}