Skip to content

Commit

Permalink
Optimised VectorUtils getCircle()
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Jan 12, 2021
1 parent b16266e commit 2a2aed5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ build.dependsOn publishToMavenLocal

group = 'com.willfp'
archivesBaseName = project.name
version = '1.1.2'
version = '1.1.3'
java.sourceCompatibility = JavaVersion.VERSION_1_8
16 changes: 15 additions & 1 deletion src/main/java/com/willfp/eco/util/VectorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@UtilityClass
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.
*
Expand Down Expand Up @@ -66,6 +73,11 @@ public Vector simplifyVector(@NotNull final Vector vec) {
* @return An array of {@link Vector}s.
*/
public Vector[] getCircle(final int radius) {
Vector[] cached = CIRCLE_CACHE.get(radius);
if (cached != null) {
return cached;
}

ArrayList<Vector> circleVecs = new ArrayList<>();

double xoffset = -radius;
Expand All @@ -85,7 +97,9 @@ public Vector[] getCircle(final int radius) {
zoffset++;
}

return circleVecs.toArray(new Vector[0]);
Vector[] result = circleVecs.toArray(new Vector[0]);
CIRCLE_CACHE.put(radius, result);
return result;
}

/**
Expand Down

0 comments on commit 2a2aed5

Please sign in to comment.