Optimised VectorUtils getCircle()
This commit is contained in:
@@ -98,5 +98,5 @@ build.dependsOn publishToMavenLocal
|
|||||||
|
|
||||||
group = 'com.willfp'
|
group = 'com.willfp'
|
||||||
archivesBaseName = project.name
|
archivesBaseName = project.name
|
||||||
version = '1.1.2'
|
version = '1.1.3'
|
||||||
java.sourceCompatibility = JavaVersion.VERSION_1_8
|
java.sourceCompatibility = JavaVersion.VERSION_1_8
|
||||||
@@ -6,9 +6,16 @@ import org.bukkit.util.Vector;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public class VectorUtils {
|
public class VectorUtils {
|
||||||
|
/**
|
||||||
|
* Cached circles to prevent many sqrt calls.
|
||||||
|
*/
|
||||||
|
private final Map<Integer, Vector[]> CIRCLE_CACHE = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If vector has all components as finite.
|
* If vector has all components as finite.
|
||||||
*
|
*
|
||||||
@@ -66,6 +73,11 @@ public class VectorUtils {
|
|||||||
* @return An array of {@link Vector}s.
|
* @return An array of {@link Vector}s.
|
||||||
*/
|
*/
|
||||||
public Vector[] getCircle(final int radius) {
|
public Vector[] getCircle(final int radius) {
|
||||||
|
Vector[] cached = CIRCLE_CACHE.get(radius);
|
||||||
|
if (cached != null) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
|
||||||
ArrayList<Vector> circleVecs = new ArrayList<>();
|
ArrayList<Vector> circleVecs = new ArrayList<>();
|
||||||
|
|
||||||
double xoffset = -radius;
|
double xoffset = -radius;
|
||||||
@@ -85,7 +97,9 @@ public class VectorUtils {
|
|||||||
zoffset++;
|
zoffset++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return circleVecs.toArray(new Vector[0]);
|
Vector[] result = circleVecs.toArray(new Vector[0]);
|
||||||
|
CIRCLE_CACHE.put(radius, result);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user