Java Code Examples for java.lang.Math#PI

The following examples show how to use java.lang.Math#PI . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UtilTest.java    From almanac-converter with Apache License 2.0 6 votes vote down vote up
@Test
public void radiansShouldConvertToDegrees() {
  double pi = Math.PI;
  double[] radians = new double[] { 0.0, pi/4.0, pi/2.0, pi, 2.0*pi };
  double[] expected = new double[5];
  expected[0] = 0.0;
  expected[1] = 45.0;
  expected[2] = 90.0;
  expected[3] = 180.0;
  expected[4] = 360.0;

  double[] actual = new double[5];
  for (int i=0; i<5; ++i) 
    actual[i] = Util.rtd(radians[i]);

  assertArrayEquals("FAIL: Radians not converting to degrees correctly",
                    expected,
                    actual,
                    1.0E-6);
}
 
Example 2
Source File: UtilTest.java    From almanac-converter with Apache License 2.0 6 votes vote down vote up
@Test
public void degreesShouldConvertToRadians() {
  double pi = Math.PI;
  double[] degrees = new double[] { 0.0, 45.0, 90.0, 180.0, 360.0 };
  double[] expected = new double[5];
  expected[0] = 0.0;
  expected[1] = pi/4.0;
  expected[2] = pi/2.0;
  expected[3] = pi;
  expected[4] = 2.0*pi;

  double[] actual = new double[5];
  for (int i=0; i<5; ++i) 
    actual[i] = Util.dtr(degrees[i]);

  assertArrayEquals("FAIL: Degrees not converting to radians correctly",
                    expected,
                    actual,
                    1.0E-6);
}
 
Example 3
Source File: UtilTest.java    From almanac-converter with Apache License 2.0 6 votes vote down vote up
@Test
public void angleInRadiansShouldFixBetweenProperRange() {
  double pi = Math.PI;
  double[] radians = new double[] { 0.0, -pi/4.0, -pi/2, 2*pi, 5*pi };
  double[] expected = new double[5];
  expected[0] = 0.0;
  expected[1] = pi*7.0/4.0;
  expected[2] = pi*3.0/2.0;
  expected[3] = 0.0;
  expected[4] = pi;

  double[] actual = new double[5];
  for (int i=0; i<5; ++i) 
    actual[i] = Util.fixAngler(radians[i]);

  assertArrayEquals("FAIL: Radians not fixing bounds correctly",
                    expected,
                    actual,
                    1.0E-6);
}
 
Example 4
Source File: Transformation.java    From GiantTrees with GNU General Public License v3.0 6 votes vote down vote up
public Transformation rotaxisz(double delta, double rho) {
	// local rotation away from the local z-axis 
	// about an angle delta using an axis given by rho 
	// - used for splitting and random rotations
	delta = delta*Math.PI/180;
	rho = rho*Math.PI/180;
	
	double a = Math.cos(rho);
	double b = Math.sin(rho);
	double si = Math.sin(delta);
	double co = Math.cos(delta);
	
	Matrix rm = new Matrix((co+a*a*(1-co)),(b*a*(1-co)),(b*si),
			(a*b*(1-co)),(co+b*b*(1-co)),(-a*si),
			(-b*si),(a*si),(co));
	return new Transformation(matrix.prod(rm),vector);
}
 
Example 5
Source File: Transformation.java    From GiantTrees with GNU General Public License v3.0 6 votes vote down vote up
public Transformation rotaxis(double angle,Vector axis) {
	// rotation about an axis
	angle = angle*Math.PI/180;
	axis=axis.normalize();
	double a = axis.getX();
	double b = axis.getY();
	double c = axis.getZ();
	double si = Math.sin(angle);
	double co = Math.cos(angle);
	
	Matrix rm = new Matrix(
			(co+a*a*(1-co)),(-c*si+b*a*(1-co)),(b*si+c*a*(1-co)),
			(c*si+a*b*(1-co)),(co+b*b*(1-co)),(-a*si+c*b*(1-co)),
			(-b*si+a*c*(1-co)),(a*si+b*c*(1-co)),(co+c*c*(1-co)));
	return new Transformation(rm.prod(matrix),vector);
}
 
Example 6
Source File: Vector.java    From GiantTrees with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the angle of a 2-dimensional vector (u,v) with the u-axis 
 *
 * @param v v-coordinate of the vector
 * @param u u-coordinate of the vector
 * @return a value from (-180..180)
 */
