So, right now I am trying to calculate the angles of a right triangle using the inverse of Cosine. However, I don't really know how to do it. I know the equation, just not how to convert into code. The equation would be:: Cos-1(A/C); However, that does not seem to work in Java. I also tried
angleX = (int) Math.acos(sideC / sideA);
If sideC and sideA were integers, one would have integer division (2 / 3 == 0).
If you do not expect a result in radians, but degrees, a conversion is needed.
As double is an approximation, use round too.
if (sideA == 0) { ... }
angleX = (int) Math.round(
Math.toDegrees(Math.acos(((double)sideC) / sideA)));
Related
case R.id.bTanx:
temp=(float) (number/0.0174532925);
num=Math.tan(temp);
display.setText("Your Result is " + num);
Guys I'm not able to get "Your Result is 1" when number = 45 ,by this code.Please help.
As tan(45)=1 in degrees.i have converted it.but no desired result.
To convert degrees to radian you first need to convert the degrees to a factor (of the circles circumference) by dividing by 360 degrees. Next you multiply by 2PI rad (which is the circumference of a 'unit circle').
When looking at the units you do this: degrees / degrees * radians = radians
So where you divide by 0.017 (2*PI / 360), you need to multiply instead:
temp = (float) (number * 0.0174532925);
Furthermore it is nicer (more clear) if you do not use 'magic numbers' and add comments (so people know what you are doing):
// Convert to rad
temp = (float) (number * 2 * Math.PI / 360);
And/or even use the available Java functionality:
// Convert to rad
temp = Math.toRadians(number);
I was trying to generate a pseudo-random angle in processing today using noise but it is not working as I would have hoped.
float xoff = 0;
float inc = 0.01;
void draw(){
float vx = cos( noise(xoff) * 2 * PI));
xoff += inc;
}
This is the important part of my code. What I thought would happen was that vx would be a random float between -1 and 1 but it is almost always negative. What seems to be the problem is that the noise(xoff) is outputting a limited range of values. Only between 0.3 and 0.7. For vx to be positive it needs to be lower than 0.3 and higher than 0.7, but this never almost never happens.
What is going wrong here?
You might adjust the noiseDetail() to include more than 4 octaves or to use a falloff below 0.5.
I have a distance formula using latitude and longitude:
distance = EARTH_MILES_RADIUS
* Math.acos(Math.sin(lat1 / RADIAN_CONV)
* Math.sin(lat2 / RADIAN_CONV)
+ Math.cos(lat1 / RADIAN_CONV)
* Math.cos(lat2 / RADIAN_CONV)
* Math.cos((lng2 - lng1) / RADIAN_CONV));
lat1,lng1,lat2,lng2 are double primitives. They come to me as double primitives and there is nothing I can do about it.
The problem is that when I have a pair of longitude or latitudes that are the same the formula sometimes returns NaN. I believe this is because I am taking the arc cosine of a number very slightly greater than 1, when in fact it should be exactly 1. I would probably have problems if the points were antipodal as well, where they might be slightly less than -1.
How can I best fix this problem?
If you are indeed calculating great circle distance as I think you are, you should use the Vincenty formula instead of what you have.
http://en.wikipedia.org/wiki/Great-circle_distance
Check for two very close (ideally equal) values in your code. For example:
boolean doubleApproxEqual(double a, double b) {
double PRECISION = 0.000001;
if (Math.abs(a-b) < PRECISION) //not sure what the name of the function is
//cannot be bothered to check
return true;
return false;
}
if you get a True, do cos(1.0) or whatever
Simply checking for equality was enough to fix my problem:
if (lat1 == lat2 && lng1 == lng2) ...
I'm having a problem with Math.atan returning the same value as the input.
public double inchToMOA( double in, double range){
double rangeIn = 36*range;
double atan = (in / rangeIn) * -1.0;
double deg = Math.atan(atan);
double moa = deg * 60;
return moa;
}
I had this all in one line, but I broke it down into different variables to see if I could find out why it wasn't working. if in = -10 and range = 300, then atan is about -.00094. The angle should be about -.053 degrees, but math.atan is returning -.00094, the same as the input.
Is my number too small for math.atan?
Inverse tangent is described here:
http://mathworld.wolfram.com/InverseTangent.html
I don't think your argument is the problem here.
You realize, of course, that computer trig functions deal in radians rather than degrees, right?
It might just be. If you look at the strict definition of the tangent function in mathematics what you see if that tan(x) = sin(x)/cos(x) for small values of "x"
lim x->0, sin(x) = x
lim x->0, cos(x) = 1
hence, you could see that lim x->0, tan(x) -> x meaning that it's inverse, arctan, returns the value it is given. As to the numerical accuracy of Math.atan I would think that the authors had gone to great lengths to ensure it's numerical accuracy.
There's nothing wrong with Math.atan. Its value is nearly 1:1 linear, intersecting the origin, for inputs close to zero. So the closer you are to zero the less change from the input there will be.
I have this function to limit a rotation to the range from 0.0 to 360.0:
private float ClampRotation( float rotation ) {
while( rotation < 0.0f ) rotation += 360.0f;
while( rotation >= 360.0f ) rotation -= 360.0f;
return rotation;
}
This functions works great and it probably can't be more efficient, but I'm just wondering if there are a native Java function that can do the same?
The closest I get is Math.min/max, but it doesn't work as this. A rotation of -10.0 should output 350.0 and not 0.0 as min/max would do.
% (modulus) works on floating point values so use rotation % 360.0f (you will need to add 360.0 afterwards to negative numbers)
Use the modulus operator then account for values less than 0;
private float ClampRotation( float rotation ) {
rotation = rotation % 360f;
if (rotation < 0f) rotation += 360f;
return rotation;
}
it's just math.. you can do it like this:
private float ClampRotation( float rotation ) {
return rotation+360.0f*Math.ceil(-rotation/360.0f);
}
i'm pretty sure it's ok
You have the traditional implementation of wrapping angles which are less than an order of magnitude away the desired range.
Modulus is a bit weird for floating point - it returns negative for negative, so you still have to have a branch, and it involves a division, which is slower on some machines ( as in I've not found a machine where % is significantly less expensive than going round a loop with a couple of subtractions two or three times ).
If your values are within say -1000 to +1000, then your version is both clearer and faster. If your values are wider than that, go for a modulus based version. If it's very important, test both on your hardware with the value ranges you are going to use.