Lat Long to Minutes and Seconds? - latitude-longitude

Google Maps gives me the Lat and Long of a location in decimal notation like this:
38.203655,-76.113281
How do I convert those to Coords (Degrees, Minutes , Seconds)

38.203655 is a decimal value of degrees. There are 60 minutes is a degree and 60 seconds in a minute (1degree == 60min == 3600s).
So take the fractional part of the value, i.e. 0.203655, and multiply it with 60 to get minutes, i.e. 12.2193, which is 12 minutes, and then repeat for the fractional part of minutes, i.e. 0.2193 = 13.158000 seconds.
Example in python:
def deg_to_dms(deg):
d = int(deg)
md = abs(deg - d) * 60
m = int(md)
sd = (md - m) * 60
return [d, m, sd]
print deg_to_dms(38.203655)
print deg_to_dms(-76.113281)

In case you need other geo-related functionality in JavaScript, you may use the following library
http://www.movable-type.co.uk/scripts/latlong.html
it provides the following functionality:
DMS from/to decimal latitude/longitude conversions
Distance calculations
Bearing calculation
Intersection point calcualtion

Python library that does the trick:
https://pypi.python.org/pypi/LatLon/1.0.2

I think this will help you with the solution :
def deg_min_sec(self,degrees=0.0):
if type(degrees) != 'float':
try:
degrees = float(degrees)
except:
print '\nERROR: Could not convert %s to float.' % (type(degrees))
return 0
minutes = degrees % 1.0 * 60
seconds = minutes % 1.0 * 60
return (degrees, minutes, seconds)

Related

Calculation issue in Kotlin

So i am just doing a simple calculation in my app but somehow I'm not getting expected answer with the formula.
var i = 90 / 60 * 1000
This always returns 1000 instead of 1500
It seems some issue with the floating point from 90 / 60 operation, but I'm not sure how to handle it in Kotlin.
The whole number (integer) division 90 / 60 results in 1, namely the places in front of the decimal point. Better divide by a floating point number:
var i = 90 / 60f * 1000
// result: 1500.0

Calculate two angles difference (3d game)

I want to record speed of angle movement in a 3D game.
So we have the X axis, where we move from 0-360 with no border, when we are on 359 and move further we hit 0 again.
The game stores the 0-360 in -180-180 instead of 0-360
To calculate the speed I have to record two stages and compare there difference with the time it took, to get the movement speed.
But how do I get the difference.
the difference from 80-120 is = 40 we can just calculate by minusing them.
but the difference from -175 to 175 is = 10, but how do I calculate that? Cause minus them will give -180, but the difference is actually 10.
Simply add 180 to each value and then take the absolute value of the difference.
Dim delta = Math.Abs((180 + final) - (180 + initial))
EDIT: Not sure whether you always want positive values and you want to differentiate between the direction, e.g. if the movement is 270 degrees in one direction is that actually 90 degrees in the other direction. I think that you actually need to define the problem a bit more clearly because it's open to interpretation at the moment.
One approach is to use a little trigonometry. I'm not entirely sure what the VB way of doing this is, so I'll just use pseudocode. If you assume a1=175 and a2=-175, this should work.
θ1 ← a1 * π / 180
θ2 ← a2 * π / 180
δ ← acos( cos(θ1)*cos(θ2) + sin(θ1)*sin(θ2) ) * 180 / π
If you are averse to the use of the trigonometry, you can use some conditionals instead
if a1 < 0 then
θ1 ← 360 - ((-a1) mod 360)
else
θ1 ← a1 mod 360
if a2 < 0 then
θ2 ← 360 - ((-a2) mod 360)
else
θ2 ← a2 mod 360
δ ← ( MAX(θ1, θ2) - MIN(θ1, θ2) ) mod 360
if δ > 180 then
δ ← 360 - δ
Both of these will return δ the smallest angle between the two angles (i.e. it will be in the range [0, 180]). You'll probably get better performance with the second method though there may be some edge cases you also need to check.
angle1 and angle2 are in the range -179...180 and the difference should return the smallest in absolute value among the numbers
(angle2-angle1)+k*360
where k varies over the integers. So to the difference (175-(-175))=350 some other associated candidates are -730, -370, -10, 710. Obviously, the sought after result would be -10.
The range of the difference in the first term in general is -359...359, so to get a result without sign indefiniteness, in a first step add 360+180=540 and compute the now guaranteed positive remainder mod 360
diff = (angle2-angle1+540) mod 360
The inserted 360 cancels under the mod operation, the 180 gives a shift of +180 that must be removed in the final result
diff = diff - 180
is now in the range -180...180 as required.
In the example, this calculates as
diff = (175-(-175)+540) % 360 - 180
= 890 % 360 -180
= 170 - 180
= -10
as required.
The other way around, exchanging 175 and -175,
diff = (-175-175+540) % 360 - 180
= (-350+540) % 360 - 180
= 190 % 360 - 180
= 190 - 180
= 10
I made a solution:
Private Function Calcdif(ByVal firstAngle As Single, ByVal secondAngle As Single) As Single
Dim difference As Single = secondAngle - firstAngle
Select Case difference
Case Is < -180
difference += 360
Case Is > 180
difference -= 360
End Select
If secondAngle = firstAngle Then
Return 0
Else
Return (Math.Abs(difference))
End If
End
End Function