static public double atan2(double v, double u)  {
	if (u==0) {
		if (v>=0) return 90;
		else return -90;
	} 
	if (u>0)  return Math.atan(v/u)*180/Math.PI;
	if (v>=0) return 180 + Math.atan(v/u)*180/Math.PI;
	return Math.atan(v/u)*180/Math.PI-180;
}
 
Example 7
Source File: Transformation.java    From GiantTrees with GNU General Public License v3.0 5 votes vote down vote up
public Transformation rotz(double angle) {
	// local rotation about z-axis
	angle = angle*Math.PI/180;
	Matrix rm = new Matrix(Math.cos(angle),-Math.sin(angle),0,
			Math.sin(angle),Math.cos(angle),0,
			0,0,1);
	return new Transformation(matrix.prod(rm),vector);
}
 
Example 8
Source File: Transformation.java    From GiantTrees with GNU General Public License v3.0 5 votes vote down vote up
public Transformation roty(double angle) {
	// local rotation about z-axis
	angle = angle*Math.PI/180;
	Matrix rm = new Matrix(Math.cos(angle),0,-Math.sin(angle),
			0,1,0,
			Math.sin(angle),0,Math.cos(angle));
	return new Transformation(matrix.prod(rm),vector);
}
 
Example 9
Source File: Transformation.java    From GiantTrees with GNU General Public License v3.0 5 votes vote down vote up
public Transformation rotx(double angle) {
	// local rotation about the x axis
	angle = angle*Math.PI/180;
	Matrix rm = new Matrix(1,0,0,
			0,Math.cos(angle),-Math.sin(angle),
			0,Math.sin(angle),Math.cos(angle));
	return new Transformation(matrix.prod(rm),vector);
}
 
Example 10
Source File: Transformation.java    From GiantTrees with GNU General Public License v3.0 5 votes vote down vote up
public Transformation rotxz(double delta, double rho) {
	// local rotation about the x and z axees - for the substems
	delta = delta*Math.PI/180;
	rho = rho*Math.PI/180;
	double sir = Math.sin(rho);
	double cor = Math.cos(rho);
	double sid = Math.sin(delta);
	double cod = Math.cos(delta);
	
	Matrix rm = new Matrix(cor,-sir*cod,sir*sid,
			sir,cor*cod,-cor*sid,
			0,sid,cod);
	return new Transformation(matrix.prod(rm),vector);
}
 
Example 11
Source File: Bearing.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/** @deprecated do not use */
@Deprecated
public static Bearing calculateBearing(Earth e, double lat1, double lon1, double lat2, double lon2, Bearing result) {
  if (result == null) {
    result = new Bearing();
  }

  if ((lat1 == lat2) && (lon1 == lon2)) {
    result.distance = 0;
    result.azimuth = 0;
    result.backazimuth = 0;
    return result;
  }

  double A = e.getMajor(); // Earth radius
  double F = e.getFlattening(); // Earth flattening value
  double R = 1.0 - F;

  double GLAT1 = DEGREES_TO_RADIANS * lat1;
  double GLAT2 = DEGREES_TO_RADIANS * lat2;
  double TU1 = R * Math.sin(GLAT1) / Math.cos(GLAT1);
  double TU2 = R * Math.sin(GLAT2) / Math.cos(GLAT2);
  double CU1 = 1. / Math.sqrt(TU1 * TU1 + 1.);
  double SU1 = CU1 * TU1;
  double CU2 = 1. / Math.sqrt(TU2 * TU2 + 1.);
  double S = CU1 * CU2;
  double BAZ = S * TU2;
  double FAZ = BAZ * TU1;
  double GLON1 = DEGREES_TO_RADIANS * lon1;
  double GLON2 = DEGREES_TO_RADIANS * lon2;
  double X = GLON2 - GLON1;
  double D, SX, CX, SY, CY, Y, SA, C2A, CZ, E, C;
  int loopCnt = 0;
  do {
    loopCnt++;
    // Check for an infinite loop
    if (loopCnt > 1000) {
      throw new IllegalArgumentException(
          "Too many iterations calculating bearing:" + lat1 + " " + lon1 + " " + lat2 + " " + lon2);
    }
    SX = Math.sin(X);
    CX = Math.cos(X);
    TU1 = CU2 * SX;
    TU2 = BAZ - SU1 * CU2 * CX;
    SY = Math.sqrt(TU1 * TU1 + TU2 * TU2);
    CY = S * CX + FAZ;
    Y = Math.atan2(SY, CY);
    SA = S * SX / SY;
    C2A = -SA * SA + 1.;
    CZ = FAZ + FAZ;
    if (C2A > 0.) {
      CZ = -CZ / C2A + CY;
    }
    E = CZ * CZ * 2. - 1.;
    C = ((-3. * C2A + 4.) * F + 4.) * C2A * F / 16.;
    D = X;
    X = ((E * CY * C + CZ) * SY * C + Y) * SA;
    X = (1. - C) * X * F + GLON2 - GLON1;
  } while (Math.abs(D - X) > EPS);

  FAZ = Math.atan2(TU1, TU2);
  BAZ = Math.atan2(CU1 * SX, BAZ * CX - SU1 * CU2) + Math.PI;
  X = Math.sqrt((1. / R / R - 1.) * C2A + 1.) + 1.;
  X = (X - 2.) / X;
  C = 1. - X;
  C = (X * X / 4. + 1.) / C;
  D = (0.375 * X * X - 1.) * X;
  X = E * CY;
  S = 1. - E - E;
  S = ((((SY * SY * 4. - 3.) * S * CZ * D / 6. - X) * D / 4. + CZ) * SY * D + Y) * C * A * R;

  result.distance = S / 1000.0; // meters to km
  result.azimuth = FAZ * RADIANS_TO_DEGREES; // radians to degrees

  if (result.azimuth < 0.0) {
    result.azimuth += 360.0; // reset azs from -180 to 180 to 0 to 360
  }

  result.backazimuth = BAZ * RADIANS_TO_DEGREES; // radians to degrees; already in 0 to 360 range

  return result;
}
 
Example 12
Source File: AG.java    From KEEL with GNU General Public License v3.0 4 votes vote down vote up
/** Inicialization of the population */
public void Initialize () {
	int i, j, temp, mitad_Pob;
	double Valor_Inicial_Sigma = 0.001;

	if (prob_mutacion < 1.0)
		Mu_next = ceil (Math.log(Randomize.Rand()) / Math.log(1.0 - prob_mutacion));
	else  Mu_next = 1;

	Trials=0;

	/* Los conjuntos difusos de los antecedentes de las reglas constituyen la
	primera parte del primer cromosoma de la poblacion inicial.
	Se inicializa C1 en el primer cromosoma. */
	New[0].n_e = 1;
	primer_gen_C2 = 0;

	for (i=0; i<base_reglas.n_reglas; i++) {
		for (j=0; j<base_reglas.tabla.n_var_estado; j++) {
			New[0].Gene[primer_gen_C2] = base_reglas.BaseReglas[i].Ant[j].x0;
			New[0].Gene[primer_gen_C2+1] = base_reglas.BaseReglas[i].Ant[j].x1;
			New[0].Gene[primer_gen_C2+2] = base_reglas.BaseReglas[i].Ant[j].x3;
			primer_gen_C2 += 3;
		}
	}

	/* Se establecen los intervalos en los que varia cada gen de la primera
	parte en la primera generacion */
	for (i=0; i<primer_gen_C2; i+=3) {
		intervalos[i].min = New[0].Gene[i] - (New[0].Gene[i+1]-New[0].Gene[i])/2.0;
		intervalos[i].max = New[0].Gene[i] + (New[0].Gene[i+1]-New[0].Gene[i])/2.0;

		intervalos[i+1].min = New[0].Gene[i+1] - (New[0].Gene[i+1]-New[0].Gene[i])/2.0;
		intervalos[i+1].max = New[0].Gene[i+1] + (New[0].Gene[i+2]-New[0].Gene[i+1])/2.0;

		intervalos[i+2].min = New[0].Gene[i+2] - (New[0].Gene[i+2]-New[0].Gene[i+1])/2.0;
		intervalos[i+2].max = New[0].Gene[i+2] + (New[0].Gene[i+2]-New[0].Gene[i+1])/2.0;
	}


	/* Se inicializa la segunda parte del primer cromosoma con los parametros
	de los consecuentes de las reglas de la BC inicial, junto con los inter-
	valos correspondientes */
	for (i=0; i<base_reglas.n_reglas; i++) {
		for (j=0; j<base_reglas.tabla.n_variables; j++) {
			temp = primer_gen_C2 + i * (base_reglas.tabla.n_variables) + j;
			New[0].Gene[temp] = Math.atan (base_reglas.BaseReglas[i].Cons[j]);
			intervalos[temp].min = -(Math.PI/2) + 1E-10;
			intervalos[temp].max = (Math.PI/2) - 1E-10;
		}
	}

	/* Se genera la segunda mitad de la poblacion inicial generando aleatoriamen-
	te C1 y manteniendo C2 */
	mitad_Pob = ceil(long_poblacion/2);
	for (i=1; i<mitad_Pob; i++) {
		for (j=0; j<primer_gen_C2; j++)
			New[i].Gene[j] = Randomize.Randdouble(intervalos[j].min, intervalos[j].max);

		for (j=primer_gen_C2; j<n_genes; j++)
			New[i].Gene[j] = New[0].Gene[j];

		New[i].n_e = 1;
	}


	/* Se genera el resto de la poblacion inicial generando aleatoriamente C1
	a partir de los intervalos anteriores y mutando C2 */
	for (i=mitad_Pob; i<long_poblacion; i++) {
		for (j=0; j<primer_gen_C2; j++)
			New[i].Gene[j] = Randomize.Randdouble(intervalos[j].min,intervalos[j].max);

		for (j=primer_gen_C2; j<n_genes; j++)
		/* Comprobamos que no se salgan del intervalo permitido [-PI/2,PI/2] */
			do
				New[i].Gene[j] = New[0].Gene[j] + ValorNormal (Valor_Inicial_Sigma);
			while (New[i].Gene[j]<=-(Math.PI/2) || New[i].Gene[j]>=(Math.PI/2));

		New[i].n_e=1;
	}
}
 
Example 13
Source File: Est_evol.java    From KEEL with GNU General Public License v3.0 4 votes vote down vote up
/** Returns the 'a' values as little as we want */
double f (double x, int y) {
	return (y * Math.PI/2 * Math.pow (x,q));
}
 
Example 14
Source File: Est_evol.java    From KEEL with GNU General Public License v3.0 4 votes vote down vote up
private void Mutacion () {
	int n_hijo, i, j, nq, n1, n2;
	double z0, z1, x1, x2, si, co;

	for (n_hijo=0; n_hijo<Landa; n_hijo++) {
		/* Mutation of sigma */
		if (n_sigma==1)  /* if we use only a sigma, the sigma is adapted with Tau_1 */
			Hijos[n_hijo].Gene[tabla.n_variables] *= ValorNormal (Tau_1);
		else {
			z0 = ValorNormal (Tau_0);
			for (i=tabla.n_variables; i<tabla.n_variables + n_sigma; i++) {
				z1 = ValorNormal (Tau);
				Hijos[n_hijo].Gene[i] *= Math.exp (z1+z0);

				/* The standard desviation is Epsilon_sigma if it becomes 0 */
				if (Hijos[n_hijo].Gene[i]==0.0)
					Hijos[n_hijo].Gene[i] = Epsilon_sigma;
			}
		}  
      
		/* Mutation of alfa */
		for (i = tabla.n_variables + n_sigma; i<tabla.n_variables + n_sigma + n_alfa; i++) {
			z0 = ValorNormal (Beta);
			Hijos[n_hijo].Gene[i] += z0;

			/* Si el valor mutado se sale del intervalo [-i,i], se proyecta
			circularmente el valor a dicho intervalo */
			if (Math.abs(Hijos[n_hijo].Gene[i])>i)
				Hijos[n_hijo].Gene[i] -= 2 *i * signo (Hijos[n_hijo].Gene[i]);
		}

		/* Mutation of x */
  
		/* we calculate the uncorrelated vector of mutations */
		for (i=0; i<tabla.n_variables; i++)
			if (tabla.n_variables + i < tabla.n_variables + n_sigma)
				Z[i] = ValorNormal (Hijos[n_hijo].Gene[tabla.n_variables+i]);
			else /* if there aren't more tipical desviations we use the latest */
				Z[i] = ValorNormal (Hijos[n_hijo].Gene[tabla.n_variables+n_sigma-1]);      
   
		/* Correlation of the vector if we use the angles */  
		if (n_alfa!=0) {
			nq = n_alfa;
			for (j=nl_alfa; j<=nm_alfa; ++j) {
				n1 = tabla.n_variables - j;
				n2 = tabla.n_variables;
				for (i=1; i<=j; ++i) {
					x1 = Z[n1-1];
					x2 = Z[n2-1];
					si = Math.sin(Hijos[n_hijo].Gene[tabla.n_variables + n_sigma + nq - 1]);
					co = Math.cos(Hijos[n_hijo].Gene[tabla.n_variables + n_sigma + nq - 1]);
					Z[n2-1] = x1*si + x2*co;
					Z[n1-1] = x1*co - x2*si;
					--n2;
					--nq;
				}
			} 
		}
   
		/* Final mutation of X */ 
		for (i=0; i<tabla.n_variables; i++) {
			Hijos[n_hijo].Gene[i] += Z[i];	

			if (Hijos[n_hijo].Gene[i] < -(Math.PI/2.0))
				Hijos[n_hijo].Gene[i] = -(Math.PI/2.0) + 1E-10;
			if (Hijos[n_hijo].Gene[i] > (Math.PI/2.0))
				Hijos[n_hijo].Gene[i] = (Math.PI/2.0) - 1E-10;
		}
	}
}
 
