| 1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
| 2 | .\" |
| 3 | .\" Standard preamble: |
| 4 | .\" ======================================================================== |
| 5 | .de Sh \" Subsection heading |
| 6 | .br |
| 7 | .if t .Sp |
| 8 | .ne 5 |
| 9 | .PP |
| 10 | \fB\\$1\fR |
| 11 | .PP |
| 12 | .. |
| 13 | .de Sp \" Vertical space (when we can't use .PP) |
| 14 | .if t .sp .5v |
| 15 | .if n .sp |
| 16 | .. |
| 17 | .de Vb \" Begin verbatim text |
| 18 | .ft CW |
| 19 | .nf |
| 20 | .ne \\$1 |
| 21 | .. |
| 22 | .de Ve \" End verbatim text |
| 23 | .ft R |
| 24 | .fi |
| 25 | .. |
| 26 | .\" Set up some character translations and predefined strings. \*(-- will |
| 27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left |
| 28 | .\" double quote, and \*(R" will give a right double quote. | will give a |
| 29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to |
| 30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' |
| 31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. |
| 32 | .tr \(*W-|\(bv\*(Tr |
| 33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
| 34 | .ie n \{\ |
| 35 | . ds -- \(*W- |
| 36 | . ds PI pi |
| 37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch |
| 38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch |
| 39 | . ds L" "" |
| 40 | . ds R" "" |
| 41 | . ds C` "" |
| 42 | . ds C' "" |
| 43 | 'br\} |
| 44 | .el\{\ |
| 45 | . ds -- \|\(em\| |
| 46 | . ds PI \(*p |
| 47 | . ds L" `` |
| 48 | . ds R" '' |
| 49 | 'br\} |
| 50 | .\" |
| 51 | .\" If the F register is turned on, we'll generate index entries on stderr for |
| 52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index |
| 53 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
| 54 | .\" output yourself in some meaningful fashion. |
| 55 | .if \nF \{\ |
| 56 | . de IX |
| 57 | . tm Index:\\$1\t\\n%\t"\\$2" |
| 58 | .. |
| 59 | . nr % 0 |
| 60 | . rr F |
| 61 | .\} |
| 62 | .\" |
| 63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 64 | .\" way too many mistakes in technical documents. |
| 65 | .hy 0 |
| 66 | .if n .na |
| 67 | .\" |
| 68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
| 69 | .\" Fear. Run. Save yourself. No user-serviceable parts. |
| 70 | . \" fudge factors for nroff and troff |
| 71 | .if n \{\ |
| 72 | . ds #H 0 |
| 73 | . ds #V .8m |
| 74 | . ds #F .3m |
| 75 | . ds #[ \f1 |
| 76 | . ds #] \fP |
| 77 | .\} |
| 78 | .if t \{\ |
| 79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
| 80 | . ds #V .6m |
| 81 | . ds #F 0 |
| 82 | . ds #[ \& |
| 83 | . ds #] \& |
| 84 | .\} |
| 85 | . \" simple accents for nroff and troff |
| 86 | .if n \{\ |
| 87 | . ds ' \& |
| 88 | . ds ` \& |
| 89 | . ds ^ \& |
| 90 | . ds , \& |
| 91 | . ds ~ ~ |
| 92 | . ds / |
| 93 | .\} |
| 94 | .if t \{\ |
| 95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
| 96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' |
| 97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' |
| 98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' |
| 99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' |
| 100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' |
| 101 | .\} |
| 102 | . \" troff and (daisy-wheel) nroff accents |
| 103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
| 104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' |
| 105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] |
| 106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' |
| 107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' |
| 108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] |
| 109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] |
| 110 | .ds ae a\h'-(\w'a'u*4/10)'e |
| 111 | .ds Ae A\h'-(\w'A'u*4/10)'E |
| 112 | . \" corrections for vroff |
| 113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
| 114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' |
| 115 | . \" for low resolution devices (crt and lpr) |
| 116 | .if \n(.H>23 .if \n(.V>19 \ |
| 117 | \{\ |
| 118 | . ds : e |
| 119 | . ds 8 ss |
| 120 | . ds o a |
| 121 | . ds d- d\h'-1'\(ga |
| 122 | . ds D- D\h'-1'\(hy |
| 123 | . ds th \o'bp' |
| 124 | . ds Th \o'LP' |
| 125 | . ds ae ae |
| 126 | . ds Ae AE |
| 127 | .\} |
| 128 | .rm #[ #] #H #V #F C |
| 129 | .\" ======================================================================== |
| 130 | .\" |
| 131 | .IX Title "Math::Trig 3" |
| 132 | .TH Math::Trig 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide" |
| 133 | .SH "NAME" |
| 134 | Math::Trig \- trigonometric functions |
| 135 | .SH "SYNOPSIS" |
| 136 | .IX Header "SYNOPSIS" |
| 137 | .Vb 1 |
| 138 | \& use Math::Trig; |
| 139 | .Ve |
| 140 | .PP |
| 141 | .Vb 3 |
| 142 | \& $x = tan(0.9); |
| 143 | \& $y = acos(3.7); |
| 144 | \& $z = asin(2.4); |
| 145 | .Ve |
| 146 | .PP |
| 147 | .Vb 1 |
| 148 | \& $halfpi = pi/2; |
| 149 | .Ve |
| 150 | .PP |
| 151 | .Vb 1 |
| 152 | \& $rad = deg2rad(120); |
| 153 | .Ve |
| 154 | .PP |
| 155 | .Vb 2 |
| 156 | \& # Import constants pi2, pip2, pip4 (2*pi, pi/2, pi/4). |
| 157 | \& use Math::Trig ':pi'; |
| 158 | .Ve |
| 159 | .PP |
| 160 | .Vb 2 |
| 161 | \& # Import the conversions between cartesian/spherical/cylindrical. |
| 162 | \& use Math::Trig ':radial'; |
| 163 | .Ve |
| 164 | .PP |
| 165 | .Vb 2 |
| 166 | \& # Import the great circle formulas. |
| 167 | \& use Math::Trig ':great_circle'; |
| 168 | .Ve |
| 169 | .SH "DESCRIPTION" |
| 170 | .IX Header "DESCRIPTION" |
| 171 | \&\f(CW\*(C`Math::Trig\*(C'\fR defines many trigonometric functions not defined by the |
| 172 | core Perl which defines only the \f(CW\*(C`sin()\*(C'\fR and \f(CW\*(C`cos()\*(C'\fR. The constant |
| 173 | \&\fBpi\fR is also defined as are a few convenience functions for angle |
| 174 | conversions, and \fIgreat circle formulas\fR for spherical movement. |
| 175 | .SH "TRIGONOMETRIC FUNCTIONS" |
| 176 | .IX Header "TRIGONOMETRIC FUNCTIONS" |
| 177 | The tangent |
| 178 | .IP "\fBtan\fR" 4 |
| 179 | .IX Item "tan" |
| 180 | .PP |
| 181 | The cofunctions of the sine, cosine, and tangent (cosec/csc and cotan/cot |
| 182 | are aliases) |
| 183 | .PP |
| 184 | \&\fBcsc\fR, \fBcosec\fR, \fBsec\fR, \fBsec\fR, \fBcot\fR, \fBcotan\fR |
| 185 | .PP |
| 186 | The arcus (also known as the inverse) functions of the sine, cosine, |
| 187 | and tangent |
| 188 | .PP |
| 189 | \&\fBasin\fR, \fBacos\fR, \fBatan\fR |
| 190 | .PP |
| 191 | The principal value of the arc tangent of y/x |
| 192 | .PP |
| 193 | \&\fBatan2\fR(y, x) |
| 194 | .PP |
| 195 | The arcus cofunctions of the sine, cosine, and tangent (acosec/acsc |
| 196 | and acotan/acot are aliases) |
| 197 | .PP |
| 198 | \&\fBacsc\fR, \fBacosec\fR, \fBasec\fR, \fBacot\fR, \fBacotan\fR |
| 199 | .PP |
| 200 | The hyperbolic sine, cosine, and tangent |
| 201 | .PP |
| 202 | \&\fBsinh\fR, \fBcosh\fR, \fBtanh\fR |
| 203 | .PP |
| 204 | The cofunctions of the hyperbolic sine, cosine, and tangent (cosech/csch |
| 205 | and cotanh/coth are aliases) |
| 206 | .PP |
| 207 | \&\fBcsch\fR, \fBcosech\fR, \fBsech\fR, \fBcoth\fR, \fBcotanh\fR |
| 208 | .PP |
| 209 | The arcus (also known as the inverse) functions of the hyperbolic |
| 210 | sine, cosine, and tangent |
| 211 | .PP |
| 212 | \&\fBasinh\fR, \fBacosh\fR, \fBatanh\fR |
| 213 | .PP |
| 214 | The arcus cofunctions of the hyperbolic sine, cosine, and tangent |
| 215 | (acsch/acosech and acoth/acotanh are aliases) |
| 216 | .PP |
| 217 | \&\fBacsch\fR, \fBacosech\fR, \fBasech\fR, \fBacoth\fR, \fBacotanh\fR |
| 218 | .PP |
| 219 | The trigonometric constant \fBpi\fR is also defined. |
| 220 | .PP |
| 221 | $pi2 = 2 * \fBpi\fR; |
| 222 | .Sh "\s-1ERRORS\s0 \s-1DUE\s0 \s-1TO\s0 \s-1DIVISION\s0 \s-1BY\s0 \s-1ZERO\s0" |
| 223 | .IX Subsection "ERRORS DUE TO DIVISION BY ZERO" |
| 224 | The following functions |
| 225 | .PP |
| 226 | .Vb 14 |
| 227 | \& acoth |
| 228 | \& acsc |
| 229 | \& acsch |
| 230 | \& asec |
| 231 | \& asech |
| 232 | \& atanh |
| 233 | \& cot |
| 234 | \& coth |
| 235 | \& csc |
| 236 | \& csch |
| 237 | \& sec |
| 238 | \& sech |
| 239 | \& tan |
| 240 | \& tanh |
| 241 | .Ve |
| 242 | .PP |
| 243 | cannot be computed for all arguments because that would mean dividing |
| 244 | by zero or taking logarithm of zero. These situations cause fatal |
| 245 | runtime errors looking like this |
| 246 | .PP |
| 247 | .Vb 3 |
| 248 | \& cot(0): Division by zero. |
| 249 | \& (Because in the definition of cot(0), the divisor sin(0) is 0) |
| 250 | \& Died at ... |
| 251 | .Ve |
| 252 | .PP |
| 253 | or |
| 254 | .PP |
| 255 | .Vb 2 |
| 256 | \& atanh(-1): Logarithm of zero. |
| 257 | \& Died at... |
| 258 | .Ve |
| 259 | .PP |
| 260 | For the \f(CW\*(C`csc\*(C'\fR, \f(CW\*(C`cot\*(C'\fR, \f(CW\*(C`asec\*(C'\fR, \f(CW\*(C`acsc\*(C'\fR, \f(CW\*(C`acot\*(C'\fR, \f(CW\*(C`csch\*(C'\fR, \f(CW\*(C`coth\*(C'\fR, |
| 261 | \&\f(CW\*(C`asech\*(C'\fR, \f(CW\*(C`acsch\*(C'\fR, the argument cannot be \f(CW0\fR (zero). For the |
| 262 | \&\f(CW\*(C`atanh\*(C'\fR, \f(CW\*(C`acoth\*(C'\fR, the argument cannot be \f(CW1\fR (one). For the |
| 263 | \&\f(CW\*(C`atanh\*(C'\fR, \f(CW\*(C`acoth\*(C'\fR, the argument cannot be \f(CW\*(C`\-1\*(C'\fR (minus one). For the |
| 264 | \&\f(CW\*(C`tan\*(C'\fR, \f(CW\*(C`sec\*(C'\fR, \f(CW\*(C`tanh\*(C'\fR, \f(CW\*(C`sech\*(C'\fR, the argument cannot be \fIpi/2 + k * |
| 265 | pi\fR, where \fIk\fR is any integer. atan2(0, 0) is undefined. |
| 266 | .Sh "\s-1SIMPLE\s0 (\s-1REAL\s0) \s-1ARGUMENTS\s0, \s-1COMPLEX\s0 \s-1RESULTS\s0" |
| 267 | .IX Subsection "SIMPLE (REAL) ARGUMENTS, COMPLEX RESULTS" |
| 268 | Please note that some of the trigonometric functions can break out |
| 269 | from the \fBreal axis\fR into the \fBcomplex plane\fR. For example |
| 270 | \&\f(CWasin(2)\fR has no definition for plain real numbers but it has |
| 271 | definition for complex numbers. |
| 272 | .PP |
| 273 | In Perl terms this means that supplying the usual Perl numbers (also |
| 274 | known as scalars, please see perldata) as input for the |
| 275 | trigonometric functions might produce as output results that no more |
| 276 | are simple real numbers: instead they are complex numbers. |
| 277 | .PP |
| 278 | The \f(CW\*(C`Math::Trig\*(C'\fR handles this by using the \f(CW\*(C`Math::Complex\*(C'\fR package |
| 279 | which knows how to handle complex numbers, please see Math::Complex |
| 280 | for more information. In practice you need not to worry about getting |
| 281 | complex numbers as results because the \f(CW\*(C`Math::Complex\*(C'\fR takes care of |
| 282 | details like for example how to display complex numbers. For example: |
| 283 | .PP |
| 284 | .Vb 1 |
| 285 | \& print asin(2), "\en"; |
| 286 | .Ve |
| 287 | .PP |
| 288 | should produce something like this (take or leave few last decimals): |
| 289 | .PP |
| 290 | .Vb 1 |
| 291 | \& 1.5707963267949-1.31695789692482i |
| 292 | .Ve |
| 293 | .PP |
| 294 | That is, a complex number with the real part of approximately \f(CW1.571\fR |
| 295 | and the imaginary part of approximately \f(CW\*(C`\-1.317\*(C'\fR. |
| 296 | .SH "PLANE ANGLE CONVERSIONS" |
| 297 | .IX Header "PLANE ANGLE CONVERSIONS" |
| 298 | (Plane, 2\-dimensional) angles may be converted with the following functions. |
| 299 | .PP |
| 300 | .Vb 2 |
| 301 | \& $radians = deg2rad($degrees); |
| 302 | \& $radians = grad2rad($gradians); |
| 303 | .Ve |
| 304 | .PP |
| 305 | .Vb 2 |
| 306 | \& $degrees = rad2deg($radians); |
| 307 | \& $degrees = grad2deg($gradians); |
| 308 | .Ve |
| 309 | .PP |
| 310 | .Vb 2 |
| 311 | \& $gradians = deg2grad($degrees); |
| 312 | \& $gradians = rad2grad($radians); |
| 313 | .Ve |
| 314 | .PP |
| 315 | The full circle is 2 \fIpi\fR radians or \fI360\fR degrees or \fI400\fR gradians. |
| 316 | The result is by default wrapped to be inside the [0, {2pi,360,400}[ circle. |
| 317 | If you don't want this, supply a true second argument: |
| 318 | .PP |
| 319 | .Vb 2 |
| 320 | \& $zillions_of_radians = deg2rad($zillions_of_degrees, 1); |
| 321 | \& $negative_degrees = rad2deg($negative_radians, 1); |
| 322 | .Ve |
| 323 | .PP |
| 324 | You can also do the wrapping explicitly by \fIrad2rad()\fR, \fIdeg2deg()\fR, and |
| 325 | \&\fIgrad2grad()\fR. |
| 326 | .SH "RADIAL COORDINATE CONVERSIONS" |
| 327 | .IX Header "RADIAL COORDINATE CONVERSIONS" |
| 328 | \&\fBRadial coordinate systems\fR are the \fBspherical\fR and the \fBcylindrical\fR |
| 329 | systems, explained shortly in more detail. |
| 330 | .PP |
| 331 | You can import radial coordinate conversion functions by using the |
| 332 | \&\f(CW\*(C`:radial\*(C'\fR tag: |
| 333 | .PP |
| 334 | .Vb 1 |
| 335 | \& use Math::Trig ':radial'; |
| 336 | .Ve |
| 337 | .PP |
| 338 | .Vb 6 |
| 339 | \& ($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z); |
| 340 | \& ($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z); |
| 341 | \& ($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z); |
| 342 | \& ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z); |
| 343 | \& ($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi); |
| 344 | \& ($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi); |
| 345 | .Ve |
| 346 | .PP |
| 347 | \&\fBAll angles are in radians\fR. |
| 348 | .Sh "\s-1COORDINATE\s0 \s-1SYSTEMS\s0" |
| 349 | .IX Subsection "COORDINATE SYSTEMS" |
| 350 | \&\fBCartesian\fR coordinates are the usual rectangular \fI(x, y, z)\fR\-coordinates. |
| 351 | .PP |
| 352 | Spherical coordinates, \fI(rho, theta, pi)\fR, are three-dimensional |
| 353 | coordinates which define a point in three-dimensional space. They are |
| 354 | based on a sphere surface. The radius of the sphere is \fBrho\fR, also |
| 355 | known as the \fIradial\fR coordinate. The angle in the \fIxy\fR\-plane |
| 356 | (around the \fIz\fR\-axis) is \fBtheta\fR, also known as the \fIazimuthal\fR |
| 357 | coordinate. The angle from the \fIz\fR\-axis is \fBphi\fR, also known as the |
| 358 | \&\fIpolar\fR coordinate. The North Pole is therefore \fI0, 0, rho\fR, and |
| 359 | the Gulf of Guinea (think of the missing big chunk of Africa) \fI0, |
| 360 | pi/2, rho\fR. In geographical terms \fIphi\fR is latitude (northward |
| 361 | positive, southward negative) and \fItheta\fR is longitude (eastward |
| 362 | positive, westward negative). |
| 363 | .PP |
| 364 | \&\fB\s-1BEWARE\s0\fR: some texts define \fItheta\fR and \fIphi\fR the other way round, |
| 365 | some texts define the \fIphi\fR to start from the horizontal plane, some |
| 366 | texts use \fIr\fR in place of \fIrho\fR. |
| 367 | .PP |
| 368 | Cylindrical coordinates, \fI(rho, theta, z)\fR, are three-dimensional |
| 369 | coordinates which define a point in three-dimensional space. They are |
| 370 | based on a cylinder surface. The radius of the cylinder is \fBrho\fR, |
| 371 | also known as the \fIradial\fR coordinate. The angle in the \fIxy\fR\-plane |
| 372 | (around the \fIz\fR\-axis) is \fBtheta\fR, also known as the \fIazimuthal\fR |
| 373 | coordinate. The third coordinate is the \fIz\fR, pointing up from the |
| 374 | \&\fBtheta\fR\-plane. |
| 375 | .Sh "3\-D \s-1ANGLE\s0 \s-1CONVERSIONS\s0" |
| 376 | .IX Subsection "3-D ANGLE CONVERSIONS" |
| 377 | Conversions to and from spherical and cylindrical coordinates are |
| 378 | available. Please notice that the conversions are not necessarily |
| 379 | reversible because of the equalities like \fIpi\fR angles being equal to |
| 380 | \&\fI\-pi\fR angles. |
| 381 | .IP "cartesian_to_cylindrical" 4 |
| 382 | .IX Item "cartesian_to_cylindrical" |
| 383 | .Vb 1 |
| 384 | \& ($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z); |
| 385 | .Ve |
| 386 | .IP "cartesian_to_spherical" 4 |
| 387 | .IX Item "cartesian_to_spherical" |
| 388 | .Vb 1 |
| 389 | \& ($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z); |
| 390 | .Ve |
| 391 | .IP "cylindrical_to_cartesian" 4 |
| 392 | .IX Item "cylindrical_to_cartesian" |
| 393 | .Vb 1 |
| 394 | \& ($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z); |
| 395 | .Ve |
| 396 | .IP "cylindrical_to_spherical" 4 |
| 397 | .IX Item "cylindrical_to_spherical" |
| 398 | .Vb 1 |
| 399 | \& ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z); |
| 400 | .Ve |
| 401 | .Sp |
| 402 | Notice that when \f(CW$z\fR is not 0 \f(CW$rho_s\fR is not equal to \f(CW$rho_c\fR. |
| 403 | .IP "spherical_to_cartesian" 4 |
| 404 | .IX Item "spherical_to_cartesian" |
| 405 | .Vb 1 |
| 406 | \& ($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi); |
| 407 | .Ve |
| 408 | .IP "spherical_to_cylindrical" 4 |
| 409 | .IX Item "spherical_to_cylindrical" |
| 410 | .Vb 1 |
| 411 | \& ($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi); |
| 412 | .Ve |
| 413 | .Sp |
| 414 | Notice that when \f(CW$z\fR is not 0 \f(CW$rho_c\fR is not equal to \f(CW$rho_s\fR. |
| 415 | .SH "GREAT CIRCLE DISTANCES AND DIRECTIONS" |
| 416 | .IX Header "GREAT CIRCLE DISTANCES AND DIRECTIONS" |
| 417 | You can compute spherical distances, called \fBgreat circle distances\fR, |
| 418 | by importing the \fIgreat_circle_distance()\fR function: |
| 419 | .PP |
| 420 | .Vb 1 |
| 421 | \& use Math::Trig 'great_circle_distance'; |
| 422 | .Ve |
| 423 | .PP |
| 424 | .Vb 1 |
| 425 | \& $distance = great_circle_distance($theta0, $phi0, $theta1, $phi1, [, $rho]); |
| 426 | .Ve |
| 427 | .PP |
| 428 | The \fIgreat circle distance\fR is the shortest distance between two |
| 429 | points on a sphere. The distance is in \f(CW$rho\fR units. The \f(CW$rho\fR is |
| 430 | optional, it defaults to 1 (the unit sphere), therefore the distance |
| 431 | defaults to radians. |
| 432 | .PP |
| 433 | If you think geographically the \fItheta\fR are longitudes: zero at the |
| 434 | Greenwhich meridian, eastward positive, westward negative\*(--and the |
| 435 | \&\fIphi\fR are latitudes: zero at the North Pole, northward positive, |
| 436 | southward negative. \fB\s-1NOTE\s0\fR: this formula thinks in mathematics, not |
| 437 | geographically: the \fIphi\fR zero is at the North Pole, not at the |
| 438 | Equator on the west coast of Africa (Bay of Guinea). You need to |
| 439 | subtract your geographical coordinates from \fIpi/2\fR (also known as 90 |
| 440 | degrees). |
| 441 | .PP |
| 442 | .Vb 2 |
| 443 | \& $distance = great_circle_distance($lon0, pi/2 - $lat0, |
| 444 | \& $lon1, pi/2 - $lat1, $rho); |
| 445 | .Ve |
| 446 | .PP |
| 447 | The direction you must follow the great circle (also known as \fIbearing\fR) |
| 448 | can be computed by the \fIgreat_circle_direction()\fR function: |
| 449 | .PP |
| 450 | .Vb 1 |
| 451 | \& use Math::Trig 'great_circle_direction'; |
| 452 | .Ve |
| 453 | .PP |
| 454 | .Vb 1 |
| 455 | \& $direction = great_circle_direction($theta0, $phi0, $theta1, $phi1); |
| 456 | .Ve |
| 457 | .PP |
| 458 | (Alias 'great_circle_bearing' is also available.) |
| 459 | The result is in radians, zero indicating straight north, pi or \-pi |
| 460 | straight south, pi/2 straight west, and \-pi/2 straight east. |
| 461 | .PP |
| 462 | You can inversely compute the destination if you know the |
| 463 | starting point, direction, and distance: |
| 464 | .PP |
| 465 | .Vb 1 |
| 466 | \& use Math::Trig 'great_circle_destination'; |
| 467 | .Ve |
| 468 | .PP |
| 469 | .Vb 2 |
| 470 | \& # thetad and phid are the destination coordinates, |
| 471 | \& # dird is the final direction at the destination. |
| 472 | .Ve |
| 473 | .PP |
| 474 | .Vb 2 |
| 475 | \& ($thetad, $phid, $dird) = |
| 476 | \& great_circle_destination($theta, $phi, $direction, $distance); |
| 477 | .Ve |
| 478 | .PP |
| 479 | or the midpoint if you know the end points: |
| 480 | .PP |
| 481 | .Vb 1 |
| 482 | \& use Math::Trig 'great_circle_midpoint'; |
| 483 | .Ve |
| 484 | .PP |
| 485 | .Vb 2 |
| 486 | \& ($thetam, $phim) = |
| 487 | \& great_circle_midpoint($theta0, $phi0, $theta1, $phi1); |
| 488 | .Ve |
| 489 | .PP |
| 490 | The \fIgreat_circle_midpoint()\fR is just a special case of |
| 491 | .PP |
| 492 | .Vb 1 |
| 493 | \& use Math::Trig 'great_circle_waypoint'; |
| 494 | .Ve |
| 495 | .PP |
| 496 | .Vb 2 |
| 497 | \& ($thetai, $phii) = |
| 498 | \& great_circle_waypoint($theta0, $phi0, $theta1, $phi1, $way); |
| 499 | .Ve |
| 500 | .PP |
| 501 | Where the \f(CW$way\fR is a value from zero ($theta0, \f(CW$phi0\fR) to one ($theta1, |
| 502 | \&\f(CW$phi1\fR). Note that antipodal points (where their distance is \fIpi\fR |
| 503 | radians) do not have waypoints between them (they would have an an |
| 504 | \&\*(L"equator\*(R" between them), and therefore \f(CW\*(C`undef\*(C'\fR is returned for |
| 505 | antipodal points. If the points are the same and the distance |
| 506 | therefore zero and all waypoints therefore identical, the first point |
| 507 | (either point) is returned. |
| 508 | .PP |
| 509 | The thetas, phis, direction, and distance in the above are all in radians. |
| 510 | .PP |
| 511 | You can import all the great circle formulas by |
| 512 | .PP |
| 513 | .Vb 1 |
| 514 | \& use Math::Trig ':great_circle'; |
| 515 | .Ve |
| 516 | .PP |
| 517 | Notice that the resulting directions might be somewhat surprising if |
| 518 | you are looking at a flat worldmap: in such map projections the great |
| 519 | circles quite often do not look like the shortest routes\*(-- but for |
| 520 | example the shortest possible routes from Europe or North America to |
| 521 | Asia do often cross the polar regions. |
| 522 | .SH "EXAMPLES" |
| 523 | .IX Header "EXAMPLES" |
| 524 | To calculate the distance between London (51.3N 0.5W) and Tokyo |
| 525 | (35.7N 139.8E) in kilometers: |
| 526 | .PP |
| 527 | .Vb 1 |
| 528 | \& use Math::Trig qw(great_circle_distance deg2rad); |
| 529 | .Ve |
| 530 | .PP |
| 531 | .Vb 5 |
| 532 | \& # Notice the 90 - latitude: phi zero is at the North Pole. |
| 533 | \& sub NESW { deg2rad($_[0]), deg2rad(90 - $_[1]) } |
| 534 | \& my @L = NESW( -0.5, 51.3); |
| 535 | \& my @T = NESW(139.8, 35.7); |
| 536 | \& my $km = great_circle_distance(@L, @T, 6378); # About 9600 km. |
| 537 | .Ve |
| 538 | .PP |
| 539 | The direction you would have to go from London to Tokyo (in radians, |
| 540 | straight north being zero, straight east being pi/2). |
| 541 | .PP |
| 542 | .Vb 1 |
| 543 | \& use Math::Trig qw(great_circle_direction); |
| 544 | .Ve |
| 545 | .PP |
| 546 | .Vb 1 |
| 547 | \& my $rad = great_circle_direction(@L, @T); # About 0.547 or 0.174 pi. |
| 548 | .Ve |
| 549 | .PP |
| 550 | The midpoint between London and Tokyo being |
| 551 | .PP |
| 552 | .Vb 1 |
| 553 | \& use Math::Trig qw(great_circle_midpoint); |
| 554 | .Ve |
| 555 | .PP |
| 556 | .Vb 1 |
| 557 | \& my @M = great_circle_midpoint(@L, @T); |
| 558 | .Ve |
| 559 | .PP |
| 560 | or about 68.11N 24.74E, in the Finnish Lapland. |
| 561 | .Sh "\s-1CAVEAT\s0 \s-1FOR\s0 \s-1GREAT\s0 \s-1CIRCLE\s0 \s-1FORMULAS\s0" |
| 562 | .IX Subsection "CAVEAT FOR GREAT CIRCLE FORMULAS" |
| 563 | The answers may be off by few percentages because of the irregular |
| 564 | (slightly aspherical) form of the Earth. The errors are at worst |
| 565 | about 0.55%, but generally below 0.3%. |
| 566 | .SH "BUGS" |
| 567 | .IX Header "BUGS" |
| 568 | Saying \f(CW\*(C`use Math::Trig;\*(C'\fR exports many mathematical routines in the |
| 569 | caller environment and even overrides some (\f(CW\*(C`sin\*(C'\fR, \f(CW\*(C`cos\*(C'\fR). This is |
| 570 | construed as a feature by the Authors, actually... ;\-) |
| 571 | .PP |
| 572 | The code is not optimized for speed, especially because we use |
| 573 | \&\f(CW\*(C`Math::Complex\*(C'\fR and thus go quite near complex numbers while doing |
| 574 | the computations even when the arguments are not. This, however, |
| 575 | cannot be completely avoided if we want things like \f(CWasin(2)\fR to give |
| 576 | an answer instead of giving a fatal runtime error. |
| 577 | .PP |
| 578 | Do not attempt navigation using these formulas. |
| 579 | .SH "AUTHORS" |
| 580 | .IX Header "AUTHORS" |
| 581 | Jarkko Hietaniemi <\fIjhi@iki.fi\fR> and |
| 582 | Raphael Manfredi <\fIRaphael_Manfredi@pobox.com\fR>. |