Why would I need an Angle structure?

Angles are usually represented as a floating-point. Its value is usually in radians but some frameworks use degrees, or even both. Unity, for example, uses radians in Mathf but degrees in Transform. The only way to find this is by reading the documentation. There’s nothing explicit in the method declaration and the compiler cannot make any validation.

There is also nothing in Mathf, or in System.Math, that tells the developer or the compiler that Sin() takes an angle value and returns a floating-point value, while Asin() is the other way around.

NetFabric.Angle implements a .NET structure that represents an angle, giving you efficient type-safety and units-safety.

Type-safety

When using the Angle structure it’s possible to impose rules like “an angle can only be added to another angle”.

Looking at the method Asin below, you can immediately see that it returns an Angle. The compiler will throw an error when you try to assign it to anything else.

double Sin (Angle angle);
Angle Asin(double value);

You also don’t have to worry about what units were used to create the angle, simplifying code and documentation.

Units-safety

To create an Angle you have to explicitly specify the units you are using:

var right0 = Angle.FromRadian(Math.PI / 2.0);
var right1 = Angle.FromDegrees(90.0);
var right2 = Angle.FromGradian(100.0);

Same when you want to get the value:

var radians = angle.ToRadians();
var degrees = angle.ToDegrees();
var gradians = angle.ToGradians();

When using math operations, comparison, trigonometry, linear interpolation and so on, you don’t have to worry about units.

Cross-platform

Angles are used on any platform. From micro-controllers to data centers, passing by mobile devices and gaming consoles. NetFabric.Angle is available for as many .NET flavors as possible. It’s available as a Nuget package and as a .unitypackage.

The source project structure to achieve this is described on my previous post.

IDE integration

NetFabric.Angle comes with debugger visualization metadata that is recognized by Visual Studio. When stopped at a breakpoint and the mouse cursor hovering on an Angle variable, the DataTip shows the variable value in degrees and when expanded shows the value in all angle measurement units. This makes it easier to understand the value of the variable.

NetFabric.Angle.DataTip.Expanded

The Unity package includes extension methods for Unity classes like Quaternion, Transform, Vector2 and Vector3, allowing the direct use o Angle.

public class NewBehaviourScript : MonoBehaviour {

    [AngleDegreesRange(-180.0, 180.0)]
    public Angle RotateX;

    void Update () {
        transform.Rotate(Time.deltaTime * RotateX, Angle.Zero, Angle.Zero);
    }

}

The Unity package also contains a custom property drawer for Angle. For now, it contains the same slider and text box with the value in degrees as the existing floating-point angles but you gain type-safety and units-safety in the scripts. This custom drawer also takes into account the angle range metadata attribute that can be specified in any of the angle measurement units.

NetFabric.Angle.Unity

Featured image: “Angles” by Benjamin Horn

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s