Skip to content

Commit

Permalink
Interpolate double (#13)
Browse files Browse the repository at this point in the history
* HELP i don't know what I'm doing

* HELP i don't know what I'm doing

* ok hopefully less sad?

* removed simpledatetime, commit on deploy
  • Loading branch information
minhnguyenbhs authored Aug 28, 2024
1 parent 0b63e4c commit 6cc774a
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 17 deletions.
63 changes: 47 additions & 16 deletions geometry/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,67 @@
*/

plugins {
// Apply the java-library plugin for API and implementation separation.
id 'java-library'
// Apply the java-library plugin for API and implementation separation.
id 'java-library'
id "com.diffplug.spotless" version "6.24.0"

}

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
// Use Maven Central for resolving dependencies.
mavenCentral()
}

spotless {
// optional: limit format enforcement to just the files changed by this feature branch
ratchetFrom 'origin/main'

format 'misc', {
// define the files to apply `misc` to
target '*.gradle', '.gitattributes', '.gitignore'

// define the steps to apply to those files
trimTrailingWhitespace()
indentWithTabs() // or spaces. Takes an integer argument if you don't like 4
endWithNewline()
}
java {
// don't need to set target, it is inferred from java
// Allow ignoring certain parts in formatting.
toggleOffOn()
// apply a specific flavor of google-java-format
googleJavaFormat('1.19.2').aosp().reflowLongStrings()
// fix formatting of type annotations
formatAnnotations()
}
}

compileJava.dependsOn 'spotlessApply'


dependencies {
// Use JUnit Jupiter for testing.
testImplementation libs.junit.jupiter
// Use JUnit Jupiter for testing.
testImplementation libs.junit.jupiter

testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

// This dependency is exported to consumers, that is to say found on their compile classpath.
api libs.commons.math3
// This dependency is exported to consumers, that is to say found on their compile classpath.
api libs.commons.math3

// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation libs.guava
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation libs.guava
}

// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

tasks.named('test') {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}

compileJava.dependsOn 'spotlessApply'
72 changes: 72 additions & 0 deletions math/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java library project to get you started.
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.7/userguide/building_java_projects.html in the Gradle documentation.
*/

plugins {
// Apply the java-library plugin for API and implementation separation.
id 'java-library'
id "com.diffplug.spotless" version "6.24.0"

}

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}

spotless {
// optional: limit format enforcement to just the files changed by this feature branch
ratchetFrom 'origin/main'

format 'misc', {
// define the files to apply `misc` to
target '*.gradle', '.gitattributes', '.gitignore'

// define the steps to apply to those files
trimTrailingWhitespace()
indentWithTabs() // or spaces. Takes an integer argument if you don't like 4
endWithNewline()
}
java {
// don't need to set target, it is inferred from java
// Allow ignoring certain parts in formatting.
toggleOffOn()
// apply a specific flavor of google-java-format
googleJavaFormat('1.19.2').aosp().reflowLongStrings()
// fix formatting of type annotations
formatAnnotations()
}
}

compileJava.dependsOn 'spotlessApply'


dependencies {
// Use JUnit Jupiter for testing.
testImplementation libs.junit.jupiter

testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

// This dependency is exported to consumers, that is to say found on their compile classpath.
api libs.commons.math3

// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation libs.guava
}

// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

tasks.named('test') {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}

compileJava.dependsOn 'spotlessApply'
87 changes: 87 additions & 0 deletions math/src/main/java/InterpolateDouble.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class InterpolateDouble {
private HashMap<Double, Double> map;
private ArrayList<Double> sortedKeys;

private final double minValue;
private final double maxValue;

private final double minKey;
private final double maxKey;

public InterpolateDouble(HashMap<Double, Double> map) {
this(map, Double.MIN_VALUE, Double.MAX_VALUE);
}

public InterpolateDouble(HashMap<Double, Double> map, double minValue, double maxValue) {
this.map = map;
this.minValue = minValue;
this.maxValue = maxValue;

sortedKeys = new ArrayList<Double>();
for (Double k : map.keySet()) {
sortedKeys.add(k);
}
Collections.sort(sortedKeys);

// Get lowest and highest keys of the HashMap
if (sortedKeys.size() > 0) {
minKey = sortedKeys.get(0);
maxKey = sortedKeys.get(sortedKeys.size() - 1);
} else {
throw new RuntimeException("Empty HashMap passed to InterpolateDouble");
}
}

/**
* Returns the interpolated value for the given key. If the key is not in the map, it will
* return the value for the closest key.
*
* @param key The key to interpolate
* @return The interpolated value
*/
public double getValue(double key) {
if (map.containsKey(key)) {
return map.get(key);
}

// Ensure that key is within the bounds of the HashMap
if (key < minKey) {
return map.get(minKey);
} else if (key > maxKey) {
return map.get(maxKey);
}

double lowerKey = 0;
double upperKey = 0;
for (double k : sortedKeys) {
if (k < key) {
lowerKey = k;
} else {
upperKey = k;
break;
}
}

double lowerValue = map.get(lowerKey);
double upperValue = map.get(upperKey);

// Edge case if keys equal each other
if (upperKey == lowerKey) {
upperKey += 0.01;
}

double t = (key - lowerKey) / (upperKey - lowerKey);
double result = lowerValue * (1.0 - t) + t * upperValue;
if (result < minValue) {
return minValue;
} else if (result > maxValue) {
return maxValue;
} else {
return result;
}
}
}
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ plugins {
}

rootProject.name = 'coppercore'
include('geometry', 'wpi_interface')
include('geometry', 'wpi_interface', 'math')

0 comments on commit 6cc774a

Please sign in to comment.