I'm trying to calculate the distance between two coordinates and I'm having trouble. I've seen formulas for JavaScript but can't seem to convert it to Autoit correctly. Example
$lat1 = 34.521630
$lon1 = -119.134680
$lat2 = 34.911463
$lon2 = -119.453130
;
;
; $distance = Some code here that will calculate distance
;
;
;
Its not a question of AutoIt itself, you need the right algorithm:
#include <Math.au3>
$lat1 = 34.521630
$lon1 = -119.134680
$lat2 = 34.911463
$lon2 = -119.453130
ConsoleWrite(_distanceInKm($lat1, $lon1, $lat2, $lon2) & #CRLF)
Func _distanceInKm($lat1, $lon1, $lat2, $lon2)
Local $iRadius = 6371
Local $iLat = _Radian($lat2 - $lat1)
Local $iLon = _Radian($lon2- $lon1)
Local $a = Sin($iLat / 2) * Sin($iLat / 2) + Cos(_Radian($lat1)) * Cos(_Radian($lat2)) * Sin($iLon / 2) * Sin($iLon / 2)
Local $c = 2 * ATan2(Sqrt($a), Sqrt(1 - $a))
Local $d = $iRadius * $c
Return Abs($d)
EndFunc
Func ATan2($y, $x)
Return (2 * ATan($y / ($x + Sqrt($x * $x + $y * $y))))
EndFunc
Related
I was calculating the distance between 2 points using google earth Ruler and haversine/Vincenty great-circle distance formula BUt result is not getting the same how can I get the same result as per google earth?
function haversineGreatCircleDistance(
$latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
{
// convert from degrees to radians
$latFrom = deg2rad($latitudeFrom);
$lonFrom = deg2rad($longitudeFrom);
$latTo = deg2rad($latitudeTo);
$lonTo = deg2rad($longitudeTo);
$latDelta = $latTo - $latFrom;
$lonDelta = $lonTo - $lonFrom;
$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
echo 'haversineGreatCircleDistance: '.$angle * $earthRadius;
}
haversineGreatCircleDistance( 22.098299, 90.394299,26.564095, 88.408258);
function vincentyGreatCircleDistance(
$latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
{
// convert from degrees to radians
$latFrom = deg2rad($latitudeFrom);
$lonFrom = deg2rad($longitudeFrom);
$latTo = deg2rad($latitudeTo);
$lonTo = deg2rad($longitudeTo);
$lonDelta = $lonTo - $lonFrom;
$a = pow(cos($latTo) * sin($lonDelta), 2) +
pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
$b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);
$angle = atan2(sqrt($a), $b);
echo 'vincentyGreatCircleDistance: '.$angle * $earthRadius;
}
vincentyGreatCircleDistance( 22.098299, 90.394299,26.564095, 88.408258);
function twopoints_on_earth($latitudeFrom, $longitudeFrom,$latitudeTo, $longitudeTo)
{
$long1 = deg2rad($longitudeFrom);
$long2 = deg2rad($longitudeTo);
$lat1 = deg2rad($latitudeFrom);
$lat2 = deg2rad($latitudeTo);
//Haversine Formula
$dlong = $long2 - $long1;
$dlati = $lat2 - $lat1;
$val = pow(sin($dlati/2),2)+cos($lat1)*cos($lat2)*pow(sin($dlong/2),2);
$res = 2 * asin(sqrt($val));
$radius = 3958.756;
return (($res*$radius)*1609.34);
}
// latitude and longitude of Two Points
$latitudeFrom = 22.098299 ;
$longitudeFrom = 90.394299;
$latitudeTo = 26.564095;
$longitudeTo = 88.408258;
// Distance between Mumbai and New York
print_r(twopoints_on_earth( $latitudeFrom, $longitudeFrom,$latitudeTo, $longitudeTo));
I am working on a game engine, and I ran into this problem when creating a position rotation scale component. The position and scale work just fine, but when I recalculate the rotation quaternion it will invert itself (i.e. point in the opposite direction). What we do is simple -
Whenever the parent quaternion is changed:
We rotate the parent accordingly - Fine
We calculate the child's local rotation (relative to parent) - Fine
We multiply the parent rotation by the child's local rotation - Not fine.
What we are doing is literally (q2/q1)*q2 -> q2*q1^-1*q2
I am using the http://www.technologicalutopia.com/sourcecode/xnageometry/quaternion.cs.htm implementation, where multiplying quaternions will rotate the first by the second, and dividing does the inverse.
I have spent weeks on this problem, so any help is appreciated. :)
There appears to be an error in the CreateFromRotationMatrix method code in the link you posted. My calculations show that all of the difference calculations have the wrong sign. The corrected code would be this, changes where annotated:
public static Quaternion CreateFromRotationMatrix(Matrix matrix)
{
double num8 = (matrix.M11 + matrix.M22) + matrix.M33;
Quaternion quaternion;
if (num8 > 0.0)
{
double num = (double)sqrt((double)(num8 + 1.0));
quaternion.W = num * 0.5;
num = 0.5 / num;
quaternion.X =-(matrix.M23 - matrix.M32) * num; // -
quaternion.Y =-(matrix.M31 - matrix.M13) * num; // -
quaternion.Z =-(matrix.M12 - matrix.M21) * num; // -
return quaternion;
}
if ((matrix.M11 >= matrix.M22) && (matrix.M11 >= matrix.M33))
{
double num7 = (double)sqrt((double)(((1.0 + matrix.M11) - matrix.M22) - matrix.M33));
double num4 = 0.5 / num7;
quaternion.X = 0.5 * num7;
quaternion.Y = (matrix.M12 + matrix.M21) * num4;
quaternion.Z = (matrix.M13 + matrix.M31) * num4;
quaternion.W =-(matrix.M23 - matrix.M32) * num4; // -
return quaternion;
}
if (matrix.M22 > matrix.M33)
{
double num6 = (double)sqrt((double)(((1.0 + matrix.M22) - matrix.M11) - matrix.M33));
double num3 = 0.5 / num6;
quaternion.X = (matrix.M21 + matrix.M12) * num3;
quaternion.Y = 0.5 * num6;
quaternion.Z = (matrix.M32 + matrix.M23) * num3;
quaternion.W =-(matrix.M31 - matrix.M13) * num3; // -
return quaternion;
}
double num5 = (double)sqrt((double)(((1.0 + matrix.M33) - matrix.M11) - matrix.M22));
double num2 = 0.5 / num5;
quaternion.X = (matrix.M31 + matrix.M13) * num2;
quaternion.Y = (matrix.M32 + matrix.M23) * num2;
quaternion.Z = 0.5 * num5;
quaternion.W =-(matrix.M12 - matrix.M21) * num2; // -
return quaternion;
}
A sample run with a MATLAB mex file (qstuff) as a driver:
>> q2
q2 =
0.531932290454131 -0.391764141068189 0.538459173155997 0.523097137240767
>> qstuff('CreateFromRotationMatrix',qstuff('ToMatrix',q2))
ans =
0.531932290454131 0.391764141068189 -0.538459173155998 -0.523097137240767
>> qstuff('CreateFromRotationMatrixNew',qstuff('ToMatrix',q2))
ans =
0.531932290454131 -0.391764141068189 0.538459173155998 0.523097137240767
The original code doesn't reproduce the quaternion, but the corrected code does.
I have following alghoritm to find line intersection of two planes:
public static function getIntersectOf2Planes ( self $P1 , self $P2 )
{
/* Line equation in parametric form:
x = x0 + t*a
y = y0 + t*b
z = z0 + t*c
*/
$x0 = ( $P1->B * $P2->D - $P2->B * $P1->D ) / ( $P1->A * $P2->B - $P2->A * $P1->B ) ;
$a = ( $P1->B * $P2->C - $P2->B * $P1->C );
$y0 = ( $P2->A * $P1->D - $P1->A * $P2->D ) / ( $P1->A * $P2->B - $P2->A * $P1->B ) ;
$b = ( $P2->A * $P1->C - $P1->A * $P2->C );
$z0 = 0;
$c = ( $P1->A * $P2->B - $P2->A * $P1->B );
$IntersectionLine = new Line3D( $x0, $a, $y0, $b, $z0, $c );
return $IntersectionLine;
}
and it works fine, but to compute $x0 and $y0 i have to divide by:
( $P1->A * $P2->B - $P2->A * $P1->B )
and in some cases, the value of this expression is equal to zero, so I get an "dividing by zero" error :(
What should I do in this case?
I know, that the case when this expression is equal to zero, doesn't mean that there is no intersection, because it's happen when I have planes perpendicular to one of the axies.
For example for:
Plane1:
A = 0
B = 0
C = 100
D = 0
Plane2:
A = 50
B = 0
C = 0
D = -250
so the equation of line should exists.
PS I wrote this code with a view to:
https://math.stackexchange.com/questions/2766615/line-by-two-planes-intersection?noredirect=1#comment5706281_2766615
In short, you have to implement the intersection algorithm for the case when (a1*b2 - a2*b1) = 0 (ie. when the planes are not independent when you set z=0).
To expand on that, first we need to understand how you got this far. First let us write down the equation of two planes:
a1x + b1y + c1z + d1 = 0
and
a2x + b2y + c2z + d2 = 0
When two planes intersect, the intersection is a line. So, the most usual way to solve that is by finding a point first on such a line and then figuring out its orientation (a, b, c) in your case. The orientation is a straight forward cross product. The intersection point is typically calculated by setting one of the co-ordinates to 0 and then solving the 2 linear equations that you get. In your code, this is done by setting:
z = 0
But this only works when the equations
a1x + b1y + d1 = 0 and a2x + b2y + d2 = 0
are capable of giving a solution for x and y, which is not the case when a1b2-a2b1=0. So In such cases, you can solve the same by setting either x or y to 0 which again gives two linear equations that you can solve to obtain a point on the line. Then you can compute the parametric form much like how you did. For example (setting y to 0):
x0 = (c1d2 - c2d1)/(a1c2 - a2c1)
y0 = 0
z0 = (a2d1 - a1d2)/(a1c2 - a2c1)
But for this to be a defined value you need to have (a1c2 - a2c1) to be non-zero. Does this help?
I got some code written in a function in VBA, but I'm stuck on how to return the output values areaAnswer1 & areaAnswer2 inside the if - else statments. I'm still new to this. Any help & suggestions are very much appreciated.
Function dateArea(inputDate1 As Date, t1 As Date, t2 As Date, duration As Integer, output As Integer) As Integer
endOfYear = Workbook.Date(Year(inputDate1), 12, 31)
inputDate2 = Workbook.Date(Year(inputDate1) + 1, Month(inputDate1), Day(inputDate1))
endOfDate1 = Workbook.Date(Year(inputDate1) + duration, Month(inputDate1), Day(inputDate1))
endOfDate2 = Workbook.Date(Year(inputDate2) + duration, Month(inputDate2), Day(inputDate2))
areaBase1 = endOfYear - inputDate1
areaBase2 = inputDate2 - endOfYear
totalArea1 = areaBase1 * 365
totalArea2 = areaBase2 * 365
triangleBase1 = endOfDate1 - inputDate1
triangleHypo1 = Workbook.Sqrt((365 * 365) + (triangleBase1 * triangleBase1))
triangleBase2 = t1 - inputDate2
triangleHypo2 = triangleHypo1 * triangleBase2 / triangleBase1
triangleHeight2 = Workbook.Sqrt((triangleHypo2 * triangleHypo2) - (triangleBase2 * triangleBase2))
triangleArea2 = (triangleBase2 * triangleHeight2) / 2
triangleBase3 = (inputDate2 - endOfYear) + (t1 - inputDate2)
triangleHypo3 = triangleBase3 * triangleHypo2 / (t1 - inputDate2)
triangleHeight3 = Workbook.Sqrt((triangleHypo3 * triangleHypo3) - (triangleBase3 * triangleBase3))
triangleArea3 = (triangleBase3 * triangleBaseHeight3) / 2
areaDiffBot2 = triangleArea3 - triangleArea2
triangleBase4 = 365 + (t1 - inputDate2)
triangleHypo4 = triangleBase4 * triangleHeight2 / (t1 - inputDate2)
triangleHeight4 = Workbook.Sqrt((triangleHypo4 * triangleHypo4) - (triangleBase4 * triangleBase4))
triangleArea4 = (triangleBase4 * triangleHeight4) / 2
areaDiffBot1 = triangleArea4 - triangleArea3
triangleHeight5 = 365 * (endOfDate1 - t2) / triangleBase1
triangleHypo5 = Workbook.Sqrt((triangleHeight5 * triangleHeight5) + ((endOfDate1 - t2) * (endOfDate1 - t2)))
triangleArea5 = (endOfDate1 - t2) * triangleHeight5 / 2
triangleBase6 = (endOfDate1 - t2) + areaBase1
triangleHeight6 = (triangleBase6) * 365 / (endOfDate1 - t2)
triangleHypo6 = Workbook.Sqrt((triangleBase6 * triangleBase6) + (triangleHeight6 * triangleHeight6))
triangleArea6 = (triangleBase6 * triangleHeight6) / 2
areaDiffTop1 = triangleArea6 - triangleArea5
triangleBase7 = triangleBase6 + areaBase2
triangleHeight7 = triangleBase7 * triangleHeight6 / triangleBase6
triangleHypo7 = Workbook.Sqrt((triangleBase7 * triangleBase7) + (triangleHeight7 * triangleHeight7))
triangleArea7 = (triangleBase7 * triangleHeight7) / 2
areaDiffTop2 = triangleArea7 - triangleArea6
totalUsedArea1 = areaDiffTop1 + areaDiffBot1
totalUsedArea2 = areaDiffTop2 + areaDiffBot2
areaAnswer1 = totalArea1 - totalUsedArea1
areaAnswer2 = totalArea2 - totalUsedArea2
If output = 1 Then
ElseIf output = 2 Then
ElseIf output = 3 Then
Else
End If
End Function
In VBA you set the return value by assigning it to the function like this:
areaAnswer1 = totalArea1 - totalUsedArea1
areaAnswer2 = totalArea2 - totalUsedArea2
If output = 1 Then
dateArea = areaAnswer1
ElseIf output = 2 Then
dateArea = areaAnswer2
ElseIf output = 3 Then
' ...etc
Note that the assignment to the function does not exit the function. In this case you don't need that immediate exit, as you are already at the end of it. But in some cases you'll want to exit the function as soon as you have assigned the return value:
Exit Function
Just have the function equal a typed value or one of the variables at the end.
select case output
case 1
dateArea = totalUsedArea1
case 2
dateArea = totalUsedArea2
case 3
dateArea = totalUsedArea3
case else
'do something or nothing
end select
Note that you have specified returning an integer which cannot contain a decimal and must be smaller than 32667 (or there abouts).
I have many formula strings similar to this:
str <- "( (( A ) * J ) - (( J ) * G ) ) / Z "
There are many parentheses which don't need to be there, (A*J - J*G)/Z is sufficient. Is there a function or package in R that can take care of this?
I tried functions for R expressions and as well as.formula but did not find what I need.
We can use R parser to do the job. The trick is that R knows when parentheses are needed based on the parse tree, so we can simply remove them from the tree:
See this:
simplify <- function(e)
{
if( mode(e) %in% c("name","numeric") ) return(e)
op <- as.character(e[[1]])
if( op == "(" ) return(simplify(e[[2]]))
if( op %in% c("+","-","*","/","^") ) return(call(op, simplify(e[[2]]), simplify(e[[3]])))
}
simplifytext <- function(s) deparse(simplify(parse(text=s)[[1]]))
Inputs:
str <- "( (( A ) * J ) - (( J ) * G ) ) / Z "
str2 <- gsub("-", "/", gsub("*", "+", str, fixed=TRUE))
Results:
> str2
[1] "( (( A ) + J ) / (( J ) + G ) ) / Z "
> simplifytext(str)
[1] "(A * J - J * G)/Z"
> simplifytext(str2)
[1] "(A + J)/(J + G)/Z"
Here are a couple of approaches:
R parsing
rmParen <- function(e) {
if (length(e) > 1) {
if (identical(e[[1]], as.symbol("("))) e <- e[[2]]
if (length(e) > 1) for (i in 1:length(e)) e[[i]] <- Recall(e[[i]])
}
e
}
s <- "( (( A ) * J ) - (( J ) * G ) ) / Z "
rmParen(parse(text = s)[[1]])
The last line returns:
(A * J - J * G)/Z
This works in all cases I tried but you might want to test it out a bit more.
If you want a character string as the return value then use deparse as in deparse(rmParen(parse(text = s)[[1]])). Note that deparse has a width.cutoff argument which is set to 60 by default but can be set to be larger if the actual expressions exceed that length.
Ryacas
library(Ryacas)
s <- "( (( A ) * J ) - (( J ) * G ) ) / Z "
Simplify(s)
The last line returns:
expression((A - G) * J/Z)
Note that its actually the print method that invokes the computation so if you want to save it then try yacas(Simplify(s))$text or as.character(yacas(Simplify(s))) .
ADDED: Ryacas solution.