Objective C - Creating Angles From Current Time

I'm trying to write code to draw a clock on the screen of an iOS device. I need to get the angle of a line (seconds, minutes, hours hands of clock) from the current time. My code accurately grabs the time, but for some reason, all of the angles I receive end up being the same (no matter what time it is).
If it helps, the angle I am constantly receiving is:
-1.5707963267948966
Here is the code I use to get the angles:
secondsTheta = ((seconds/60) * (2 * M_PI)) - (M_PI / 2);
minutesTheta = ((minutes/60) + (seconds/3600)) * (2 * M_PI) - (M_PI / 2);
hoursTheta = ((hours/12) + (minutes/720) + (seconds/43200)) * (2 * M_PI) - (M_PI / 2);
My thought is that something is funky with M_PI, but I don't know what would be...but as I said, the seconds, minutes, and hours variables are correct. They are declared in my header file as ints, and I know that [NSDateComponents seconds](etc) returns an NSInteger, but I don't think that should matter for this basic math.
Since the seconds, minutes, and hours variables are declared as ints the division will not give you the correct values. An int divided by another init will result in an int, what is needed for the result is a float. In order to have the compiler use floating point arithmetic it is necessary that one of the operands be a floating point format number (float).
Example: 10 seconds divided by 60 (10/60) will use integer math and result in 0.
Example: 10.0 seconds divided by 60 (10/60) will use floating point math and result in 0.1.66666667.
Example:
secondsTheta = ((seconds/60.0) * (2 * M_PI)) - (M_PI / 2);
or
secondsTheta = (((float)seconds/60) * (2 * M_PI)) - (M_PI / 2);
Your seconds, minutes and hours are ints. Dividing ints by ints does integer arithmetic and truncates the values, so
seconds/60
will always give you 0. Objective C inherits this behavior from C and this is fairly common behavior among programming languages.

Rounding time in Objective C

I got the following code:
int days = round(durationTimeInterval / D_DAY);
int secondsLeft = durationTimeInterval - (days * D_DAY);
double hoursLeft = secondsLeft % D_HOUR;
double hours = round(hoursLeft);
if seconds left is 10710 then dividing by D_HOUR give me 2,975 I then want to round that up to 3 hours... Am not sure how to do this? Please advise.
You could use the round function or ceil function:
ROUND
CEIL

Objective-C Integer Arithmetic

I'm trying to calculate some numbers in an iPhone application.
int i = 12;
int o = (60 / (i * 50)) * 1000;
I would expect o to be 100 (that's milliseconds) in this example but it equals 0 as displayed by NSLog(#"%d", o).
This also equals 0.
int o = 60 / (i * 50) * 1000;
This equals 250,000, which is straight left-to-right math.
int o = 60 / i * 50 * 1000;
What's flying over my head here?
Thanks,
Nick
In Objective-C / performs integer division on integer arguments, so 4/5 is truncated to 0, 3/2 is truncated to 1, and so on. You probably want to cast some of your numbers to floating-point forms before performing division.
You're also running in to issues with precedence. In the expression
60 / (i * 50) * 1000
the term inside the parentheses is calculated first, so 60 is divided by 600 which produces the result 0. In
60 / i * 50 * 1000
the first operation is to divide 60 by 12 which gives the result 5 and then the multiplications are carried out.
An integer divided by an integer is an integer.
so 60/600 is not 0.1, it is 0.
Cast (or declare) some stuff as float instead.
It's doing integer math. 60 / (12 * 50) is 0.1, truncates to 0.
Should work if you force floating point and then cast back to an integer.
int o = (int)(60.0 / ((double) i / 50.0) * 1000.0;
Probably not really necessary to make everything a double.
Replace:
int o = (60 / (i * 50)) * 1000;
with:
int o = 1200/i;
By order of precedence, the operation:
60 / (12 * 50)
is performed before multiplying by 1000.
This value is less than 1 and is cast to an int, which truncates it to 0. And 0 times anything is 0.
Use a float or first multiply by 1000 to ensure you're not ending up with propagating a 0 in your calculations.
All the operations in your expression are performed in integer arithmetic, meaning that the fractional part of each intermediate result is truncated. This means that if you divide a smaller integer by a larger integer you will always get 0.
To get the result you want you must either make sure the operations are performed in a particular order, or you must use floats. For example the result of
int o = (60.0 / (i * 50.0)) * 1000.0;
should be o = 100.
I think you need to use float here instead of int. It will work the way you want! Will give you answer in decimals as well.