Utils essentials

While we’re on the topic of interpolation, he’s how I calculate height on a uniform grid, by splitting a quad into two triangles:


   public static final float interpolate2d(float x, float z, float nw, float ne, float se, float sw)
   {
      // n -= n % dim -> n = 0..dim (local offset)
      x = x - (int) x;
      z = z - (int) z;

      // Which triangle of quad (left | right)
      if (x > z)
         sw = nw + se - ne;
      else
         ne = se + nw - sw;

      // calculate interpolation of selected triangle
      float n = lerp(x, nw, ne);
      float s = lerp(x, sw, se);
      return lerp(z, n, s);
   }

public static String getStackTrace(){
	StringWriter sw = new StringWriter();
	new Throwable().printStackTrace(new PrintWriter(sw));
	return sw.toString();
}

public static void ultraHandySysoutJumpWithOneClickToTheCodeInEclipse(Object o){
	System.out.println(""+o+"\n--------------\n"+getStackTrace());
}

public static void main(String[] args){
	ultraHandySysoutJumpWithOneClickToTheCodeInEclipse("nice!");
}

This comes very handy in Eclipse - you can jump directly to the code, Eclipse formats the trace as links. This saved me a lot of time. Maybe shorten the method name to sysout or so.

Thread.dumpStack() seems to work just fine :slight_smile:

but… then it looks like an aggressive red error and you don’t have control in which order it prints (different threads). Try:

	for(int i=0;i<100;i++){
			Thread.dumpStack();
			System.out.println(""+i);
		}

You mean you don’t print everything to stderr? ;D

I use http://marian.musgit.com/grepconsole/index.html to colorize all errors in rainbow colors :wink:

You can get similar kinds of curves by building on the perlin ease function, without the pesky trig functions.

I don’t quite understand what this does. So you can click the line in the stack trace to jump to it? That seems to usually work for me already.

He means it is printed to stdout as opposed to stderr


	public static float getXAtEndOfRotatedLineByOrigin(float x, float lineLength, float angle) {
		return x + (float) Math.cos(Math.toRadians(angle)) * lineLength;
	}

	public static float getYAtEndOfRotatedLineByOrigin(float y, float lineLength, float angle) {
		return y + (float) Math.sin(Math.toRadians(angle)) * lineLength;
	}


/**Lockup table for trigonometric functions (sinus and cosinus) used to improve performance
 * Singelton
 * @author Henning Brackmann
 */
public class TrigonometricLockupTable {
  private static TrigonometricLockupTable instance;
  private float[] sinustable_;
  private float[] cosinustable_;
  float precision=10;
  

  private TrigonometricLockupTable(){
    sinustable_ = new float[(int) (360*precision)];
    for (int i=0;i<sinustable_.length;i++){
      sinustable_[i]=(float)Math.sin(Math.toRadians(i/precision));
    }
    
    cosinustable_ = new float[(int) (360*precision)];
    for (int i=0;i<cosinustable_.length;i++){
      cosinustable_[i]=(float)Math.cos(Math.toRadians(i/precision));
    }
  }
  
  public static TrigonometricLockupTable getInstance(){
    if (instance==null){
      instance = new TrigonometricLockupTable();
    }
    return instance;
  }
  
  /**compute the sinus from lockup table
   * no range check 
   * @param aAngle Values between 0 and 359
   * @return sinus(aAngle)
   */
  public float sinus(float aAngle){
    return sinustable_[(int)(aAngle*precision)];
  }
  
  /**compute the cosinus from lockup table
   * no range check
   * @param aAngle Values between 0 and 359
   * @return cosinus(aAngle)
   */
  public float cosinus(float aAngle){
    return cosinustable_[(int)(aAngle*precision)];
  }
}

Right… what about negative angles, or angles greater than a full rotation.

no range check because i do the check at other places but it could be easyly added:


  /**correct a <0 or 359 degree overflow*/
  private float limitTo360(float angle){
    float resultangle=angle;
    if (resultangle>359) resultangle=resultangle%359;
    if (resultangle<0) resultangle=359-((-1*resultangle)%359);
    return resultangle;
  }

i dont undestand the SIN_MASK from your link. Its probably a smart trick but for now i am happy with my solution.

359.5 degrees is not equivalent to 0.5 degrees, you should be using 360 in your limit calculations.

a good point you are right.