Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved MathUtils from mapbox-java2.x telemetry to mapbox-java3.0 servi… #719

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions services-core/src/main/java/com/mapbox/core/utils/MathUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.mapbox.core.utils;

public class MathUtils {

/**
* Test a value in specified range, returning minimum if it's below, and maximum if it's above
*
* @param value Value to test
* @param min Minimum value of range
* @param max Maximum value of range
* @return value if it's between min and max, min if it's below, max if it's above
*/
public static double clamp(double value, double min, double max) {
return Math.max(min, Math.min(max, value));
}

/**
* Test a value in specified range, returning minimum if it's below, and maximum if it's above
*
* @param value Value to test
* @param min Minimum value of range
* @param max Maximum value of range
* @return value if it's between min and max, min if it's below, max if it's above
*/
public static float clamp(float value, float min, float max) {
return Math.max(min, Math.min(max, value));
}

/**
* Constrains value to the given range (including min, excluding max) via modular arithmetic.
* <p>
* Same formula as used in Core GL (wrap.hpp)
* std::fmod((std::fmod((value - min), d) + d), d) + min;
*
* @param value Value to wrap
* @param min Minimum value
* @param max Maximum value
* @return Wrapped value
*/
public static double wrap(double value, double min, double max) {
double delta = max - min;

double firstMod = (value - min) % delta;
double secondMod = (firstMod + delta) % delta;

return secondMod + min;
}
}