All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jbox2d.tests.math.SinCosTest Maven / Gradle / Ivy

Go to download

The testbed for JBox2D, a 2d java physics engine, ported from the C++ Box2d engine.

There is a newer version: 2.2.1.1
Show newest version
/*******************************************************************************
 * Copyright (c) 2013, Daniel Murphy
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 	* Redistributions of source code must retain the above copyright notice,
 * 	  this list of conditions and the following disclaimer.
 * 	* Redistributions in binary form must reproduce the above copyright notice,
 * 	  this list of conditions and the following disclaimer in the documentation
 * 	  and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 ******************************************************************************/
package org.jbox2d.tests.math;

import org.jbox2d.common.MathUtils;

public class SinCosTest {
	// formating stuff
	public static final int COLUMN_PADDING = 3;
	public static final int NUM_DECIMALS = 8;
	
	public static int numTables = 50;
	// accuracy
	public static float mostPreciseTable = .00001f;
	public static float leastPreciseTable = .01f;
	public static int accuracyIterations = 100000;
	
	// speed
	public static int speedTrials = 20;
	public static int speedIterations = 5000;
		
	

	private static SinCosTable[] tables;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int overall = 5;
		try{
			numTables = Integer.parseInt(args[0]);
			mostPreciseTable = Float.parseFloat(args[1]);
			leastPreciseTable = Float.parseFloat(args[2]);
			accuracyIterations = Integer.parseInt(args[3]);
			speedTrials = Integer.parseInt(args[4]);
			speedIterations = Integer.parseInt(args[5]);
			overall = Integer.parseInt(args[6]);
		}catch(Exception e){
			System.out.println( "Parameters:   " +
					"  " +
					" ");
			System.out.println( "Sample parameters: 200 .00001 .01 100000 20 5000 2" );
			//return;
		}
		System.out.println("Tables: "+ numTables);
		System.out.println("Most Precise Table: "+mostPreciseTable);
		System.out.println("Least Precise Table: "+leastPreciseTable);
		System.out.println("Accuracy Iterations: "+accuracyIterations);
		System.out.println("Speed Trials: "+ speedTrials);
		System.out.println("Speed Iterations: "+ speedIterations);
		
		constructTables();
		doAccuracyTest(true);
		for(int i=0; i colLengths[j+1]){
					colLengths[j+1] = colLength;
				}
			}else{
				int colLength = header[j].length() + COLUMN_PADDING;
				if( colLength > colLengths[j+1]){
					colLengths[j+1] = colLength;
				}
				for( int i=0; i< results.length; i++){
						colLength = (formatDecimal(results[i][j], NUM_DECIMALS)).length() + COLUMN_PADDING;
						if( colLength > colLengths[j+1]){
							colLengths[j+1] = colLength;
						}
				}
			}
			
		}
		
		// header
		
		System.out.print(spaceString(side[0], colLengths[0]));	
		for(int i=1; i< colLengths.length; i++){
			System.out.print(spaceString(header[i-1], colLengths[i]));
		}
		System.out.println();
		
		for(int i=0; i < results.length; i++){
			
			for(int j=-1; j= space){
			return str.substring(0, space);
		}
		String s = new String(str);

		for (int i = s.length(); i < space; i++) {
			s = " " + s;
		}
		return s;
	}
	
	private static String formatDecimal(double n, int decimals) {
		String num = n + "";
		// no decimal
		if (num.indexOf(".") == -1) {
			return num;
		}
		
		boolean ePresent = false;
		String e = null;
		
		if(num.indexOf("E") != -1){
			e = num.substring(num.indexOf("E"));
			decimals -= e.length();
			num = num.substring(0, num.indexOf("E"));
			ePresent = true;
		}

		int decLen = num.substring(num.indexOf(".") + 1).length();
		int numLen = num.substring(0, num.indexOf(".")).length();

		// if not enough decimals
		if (decLen < decimals) {
			for (int i = 0; i < (decimals - decLen); i++) {
				num = num + " ";
			}
		} else if(decLen > decimals){ // more decimals than needed
			num = num.substring(0, numLen + decimals + 1);
		}
		if(ePresent){
			num += e;
		}
		return num;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy