www.pudn.com > MobiCraft_src.rar > MyMath.java
// style: tabs, tabsize=4, style=ANSI
//+----------------------------------------------------------------------+
// Copyleft 2007. MobiCraft Team. GNU GPL license.
// Made by Andrew Denisov and Zahar Semenov
//+----------------------------------------------------------------------+
// Filename: MyMath.java
//+----------------------------------------------------------------------+
// Comment: Math in "int" numbers.
// Based on DieWorld's code. Thanks to him for it.
//+----------------------------------------------------------------------+
package app;
public class MyMath
{
//значение синусов и квадратов синусов для углов от 0 до 89 градусов
private final static int sinval[] =
{
0, 17, 35, 52, 70, 87, 105, 122, 139, 156,
174, 191, 208, 225, 242, 259, 276, 292, 309, 326,
342, 358, 375, 391, 407, 423, 438, 454, 469, 485,
500, 515, 530, 545, 559, 574, 588, 602, 616, 629,
643, 656, 669, 682, 695, 707, 719, 731, 743, 755,
766, 777, 788, 799, 809, 819, 829, 839, 848, 857,
866, 875, 883, 891, 899, 906, 914, 921, 927, 934,
940, 946, 951, 956, 961, 966, 970, 974, 978, 982,
985, 988, 990, 993, 995, 996, 998, 999, 999, 1000};
private final static int sin2val[] ={0, 305, 1218, 2739, 4866, 7596, 10926, 14852, 19369, 24472, 30154, 36408, 43227, 50603, 58526, 66987, 75976, 85481, 95491, 105995, 116978, 128428, 140330, 152671, 165435, 178606, 192169, 206107, 220404, 235040, 250000, 265264, 280814, 296632, 312697, 328990, 345491, 362181, 379039, 396044, 413176, 430413, 447736, 465122, 482550, 500000, 517450, 534878, 552264, 569587, 586824, 603956, 620961, 637819, 654508, 671010, 687303, 703368, 719186, 734736, 750000, 764960, 779596, 793893, 807831, 821394, 834565, 847329, 859670, 871572, 883022, 894005, 904508, 914519, 924024, 933013, 941474, 949397, 956773, 963592, 969846, 975528, 980631, 985148, 989074, 992404, 995134, 997261, 998782, 999695};
// Синус. аргумент передаётся в градусах. MyMath.wsin(x) = 1000*Math.sin(x/180*PI)
public static int wsin(int arg)
{
int q = 1;
if (arg < 0) {arg = -arg; q = -1;}
while (arg >= 360) {arg -= 360;}
if (arg >= 0 && arg < 90) {return q*sinval[arg];}
if (arg >= 90 && arg < 180) {return q*sinval[179 - arg];}
if (arg >= 180 && arg < 270) {return -q*sinval[arg - 180];}
if (arg >= 270 && arg < 360) {return -q*sinval[359 - arg];}
return 1001;
}
// Косинус
public static int wcos(int arg)
{
return wsin(90 - arg);
}
// Тангенс
public static int wtg(int arg)
{
return wsin(arg)/wcos(arg);
}
// Котангенс
public static int wctg(int arg)
{
return wcos(arg)/wsin(arg);
}
// Точный угол по двум катетам 0..360; 361, если х=у=0
public static int wangle(int x, int y)
{
long wx = x;
long wy = y;
if (x == 0 && y == 0) { return 361;}
long katet2 = (10000*wy*wy);
long hypo2 = (wx*wx + wy*wy);
long sin2a = (katet2 / hypo2);
sin2a *= 100;
int alpha = 0;
int i;
for (i = 0; i < 89; i++)
{
if (sin2a >= sin2val[i] && sin2a <= sin2val[i+1])
{
alpha = i;
break;
}
}
if (alpha == 0 && i == 89) {
alpha = 90;
}
//alpha = 90 - alpha;
if (x >= 0 && y >= 0){
return alpha;
}
if (x < 0 && y >= 0){
return 180 - alpha;
}
if (x < 0 && y < 0){
return 180 + alpha;
}
if (x >= 0 && y < 0){
return 360 - alpha;
}
return 361;
}
// Квадратный корень
public static int sqrt(int iX)
{
int iGuess = 0;
int iBit = 0x8000;
int iBitShift = 15;
do
{
// 2*Bit*Guess + Bit^2
int iForwardGuess = (iGuess + iGuess + iBit) << iBitShift;
// Verify if the bit should be toggled or not
if (iX >= iForwardGuess)
{
iGuess |= iBit;
iX -= iForwardGuess;
}
iBitShift--;
iBit >>= 1;
}
while (iBit != 0);
return iGuess;
}
/*
// Квадратный корень в целых числах
public static int sqrt(int L)
{
int div, rslt = L;
if (L <= 0)
return 0;
else if ((L & 0xFFFF0000) != 0)
if ((L & 0xFF000000) != 0)
div = 0x3FFF;
else
div = 0x7FF;
else if ((L & 0x0FF00) != 0)
div = 0x3F;
else
div = 0x7;
while (true)
{
div = div + L / div;
div = (div & 1) + (div >> 1);
if (rslt > div)
rslt = div;
else
{
if (L / rslt == rslt - 1 && L % rslt == 0)
rslt--;
return rslt;
}
}
}
*/
public static int sgn(int i)
{
if (i > 0)
return 1;
if (i < 0)
return -1;
return 0;
}
public static int abs(int i)
{
if (i < 0)
return -i;
else
return i;
}
public static int min(int i, int j)
{
if (i < j)
return i;
else
return j;
}
public static int max(int i, int j)
{
if (i > j)
return i;
else
return j;
}
// Степень. i^power
public static int power(int i, int power)
{
int k = i;
for (int z = 0; z < power; z++)
k*=i;
return k;
}
}