|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2024 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +* |
| 18 | +* |
| 19 | +* ## Notice |
| 20 | +* |
| 21 | +* The original code, copyright and license are from [Go]{@link https://golang.org/src/math/atan2f.go}. The implementation follows the original, but has been modified for JavaScript. |
| 22 | +* |
| 23 | +* ```text |
| 24 | +* Copyright (c) 2009 The Go Authors. All rights reserved. |
| 25 | +* |
| 26 | +* Redistribution and use in source and binary forms, with or withoutf |
| 27 | +* modification, are permitted provided that the following conditions are |
| 28 | +* met: |
| 29 | +* |
| 30 | +* * Redistributions of source code must retain the above copyright |
| 31 | +* notice, this list of conditions and the following disclaimer. |
| 32 | +* * Redistributions in binary form must reproduce the above |
| 33 | +* copyright notice, this list of conditions and the following disclaimer |
| 34 | +* in the documentation and/or other materials provided with the |
| 35 | +* distribution. |
| 36 | +* * Neither the name of Google Inc. nor the names of its |
| 37 | +* contributors may be used to endorse or promote products derived from |
| 38 | +* this software without specific prior written permission. |
| 39 | +* |
| 40 | +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 41 | +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 42 | +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 43 | +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 44 | +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 45 | +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 46 | +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 47 | +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 48 | +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 49 | +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 50 | +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 51 | +* ``` |
| 52 | +*/ |
| 53 | + |
| 54 | +'use strict'; |
| 55 | + |
| 56 | +// MODULES // |
| 57 | + |
| 58 | +var isinfinite = require( '@stdlib/math/base/assert/is-infinite' ); |
| 59 | +var copysign = require( '@stdlib/math/base/special/copysign' ); |
| 60 | +var signbit = require( '@stdlib/number/float64/base/signbit' ); |
| 61 | +var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); |
| 62 | +var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); |
| 63 | +var atan2 = require( '@stdlib/math/base/special/atan2' ); |
| 64 | +var PINF = require( '@stdlib/constants/float64/pinf' ); |
| 65 | +var PI = require( '@stdlib/constants/float64/pi' ); |
| 66 | +var PI02F = require( '@stdlib/constants/float32/half-pi' ); |
| 67 | +var PI04F = require( '@stdlib/constants/float32/fourth-pi' ); |
| 68 | + |
| 69 | + |
| 70 | +// MAIN // |
| 71 | + |
| 72 | +/** |
| 73 | +* Computes the angle in the plane (in radians) between the positive x-axis and the ray from `(0,0)` to the point `(x,y)`. |
| 74 | +* |
| 75 | +* @param {number} y - `y` coordinate |
| 76 | +* @param {number} x - `x` coordinate |
| 77 | +* @returns {number} angle (in radians) |
| 78 | +* |
| 79 | +* @example |
| 80 | +* var v = atan2f( 2.0, 2.0 ); // => atanf(1.0) |
| 81 | +* // returns ~0.785 |
| 82 | +* |
| 83 | +* @example |
| 84 | +* var v = atan2f( 6.0, 2.0 ); // => atanf(3.0) |
| 85 | +* // returns ~1.249 |
| 86 | +* |
| 87 | +* @example |
| 88 | +* var v = atan2f( -1.0, -1.0 ); // => atanf(1.0) - π |
| 89 | +* // returns ~-2.356 |
| 90 | +* |
| 91 | +* @example |
| 92 | +* var v = atan2f( 3.0, 0.0 ); // => π/2 |
| 93 | +* // returns ~1.571 |
| 94 | +* |
| 95 | +* @example |
| 96 | +* var v = atan2f( -2.0, 0.0 ); // => -π/2 |
| 97 | +* // returns ~-1.571 |
| 98 | +* |
| 99 | +* @example |
| 100 | +* var v = atan2f( 0.0, 0.0 ); |
| 101 | +* // returns 0.0 |
| 102 | +* |
| 103 | +* @example |
| 104 | +* var v = atan2f( 3.0, NaN ); |
| 105 | +* // returns NaN |
| 106 | +* |
| 107 | +* @example |
| 108 | +* var v = atan2f( NaN, 2.0 ); |
| 109 | +* // returns NaN |
| 110 | +*/ |
| 111 | +function atan2f( y, x ) { |
| 112 | + var q; |
| 113 | + if ( isnanf( x ) || isnanf( y ) ) { |
| 114 | + return NaN; |
| 115 | + } |
| 116 | + x = float64ToFloat32( x ); |
| 117 | + if ( isinfinite( x ) ) { |
| 118 | + if ( x === PINF ) { |
| 119 | + y = float64ToFloat32( y ); |
| 120 | + if ( isinfinite( y ) ) { |
| 121 | + return copysign( PI04F, y ); |
| 122 | + } |
| 123 | + return copysign( 0.0, y ); |
| 124 | + } |
| 125 | + // Case: x is -Infinity |
| 126 | + if ( isinfinite( y ) ) { |
| 127 | + return copysign( 3.0*PI04F, y ); |
| 128 | + } |
| 129 | + return copysign( PI, y ); |
| 130 | + } |
| 131 | + if ( isinfinite( y ) ) { |
| 132 | + return copysign( PI02F, y ); |
| 133 | + } |
| 134 | + if ( y === 0.0 ) { |
| 135 | + if ( x >= 0.0 && !signbit( x ) ) { |
| 136 | + return copysign( 0.0, y ); |
| 137 | + } |
| 138 | + return copysign( PI, y ); |
| 139 | + } |
| 140 | + if ( x === 0.0 ) { |
| 141 | + return copysign( PI02F, y ); |
| 142 | + } |
| 143 | + q = atan2( y / x ); |
| 144 | + if ( x < 0.0 ) { |
| 145 | + if ( q <= 0.0 ) { |
| 146 | + return q + PI; |
| 147 | + } |
| 148 | + return q - PI; |
| 149 | + } |
| 150 | + return q; |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | +// EXPORTS // |
| 155 | + |
| 156 | +module.exports = atan2f; |
0 commit comments