I have tried to convert a calculation from an app I made using MIT AppInventor which uses Kawa to Android using Java.The problem I'm facing is that the trigonometric parts of the calculation in Kawa are using degress.My question is how do I translate this calculation to Java and get the same output?
This is how I do the calculation is Kawa,all variables are of type double:
Tri 1=atan(Offset Depth/Offset Length)
Mark 1=sqrt(Offset Length^2+Offset Depth^2)
Tri 2=(180-Tri1)/2
Mark 2=Duct Depth/(tan(Tri 2))
Then I did my best to translate it to Java code,the variables are double also as above,depth,length and duct depth are user input values.
tri1 = Math.atan(offsetDepth / offsetLength);
marking1 = Math.sqrt(Math.pow(offsetLength,2) + Math.pow(offsetDepth,2));
tri2 = (180 - tri1) / 2;
marking2 = ductDepth / Math.tan(tri2);
Screenshot of what the inputs and outputs look like:
You can use Math.toRadians() to convert degrees to radians.
You can convert the angles to radians yourself.
As we know:
180 degrees = PI radians
So:
1 degree = PI / 180 radians
So wherever you have X degrees,
they are equal to (X * PI / 180) radians.
In Java you have
Math.PI
which defines the value of the PI number.
Just change your Java code to this:
tri11 = Math.atan(1.0 * offsetDepth / offsetLength); // tri11 is radians
tri1 = tri11 * 180.0 / Math.PI; // tri1 is degrees
marking1 = Math.sqrt(Math.pow(1.0 * offsetLength,2) + Math.pow(1.0 * offsetDepth,2));
tri2 = (180.0 - tri1) / 2.0; // tri2 is degrees
tri22 = tri2 * Math.PI / 180.0; // tri22 is radians
marking2 = 1.0 * ductDepth / Math.tan(tri22);
// output whatever you like now
Related
I have this method for calculating regular polygon area:
public double getArea() {
return (sideLength *
sideLength * sides) /
(4 * Math.tan(180 / (double) sides));
}
for sideLength and sides both being equal to 10 it returns -219.816218.
"
However this online calculator: https://www.omnicalculator.com/math/regular-polygon-area
returns 769.4. What is wrong with my method? The formula I use is specified here
.
The arguments to trigonometric functions are defined on radians, not degrees. Use Math.toRadians to convert the angle in degrees to radians - like this:
Math.tan(Math.toRadians(180 / (double) sides))
Or do this computation in radians to start with.
Math.tan(Math.PI / sides)
Use the following return statement
return (sideLength * sideLength * sides) / (4 * Math.tan((180 / sides) * 3.14159 / 180));
Here, *(3.14159 / 180) is added to convert the area from degree converted to radians
The problem is that the Math.tan function uses radians as it's default unit of measure. Use this instead:
(4*Math.tan(Math.PI/180 * 180/(double) sides))
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 have converted the formula into Java provided here. But accuracy is a problem. We are using GPS coordinates.
We are using iPhone provided GPS location that is upto 10 decimal point accuracy.
/*
* Latitude and Longitude are in Degree
* Unit Of Measure : 1 = Feet,2 = Kilometer,3 = Miles
*/
//TODO 3 Change Unit of Measure and DISTANCE_IN_FEET constants to Enum
public static Double calculateDistance(double latitudeA,double longitudeA,double latitudeB,double longitudeB,short unitOfMeasure){
Double distance;
distance = DISTANCE_IN_FEET *
Math.acos(
Math.cos(Math.toRadians(latitudeA)) * Math.cos(Math.toRadians(latitudeB))
*
Math.cos(Math.toRadians(longitudeB) - Math.toRadians(longitudeA))
+
Math.sin(Math.toRadians(latitudeA))
*
Math.sin(Math.toRadians(latitudeB))
);
return distance;
}
FYI : public static final int DISTANCE_IN_FEET = 20924640;
And then I use Math.round(distance); to convert to long.
For actual 25 feet, I am getting 7 feet output.
You need the haversine formula.
Here's my java implementation:
/**
* Calculates the distance in km between two lat/long points
* using the haversine formula
*/
public static double haversine(
double lat1, double lng1, double lat2, double lng2) {
int r = 6371; // average radius of the earth in km
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lng2 - lng1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
* Math.sin(dLon / 2) * Math.sin(dLon / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = r * c;
return d;
}
According to Wikipedia, 10 feet is close to the limit of what you can expect from normal GPS accuracy. (And that assumes you are getting a good GPS signal.) This post on http://gis.stackexchange.com provides more detailed numbers. It basically says that 3 meter horizontal resolution is the best you are likely to get with an ordinary GPS receiver.
So if you are comparing locations provided by two different (ordinary) receivers with best case ( 3 meter / 10 feet ) resolution on each, you are still not going to be able to tell reliably if one receiver is within 10 feet of the other.
(Note your 10 digits of "precision" may well be illusory. The real issue is how accurate the locations are; i.e. how big the error bars are ... on two different devices.)
So in that case what other alternatives I have?
The linked articles mention WAAS and Carrier Phase GPS (CPGPS). Another option is WPS.
I would like to convert the Math.sin(x), where x in radians to a result which will give me as x in degrees not radians.
I have used the normal method and java built in method of conversion between degrees and radians, but any argument I pass to the Math.sin() method is being treated as radians, thereby causing my conversion to be a futile effort.
I want the output of a sin Input to be given as if the input is treated in degrees not radians like the Math.sin() method does.
Java's Math library gives you methods to convert between degrees and radians: toRadians and toDegrees:
public class examples
{
public static void main(String[] args)
{
System.out.println( Math.toRadians( 180 ) ) ;
System.out.println( Math.toDegrees( Math.PI ) ) ;
}
}
If your input is in degrees, you need to convert the number going in to sin to radians:
double angle = 90 ;
double result = Math.sin( Math.toRadians( angle ) ) ;
System.out.println( result ) ;
if your radian value is a, then multiply the radian value with (22/7)/180.
The code would be like this for the above situation:-
double rad = 45 // value in radians.
double deg ;
deg = rad * Math.PI/180; // value of rad in degrees.
You can convert radians to degrees like this:
double rad = 3.14159;
double deg = rad*180/Math.PI;
And the reverse to convert degrees to radians (multiply by pi/180). You can't change the "input method" for Math.sin (you can't tell the function to use degrees instead of radians), you can only change what you pass it as a parameter. If you want the rest of the program to work with degrees, you have to convert it to radians especially for Math.sin(). In other words, multiply the degrees value by pi and divide by 180. To use this with Math.sin(), just convert it like so:
double angle = 90; //90 degrees
double result = Math.sin(angle*Math.PI/180);
Just using the conversion by itself doesn't change anything, you have to pass the converted value to the sin function.
If you want to print sin(90) degree value, you can use this code:
double value = 90.0;
double radians = Math.toRadians(value);
System.out.format("The sine of %.1f degrees is %.4f%n", value, Math.sin(radians));
I´m trying to implement A Practical Analytic Model for Daylight in Processing and project it on a 2d canvas.
Everything works fine so far, except a nasty problem with the azimuth of the sun.
When i animate the time of the day, the sun is jumping to the opposite site on a specific time.
This happens because the azimuth goes from -90 to +90 degree or vice versa.
I´m not sure if that´s a restriction of the paper or i made a mistake calculating the solar position.
As far as i understand the azimuth should be between 0 and 360 degree.
Anyone already implemented the Preetham paper and can help me?
Here´s my code to calculate the solar position.
You can download the complete Processing sketch here:
https://dl.dropbox.com/u/42247259/PreethamSky.zip
Thanks for your help.
H.G.
private void calculateSolarPosition() {
float t = solarTime(standardTime, dayOfYear, standardMeridian, longitude);
float delta = solarDeclination(dayOfYear);
thetaS = angleFromSunToZenith(t, delta, latitude);
phiS = sunAzimuth(t, delta, latitude);
}
/// Returns the solar time at a certain geographic place, day of year and standard time.
private float solarTime(float standardTime, int dayOfYear, float standardMeridian, float longitude) {
return (float)(standardTime + 0.17 * sin(4 * PI * (dayOfYear - 80) / 373) - 0.129 * sin(2 * PI * (dayOfYear - 8) / 355) + 12 * (standardMeridian - longitude) / PI);
}
/// Returns the solar declination. Solar declination is the angle between the rays of the sun and the
/// plane of the earth's equator.
private float solarDeclination(int dayOfYear) {
return (float)(0.4093 * sin(2 * PI * (dayOfYear - 81) / 368.0));
}
/// Returns the angle from the sun to the zenith in rad.
private float angleFromSunToZenith(float solarTime, float solarDeclination, float latitude) {
return (float)(PI / 2 - asin(sin(latitude) * sin(solarDeclination) - cos(latitude) * cos(solarDeclination) * cos(PI * solarTime / 12)));
}
/// Returns the azimuth of the sun in rad. Azimuth is the angle between a line to south and the sun.
private float sunAzimuth(float solarTime, float solarDeclination, float latitude) {
return (float)-(atan((-cos(solarDeclination) * sin(PI * solarTime / 12)) /
(cos(latitude) * sin(solarDeclination) - sin(latitude) * cos(solarDeclination) *
cos(PI * solarTime / 12.0))));
}
I have not tried that so it may not work (depends on the formula which I haven't studied in detail), but instead of atan you could use the atan2 function which gives a full 360 degree result.
/// Returns the azimuth of the sun in rad. Azimuth is the angle between a line to south and the sun.
private float sunAzimuth(float solarTime, float solarDeclination, float latitude) {
return (float)-(atan2((-cos(solarDeclination) * sin(PI * solarTime / 12)),
(cos(latitude) * sin(solarDeclination) - sin(latitude) * cos(solarDeclination) *
cos(PI * solarTime / 12.0))));
}
This function takes into account the signs of numerator and denominator to decide in which quadrant the angle should be.