Skip to content

Commit

Permalink
Review type conversions in coordinate system conversions.
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmada committed Oct 21, 2023
1 parent b002b88 commit 1e7b090
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 59 deletions.
19 changes: 0 additions & 19 deletions src/NetFabric.Numerics/Cartesian2/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,4 @@ public static Polar.Point<T, Radians, T> ToPolar<T>(in Point<T> point)

return new(radius, azimuth);
}

/// <summary>
/// Converts a cartesian 2D point to polar coordinates.
/// </summary>
/// <typeparam name="T">The type of the point coordinates.</typeparam>
/// <typeparam name="TRadius">The type of the radius value.</typeparam>
/// <typeparam name="TAngle">The type of the angle values.</typeparam>
/// <param name="point">The cartesian 2D point to convert.</param>
/// <returns>The polar coordinates representing the point.</returns>
public static Polar.Point<TRadius, Radians, TAngle> ToPolar<T, TRadius, TAngle>(in Point<T> point)
where T : struct, IFloatingPointIeee754<T>, IMinMaxValue<T>
where TRadius : struct, IFloatingPoint<TRadius>, IMinMaxValue<TRadius>
where TAngle : struct, IFloatingPoint<TAngle>, IMinMaxValue<TAngle>, ITrigonometricFunctions<TAngle>
{
var radius = TRadius.CreateChecked(Utils.Magnitude(point.X, point.Y));
var azimuth = Angle<Radians, TAngle>.CreateChecked(Angle.Atan2(point.Y, point.X));

return new(radius, azimuth);
}
}
21 changes: 0 additions & 21 deletions src/NetFabric.Numerics/Cartesian3/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,25 +242,4 @@ public static Spherical.Point<T, Radians, T> ToSpherical<T>(Point<T> point)

return new(radius, azimuth, polar);
}

/// <summary>
/// Converts a cartesian 3D point to spherical coordinates.
/// </summary>
/// <typeparam name="T">The type of the point coordinates.</typeparam>
/// <param name="point">The cartesian 3D point to convert.</param>
/// <returns>The spherical coordinates representing the point.</returns>
public static Spherical.Point<TRadius, Radians, TAngle> ToSpherical<T, TRadius, TAngle>(Point<T> point)
where T : struct, IFloatingPointIeee754<T>, IMinMaxValue<T>
where TRadius : struct, IFloatingPoint<TRadius>, IMinMaxValue<TRadius>
where TAngle : struct, IFloatingPoint<TAngle>, IMinMaxValue<TAngle>, ITrigonometricFunctions<TAngle>
{
var radius = Utils.Magnitude(point.X, point.Y, point.Z);
if (radius == T.Zero)
return Spherical.Point<TRadius, Radians, TAngle>.Zero;

var azimuth = Angle<Radians, TAngle>.CreateChecked(Angle.Atan2(point.Y, point.X));
var polar = Angle<Radians, TAngle>.CreateChecked((Angle<Radians, T>)Angle.Acos(point.Z / radius));

return new(TRadius.CreateChecked(radius), Angle<Radians, TAngle>.CreateChecked(azimuth), polar);
}
}
15 changes: 9 additions & 6 deletions src/NetFabric.Numerics/Polar/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ public static PointReduced<TRadius, TAngleUnits, TAngle> Reduce<TRadius, TAngleU
public static Cartesian2.Point<T> ToCartesian<T>(Point<T, Radians, T> point)
where T : struct, IFloatingPoint<T>, IMinMaxValue<T>, ITrigonometricFunctions<T>
{
var (sin, cos) = Angle.SinCos(point.Azimuth);
return new(point.Radius * cos, point.Radius * sin);
var (sinAzimuth, cosAzimuth) = Angle.SinCos(point.Azimuth);
return new(point.Radius * cosAzimuth, point.Radius * sinAzimuth);
}

