Add Heading enum

This commit is contained in:
Oliver Booth 2022-12-05 19:53:19 +00:00
parent 5e30ef3877
commit 68fa2203d6
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
3 changed files with 154 additions and 0 deletions

View File

@ -5,6 +5,21 @@
/// </summary>
public readonly partial struct Coordinates : IEquatable<Coordinates>
{
/// <summary>
/// Initializes a new instance of the <see cref="Coordinates" /> struct.
/// </summary>
/// <param name="x">The X coordinate.</param>
/// <param name="y">The Y coordinate.</param>
/// <param name="z">The Z coordinate.</param>
/// <param name="yaw">The yaw.</param>
/// <param name="isRelative">
/// <see langword="true" /> if these coordinates represent relative coordinates; <see langword="false" /> otherwise.
/// </param>
public Coordinates(double x, double y, double z, Heading yaw, bool isRelative = false)
: this(null, x, y, z, yaw.ToHeading(), isRelative)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Coordinates" /> struct.
/// </summary>
@ -20,6 +35,22 @@ public readonly partial struct Coordinates : IEquatable<Coordinates>
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Coordinates" /> struct.
/// </summary>
/// <param name="world">The world name.</param>
/// <param name="x">The X coordinate.</param>
/// <param name="y">The Y coordinate.</param>
/// <param name="z">The Z coordinate.</param>
/// <param name="yaw">The yaw.</param>
/// <param name="isRelative">
/// <see langword="true" /> if these coordinates represent relative coordinates; <see langword="false" /> otherwise.
/// </param>
public Coordinates(string? world, double x, double y, double z, Heading yaw, bool isRelative = false)
: this(world, x, y, z, yaw.ToHeading(), isRelative)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Coordinates" /> struct.
/// </summary>
@ -41,6 +72,16 @@ public readonly partial struct Coordinates : IEquatable<Coordinates>
IsRelative = isRelative;
}
/// <summary>
/// Gets or initializes the heading.
/// </summary>
/// <value>The heading.</value>
public Heading Heading
{
get => Yaw.ToHeading();
init => Yaw = value.ToHeading();
}
/// <summary>
/// Gets or initializes a value indicating whether this instance represents relative coordinates.
/// </summary>

59
VpSharp/src/Heading.cs Normal file
View File

@ -0,0 +1,59 @@
using System.ComponentModel;
// ReSharper disable InconsistentNaming
namespace VpSharp;
/// <summary>
/// An enumeration of headings.
/// </summary>
public enum Heading
{
/// <summary>
/// North.
/// </summary>
[Description("North")]
N,
/// <summary>
/// Northeast.
/// </summary>
[Description("Northeast")]
NE,
/// <summary>
/// East.
/// </summary>
[Description("East")]
E,
/// <summary>
/// Southeast.
/// </summary>
[Description("Southeast")]
SE,
/// <summary>
/// South.
/// </summary>
[Description("South")]
S,
/// <summary>
/// Southwest.
/// </summary>
[Description("Southwest")]
SW,
/// <summary>
/// West.
/// </summary>
[Description("West")]
W,
/// <summary>
/// Northwest.
/// </summary>
[Description("Northwest")]
NW
}

View File

@ -0,0 +1,54 @@
namespace VpSharp;
/// <summary>
/// Extension methods to convert to/from <see cref="Heading" /> and <see cref="float" />.
/// </summary>
public static class HeadingExtensions
{
/// <summary>
/// Converts a <see cref="Heading" /> to an angle representing the heading.
/// </summary>
/// <param name="heading">The heading to convert.</param>
/// <returns>A value from <see cref="Heading" />.</returns>
public static Heading ToHeading(this double heading)
{
heading %= 360;
if (heading < 0)
{
heading += 360;
}
return ((int)heading / 45) switch
{
1 => Heading.NE,
2 => Heading.E,
3 => Heading.SE,
4 => Heading.S,
5 => Heading.SW,
6 => Heading.W,
7 => Heading.NW,
_ => Heading.N
};
}
/// <summary>
/// Converts a <see cref="Heading" /> to an angle representing the heading.
/// </summary>
/// <param name="direction">The heading to convert.</param>
/// <returns>The heading, in degrees.</returns>
public static double ToHeading(this Heading direction)
{
return direction switch
{
Heading.N => 0,
Heading.NE => 45,
Heading.E => 90,
Heading.SE => 135,
Heading.S => 180,
Heading.SW => 225,
Heading.W => 270,
Heading.NW => 315,
_ => 0
};
}
}