question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Maths: struct representing an Angle

See original GitHub issue

Summary of feature

In geometry, I work with angles quite often, so I ended having an Angle structure which has proven a lot more useful than I imagined, because it handles stuff like:

  • Radian/Degree conversion
  • Angle between vectors
  • radial arithmetics (an angle can represent a direction, but also the number of turns)
  • Equality (which is tricky; is 0° == 360° ? it depends on the context)

Once you think about it, angles have their own mathematical domain that has always been very underrated.

Taken from my code, it would be something like this:

struct Angle
{
    public float Radians;

    public float Degrees => Radians * 180 / Math.Pi;

    public static implicit operator float(Angle angle) => angle.Radians;

    public static Angle InDegrees(float degrees) ... 
    public static Angle InRadians(float radians) ... 
    public static Angle InClockTime(int hour, int min, int second) ... 
    public static Angle Between(Vector2 a, Vector2 b) ...
    public static Angle Between(Vector3 a, Vector3 b) ...
    public static Angle ArcTan(float y, float x) ...
    public static Angle ArcCos(float cos) ...
    public static Angle ArcSin(float sin) ...
    public static Angle Round(Angle angle) ... rounds any angle to 0°-360° range
    public static operator Angle +(Angle a, Angle b)....
}

Comments

The biggest advantage I had by using such a structure is that it greatly reduces the extremely common case of using radians in place of degrees or the other way around, in other words: it makes the concept of an angle value strong typed.

And it makes the code a lot more pleasant:

var angle = Angle.InDegrees(90) + Angle.Between( vector1, vector2 );

Does this have a proposal?

Check the documentation/proposals folder. If it doesn’t have one, you may need to create one if you’re making massive breaking changes.

Issue Analytics

  • State:open
  • Created 9 months ago
  • Reactions:1
  • Comments:21 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
Perkseycommented, Jan 7, 2023

FYI here’s the discussion recording, the notes probably don’t go into all of the details of our discussion: https://youtu.be/gjneerMeRVU?t=3720

1reaction
HurricanKaicommented, Dec 14, 2022

readonly struct Radians { public readonly float Value } is preferable IMO. For maths it’d be on theme to make it take <T>, but I don’t really mind either option.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AngleSingle Struct
Mathematics.AngleSingle instance that represents the full rotation angle (i.e. 360° or 2π). Gradians. Gets or sets the total ...
Read more >
angle structures and normal surfaces - Mathematics Department
An angle structure of an ideal triangulation is an assignment of a real number, termed angle, in the range. (0,π) to each edge...
Read more >
Eleventh Week Examples --- Structures
We set up structures that represent geometric entities: point, segment, triangle and tetrahedron. The fact that we can view, say, a triangle as...
Read more >
Angle - Math.NET Spatial Documentation
A new instance of the Angle struct. Angle Parse(string value, IFormatProvider formatProvider). Attempts to convert a string into an Angle ...
Read more >
Angles: Definition, Representation, Types, Examples, and ...
The angle is represented using the symbol '∠'. It is measured in degrees or radians. The study of angle is highly useful in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found