Grab a float from 0 to 1 based on millisecond time

It’s pretty hard to describe this. This code can get a number between 0 and 1 at a fixed second rate without having to use a counter to actually find the number, you can “pluck” it.


   public static float getNumberFromTime() {
		return getNumberFromTime(System.currentTimeMillis(), 1);
	}

	public static float getNumberFromTime(float seconds) {
		return getNumberFromTime(System.currentTimeMillis(), seconds);
	}
	
	public static float getNumberFromTime(long time, float seconds) {
      if (seconds == 0f) throw new IllegalArgumentException("seconds cannot be zero!");
		return ((time % Math.round((seconds * 1000))) / (seconds * 1000f));
	}

If you specify seconds to be 1f, that means if you had continuously called the method for one second, the output would be something like 0, 0.001, 0.002, …, 0.999, 1.0, and it’ll reset back to 0. A good use of this if you want a colour’s hue to constantly change, giving a rainbow effect. You could, at any time pluck the saturation using this method and get the colour. This is good if you don’t care when it STARTS, as long as it loops nicely. Also, because it doesn’t actually count from 0-1, you can call this method with other second parameters and get a different number.

This was quite hard to describe. This method was meant to be called per render update.

This is called the fractional part, the fpart function in TI-Basic :point:

It’s more succintly written as [icode]f(x) = x - floor(x)[/icode]

Also called the sawtooth function, etc.