Weather routing: Difference between revisions

→‎{{header|Wren}}: Removed some more unused functions and also fixed discrepancy in final duration (thanks Pete Lomax for finding that).
(Added Wren)
(→‎{{header|Wren}}: Removed some more unused functions and also fixed discrepancy in final duration (thanks Pete Lomax for finding that).)
Line 455:
=={{header|Wren}}==
{{trans|Julia}}
A reasonably faithful translation though I haven't bothered to split the code up into modules (which would mean separate files in Wren) and have dispensed altogether with thefour inverse_haversine functionfunctions which isnaren't actually called.
 
For some unknown reason, there's a slight discrepancy in the final duration, which I've rounded off to the nearest minute anyway, though fortunately it doesn't affect the result.
 
As Wren uses 0-based indexing the points in the minimum path have coordinates one less than those in the Julia results.
Line 533 ⟶ 531:
static asin(d) { rad2Deg(d.asin) }
static atan(x, y) { rad2Deg(x.atan(y)) }
 
// Converts x, y coordinates to polar coordinates with angle in degrees.
static cart2Polar(x, y) { [x*x + y*y, D.atan(x, y)] }
 
// Converts polar coordinates in degrees to cartesian x, y coordinates.
static polar2Cart(r, deg) { [r * D.cos(deg), r * D.sin(deg)] }
}
 
Line 752 ⟶ 744:
}
return [min, idx]
}
 
// Get the closest GridPoint in matrix 'mat' to a given position 'p' which are defined as follows:
// * p: Cartesian indices of a Position (latitude, longitude in degrees) in grid of GridPoints
// * mat: matrix of Gridpoints.
var closestPoint = Fn.new { |p, mat|
var mat2 = mat.map { |gp| haversine.call(p.lat, p.lon, gp.pt.lat, gp.pt.lon)[0] }.toList
return findMin.call(mat2)[1]
}
 
Line 878 ⟶ 862:
}
}
 
 
var startPos = Point2.new(0, 3) // 0-based
Line 906 ⟶ 889:
The route taking the least time found was:
[[0, 3], [0, 4], [1, 5], [2, 6], [3, 6], [4, 6], [5, 6], [6, 6], [7, 5], [7, 4], [8, 3]]
which has duration 4 hours, 4544 minutes.
</pre>
9,485

edits