From 68fa2203d6b7a9b630b10ca836f04eb807e0d872 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Mon, 5 Dec 2022 19:53:19 +0000 Subject: [PATCH] Add Heading enum --- VpSharp/src/Coordinates.cs | 41 ++++++++++++++++++++++ VpSharp/src/Heading.cs | 59 ++++++++++++++++++++++++++++++++ VpSharp/src/HeadingExtensions.cs | 54 +++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 VpSharp/src/Heading.cs create mode 100644 VpSharp/src/HeadingExtensions.cs diff --git a/VpSharp/src/Coordinates.cs b/VpSharp/src/Coordinates.cs index e433cf5..456c843 100644 --- a/VpSharp/src/Coordinates.cs +++ b/VpSharp/src/Coordinates.cs @@ -5,6 +5,21 @@ /// public readonly partial struct Coordinates : IEquatable { + /// + /// Initializes a new instance of the struct. + /// + /// The X coordinate. + /// The Y coordinate. + /// The Z coordinate. + /// The yaw. + /// + /// if these coordinates represent relative coordinates; otherwise. + /// + public Coordinates(double x, double y, double z, Heading yaw, bool isRelative = false) + : this(null, x, y, z, yaw.ToHeading(), isRelative) + { + } + /// /// Initializes a new instance of the struct. /// @@ -20,6 +35,22 @@ public readonly partial struct Coordinates : IEquatable { } + /// + /// Initializes a new instance of the struct. + /// + /// The world name. + /// The X coordinate. + /// The Y coordinate. + /// The Z coordinate. + /// The yaw. + /// + /// if these coordinates represent relative coordinates; otherwise. + /// + public Coordinates(string? world, double x, double y, double z, Heading yaw, bool isRelative = false) + : this(world, x, y, z, yaw.ToHeading(), isRelative) + { + } + /// /// Initializes a new instance of the struct. /// @@ -41,6 +72,16 @@ public readonly partial struct Coordinates : IEquatable IsRelative = isRelative; } + /// + /// Gets or initializes the heading. + /// + /// The heading. + public Heading Heading + { + get => Yaw.ToHeading(); + init => Yaw = value.ToHeading(); + } + /// /// Gets or initializes a value indicating whether this instance represents relative coordinates. /// diff --git a/VpSharp/src/Heading.cs b/VpSharp/src/Heading.cs new file mode 100644 index 0000000..6d6cebf --- /dev/null +++ b/VpSharp/src/Heading.cs @@ -0,0 +1,59 @@ +using System.ComponentModel; + +// ReSharper disable InconsistentNaming + +namespace VpSharp; + +/// +/// An enumeration of headings. +/// +public enum Heading +{ + /// + /// North. + /// + [Description("North")] + N, + + /// + /// Northeast. + /// + [Description("Northeast")] + NE, + + /// + /// East. + /// + [Description("East")] + E, + + /// + /// Southeast. + /// + [Description("Southeast")] + SE, + + /// + /// South. + /// + [Description("South")] + S, + + /// + /// Southwest. + /// + [Description("Southwest")] + SW, + + /// + /// West. + /// + [Description("West")] + W, + + /// + /// Northwest. + /// + [Description("Northwest")] + NW +} diff --git a/VpSharp/src/HeadingExtensions.cs b/VpSharp/src/HeadingExtensions.cs new file mode 100644 index 0000000..99f16c5 --- /dev/null +++ b/VpSharp/src/HeadingExtensions.cs @@ -0,0 +1,54 @@ +namespace VpSharp; + +/// +/// Extension methods to convert to/from and . +/// +public static class HeadingExtensions +{ + /// + /// Converts a to an angle representing the heading. + /// + /// The heading to convert. + /// A value from . + 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 + }; + } + + /// + /// Converts a to an angle representing the heading. + /// + /// The heading to convert. + /// The heading, in degrees. + 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 + }; + } +}