Skip to content

Commit 412dfc2

Browse files
kythyriaKeavon
andauthored
Make the 'Tangent Inverse' node accept DVec2 in lieu of the separate atan2 node (#2516)
* Make the Tangent Inverse node accept DVec2 If given a DVec2 it will be atan2 instead of normal atan. * Remove the now-redundant atan2 node * Doc comment --------- Co-authored-by: Keavon Chambers <keavon@keavon.com>
1 parent 86e6923 commit 412dfc2

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

node-graph/gcore/src/ops.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -219,23 +219,35 @@ fn cosine_inverse<U: num_traits::float::Float>(_: impl Ctx, #[implementations(f6
219219
if radians { value.acos() } else { value.acos().to_degrees() }
220220
}
221221

222-
/// The inverse tangent trigonometric function (atan) calculates the angle whose tangent is the specified value.
222+
/// The inverse tangent trigonometric function (atan or atan2, depending on input type) calculates:
223+
/// atan: the angle whose tangent is the specified scalar number.
224+
/// atan2: the angle of a ray from the origin to the specified vector2 point.
223225
#[node_macro::node(category("Math: Trig"))]
224-
fn tangent_inverse<U: num_traits::float::Float>(_: impl Ctx, #[implementations(f64, f32)] value: U, radians: bool) -> U {
225-
if radians { value.atan() } else { value.atan().to_degrees() }
226+
fn tangent_inverse<U: TangentInverse>(_: impl Ctx, #[implementations(f64, f32, DVec2)] value: U, radians: bool) -> U::Output {
227+
value.atan(radians)
226228
}
227229

228-
/// The inverse tangent trigonometric function (atan2) calculates the angle whose tangent is the ratio of the two specified values.
229-
#[node_macro::node(name("Tangent Inverse 2-Argument"), category("Math: Trig"))]
230-
fn tangent_inverse_2_argument<U: num_traits::float::Float>(
231-
_: impl Ctx,
232-
#[implementations(f64, f32)] y: U,
233-
#[expose]
234-
#[implementations(f64, f32)]
235-
x: U,
236-
radians: bool,
237-
) -> U {
238-
if radians { y.atan2(x) } else { y.atan2(x).to_degrees() }
230+
pub trait TangentInverse {
231+
type Output: num_traits::float::Float;
232+
fn atan(self, radians: bool) -> Self::Output;
233+
}
234+
impl TangentInverse for f32 {
235+
type Output = f32;
236+
fn atan(self, radians: bool) -> Self::Output {
237+
if radians { self.atan() } else { self.atan().to_degrees() }
238+
}
239+
}
240+
impl TangentInverse for f64 {
241+
type Output = f64;
242+
fn atan(self, radians: bool) -> Self::Output {
243+
if radians { self.atan() } else { self.atan().to_degrees() }
244+
}
245+
}
246+
impl TangentInverse for glam::DVec2 {
247+
type Output = f64;
248+
fn atan(self, radians: bool) -> Self::Output {
249+
if radians { self.y.atan2(self.x) } else { self.y.atan2(self.x).to_degrees() }
250+
}
239251
}
240252

241253
/// The random function (rand) converts a seed into a random number within the specified range, inclusive of the minimum and exclusive of the maximum. The minimum and maximum values are automatically swapped if they are reversed.

0 commit comments

Comments
 (0)