Example 15
Source File: WebMercatorTile.java    From osm-lib with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static double tile2lat(int yTile, int zoom) {
    double n = Math.PI - (2.0 * Math.PI * yTile) / Math.pow(2.0, zoom);
    return Math.toDegrees(Math.atan(Math.sinh(n)));
}
 
Example 16
Source File: UTM.java    From satstat with GNU General Public License v3.0 4 votes vote down vote up
public static String lat_lon_to_utm(double Lat, double Long, Context c) {

        double deg2rad = Math.PI / 180.0;
        double rad2deg = 180.0 / Math.PI;

        // Parameters for WGS-84
        double a = 6378137.0;
        double eccSquared = 0.00669438;
        double k0 = 0.9996;

        double LongTemp = (Long + 180) - (int) ((Long + 180) / 360) * 360 - 180;
        int ZoneNumber = ((int) (LongTemp + 180) / 6) + 1;

        double LatRad = Lat * deg2rad;
        double LongRad = LongTemp * deg2rad;

        if (Lat >= 56.0 && Lat < 64.0 && LongTemp >= 3.0 && LongTemp < 12.0) {
            ZoneNumber = 32;
        }

        // Special zones for Svalbard
        if (Lat >= 72.0 && Lat < 84.0)
            if (LongTemp >= 0.0 && LongTemp < 9.0)
                ZoneNumber = 31;
            else if (LongTemp >= 9.0 && LongTemp < 21.0) ZoneNumber = 33;
            else if (LongTemp >= 21.0 && LongTemp < 33.0) ZoneNumber = 35;
            else if (LongTemp >= 33.0 && LongTemp < 42.0) ZoneNumber = 37;

        double LongOrigin = (ZoneNumber - 1) * 6 - 180 + 3;
        double LongOriginRad = LongOrigin * deg2rad;

        double eccPrimeSquared = (eccSquared) / (1 - eccSquared);
        double N = a / Math.sqrt(1 - eccSquared * Math.sin(LatRad) * Math.sin(LatRad));
        double T = Math.tan(LatRad) * Math.tan(LatRad);
        double C = eccPrimeSquared * Math.cos(LatRad) * Math.cos(LatRad);
        double A = Math.cos(LatRad) * (LongRad - LongOriginRad);

        double M = a * ((1 - eccSquared / 4
                - 3 * eccSquared * eccSquared / 64
                - 5 * eccSquared * eccSquared * eccSquared / 256) * LatRad
                - (3 * eccSquared / 8
                + 3 * eccSquared * eccSquared / 32
                + 45 * eccSquared * eccSquared * eccSquared / 1024) * Math.sin(2 * LatRad)
                + (15 * eccSquared * eccSquared / 256 + 45 * eccSquared * eccSquared * eccSquared / 1024) * Math.sin(4 * LatRad)
                - (35 * eccSquared * eccSquared * eccSquared / 3072) * Math.sin(6 * LatRad));

        double UTMEasting = (k0 * N * (A + (1 - T + C) * A * A * A / 6
                + (5 - 18 * T + T * T + 72 * C - 58 * eccPrimeSquared) * A * A * A * A * A / 120)
                + 500000.0);

        double UTMNorthing = (k0 * (M + N * Math.tan(LatRad) * (A * A / 2 + (5 - T + 9 * C + 4 * C * C) * A * A * A * A / 24
                + (61
                - 58 * T
                + T * T
                + 600 * C
                - 330 * eccPrimeSquared) * A * A * A * A * A * A / 720)));

        if (Lat > 84 || Lat < -80) {
            return (c.getString(R.string.utm_outside_latitude_range));
        } else {
            if (Lat < 0)
                UTMNorthing = UTMNorthing + 10000000.0;
            return (String.format("%d / %s / %,d / %,d", ZoneNumber, ((Lat > 0) ? "N" : "S"), Math.round(UTMEasting), Math.round(UTMNorthing)));
        }
    }