]> Creatis software - CreaPhase.git/blob - octave_packages/mapping-1.0.7/distance.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / mapping-1.0.7 / distance.m
1 ## Copyright (C) 2004 Andrew Collier <abcollier@users.sourceforge.net>
2 ##
3 ## This program is free software; it is distributed in the hope that it
4 ## will be useful, but WITHOUT ANY WARRANTY; without even the implied
5 ## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
6 ## the GNU General Public License for more details.
7 ##
8 ## You should have received a copy of the GNU General Public License
9 ## along with this file; see the file COPYING.  If not, see
10 ## <http://www.gnu.org/licenses/>.
11
12 ## -*- texinfo -*-
13 ## @deftypefn {Function File} {} @var{dist} = distance(@var{pt1}, @var{pt2})
14 ##
15 ## Calculates the distance (in degrees) between @var{pt1} and @var{pt2}.
16 ##
17 ## @var{pt1} and @var{pt2} are two-column matrices of the form [latitude longitude].
18 ##
19 ## @example
20 ## >> distance([37,-76], [37,-9]) 
21 ## ans = 52.309
22 ## >> distance([37,-76], [67,-76])
23 ## ans = 30.000
24 ## @end example
25 ##
26 ## @seealso{azimuth,elevation}
27 ## @end deftypefn
28
29 ## Author: Andrew Collier <abcollier@users.sourceforge.net>
30
31 ## Uses "cosine formula".
32
33 function dist = distance(pt1, pt2)
34   pt1 = deg2rad(pt1);
35   pt2 = deg2rad(pt2);
36
37   c = pi / 2 - pt1(1);
38   b = pi / 2 - pt2(1);
39   A = pt2(2) - pt1(2);
40
41   dist = rad2deg(acos(cos(b) * cos(c) + sin(b) * sin(c) * cos(A)));
42 endfunction
43
44 ## http://www.mathworks.com/access/helpdesk/help/toolbox/map/distance.shtml