/// <summary>
Expand All @@ -392,11 +392,14 @@ public static Cartesian2.Point<T> ToCartesian<T>(Point<T, Radians, T> point)
/// <returns>The cartesian 2D coordinates representing the point.</returns>
public static Cartesian2.Point<T> ToCartesian<TRadius, TAngle, T>(Point<TRadius, Radians, TAngle> point)
where TRadius : struct, IFloatingPoint<TRadius>, IMinMaxValue<TRadius>
where TAngle : struct, IFloatingPoint<TAngle>, IMinMaxValue<TAngle>, ITrigonometricFunctions<TAngle>
where T : struct, INumber<T>, IMinMaxValue<T>
where TAngle : struct, IFloatingPoint<TAngle>, IMinMaxValue<TAngle>
where T : struct, IFloatingPoint<T>, IMinMaxValue<T>, ITrigonometricFunctions<T>
{
var x = T.CreateChecked(point.Radius * TRadius.CreateChecked(Angle.Cos(point.Azimuth)));
var y = T.CreateChecked(point.Radius * TRadius.CreateChecked(Angle.Sin(point.Azimuth)));
var (sinAzimuth, cosAzimuth) = Angle.SinCos(Angle<Radians, T>.CreateChecked(point.Azimuth));
var radius = T.CreateChecked(point.Radius);

var x = radius * cosAzimuth;
var y = radius * sinAzimuth;

return new(x, y);
}
Expand Down
25 changes: 12 additions & 13 deletions src/NetFabric.Numerics/Spherical/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,9 @@ public static Cartesian3.Point<T> ToCartesian<T>(Point<T, Radians, T> point)
var (sinAzimuth, cosAzimuth) = Angle.SinCos(point.Azimuth);
var (sinPolar, cosPolar) = Angle.SinCos(point.Polar);

var x = T.CreateChecked(point.Radius * sinPolar * cosAzimuth);
var y = T.CreateChecked(point.Radius * sinPolar * sinAzimuth);
var z = T.CreateChecked(point.Radius * cosPolar);
var x = point.Radius * sinPolar * cosAzimuth;
var y = point.Radius * sinPolar * sinAzimuth;
var z = point.Radius * cosPolar;

return new(x, y, z);
}
Expand All @@ -412,17 +412,16 @@ public static Cartesian3.Point<T> ToCartesian<T>(Point<T, Radians, T> point)
/// <returns>The cartesian 3D coordinates representing the point.</returns>
public static Cartesian3.Point<T> ToCartesian<TRadius, TAngle, T>(Point<TRadius, Radians, TAngle> point)
where TRadius : struct, IFloatingPoint<TRadius>, IMinMaxValue<TRadius>
where TAngle : struct, IFloatingPoint<TAngle>, IMinMaxValue<TAngle>, ITrigonometricFunctions<TAngle>
where T : struct, INumber<T>, IMinMaxValue<T>
where TAngle : struct, IFloatingPoint<TAngle>, IMinMaxValue<TAngle>
where T : struct, IFloatingPoint<T>, IMinMaxValue<T>, ITrigonometricFunctions<T>
{
var sinAzimuth = Angle.Sin(point.Azimuth);
var cosAzimuth = Angle.Cos(point.Azimuth);
var sinPolar = Angle.Sin(point.Polar);
var cosPolar = Angle.Cos(point.Polar);

var x = T.CreateChecked(point.Radius * TRadius.CreateChecked(sinPolar * cosAzimuth));
var y = T.CreateChecked(point.Radius * TRadius.CreateChecked(sinPolar * sinAzimuth));
var z = T.CreateChecked(point.Radius * TRadius.CreateChecked(cosPolar));
var (sinAzimuth, cosAzimuth) = Angle.SinCos(Angle<Radians, T>.CreateChecked(point.Azimuth));
var (sinPolar, cosPolar) = Angle.SinCos(Angle<Radians, T>.CreateChecked(point.Polar));
var radius = T.CreateChecked(point.Radius);

var x = radius * sinPolar * cosAzimuth;
var y = radius * sinPolar * sinAzimuth;
var z = radius * cosPolar;

return new(x, y, z);
}
Expand Down

0 comments on commit 1e7b090

Please sign in to comment.