This repository was archived by the owner on Jun 28, 2026. It is now read-only.
Angle changes#121
Open
metalgearsloth wants to merge 2 commits into
Open
Conversation
- Use double constants not floats, based on my benchmarks it might be slightly slower (depending on when I run it it varies but generally I think it's slower). - Use multiplication not division for constants. - Remove redundant readonlys (at least what rider tells me).
| public readonly double Theta; | ||
|
|
||
| private const double PiOver2 = Math.PI / 2.0; | ||
| private const double TwoPi = 2.0 * Math.PI; |
| /// </summary> | ||
| public readonly double Theta; | ||
|
|
||
| private const double PiOver2 = Math.PI / 2.0; |
Member
There was a problem hiding this comment.
are you sure the compiler does not already do this for you as a very obvious optimization?
| } | ||
|
|
||
| public readonly Vector2 ToWorldVec() | ||
| public Vector2 ToWorldVec() |
Member
There was a problem hiding this comment.
add documentation while you are at it
| @@ -44,7 +46,7 @@ public Angle(Vector2 dir) | |||
|
|
|||
| public static Angle FromWorldVec(Vector2 dir) | |||
Member
There was a problem hiding this comment.
documentation.
Make sure to mention why this is different for world vectors
| var theta = Theta - PiOver2; | ||
| var x = Math.Cos(theta); | ||
| var y = Math.Sin(theta); | ||
| return new Vector2((float) x, (float) y); |
Member
There was a problem hiding this comment.
Suggested change
| return new Vector2((float) x, (float) y); | |
| return new Vector2((float)x, (float)y); |
formatting convention
isn't your IDE reading the .editorconfig correctly? This should be a warning
|
|
||
| private const double Segment = 2 * Math.PI / 8.0; // Cut the circle into 8 pieces | ||
| private const double Offset = Segment / 2.0; // offset the pieces by 1/2 their size | ||
| private const double InvSegment = 1.0 / Segment; |
Member
There was a problem hiding this comment.
sure the compiler doesn't just do this for you?
|
|
||
| [Pure] | ||
| public readonly Direction GetCardinalDir() | ||
| public Direction GetCardinalDir() |
| ang += TwoPi; | ||
|
|
||
| return (Direction) (Math.Floor((ang + CardinalOffset) / CardinalSegment) * 2 % 8); | ||
| return (Direction) (((int) ((ang + CardinalOffset) * InvCardinalSegment) * 2) & 7); |
Member
There was a problem hiding this comment.
Suggested change
| return (Direction) (((int) ((ang + CardinalOffset) * InvCardinalSegment) * 2) & 7); | |
| return (Direction) (((int)((ang + CardinalOffset) * InvCardinalSegment) * 2) & 7); |
|
|
||
| [Pure] | ||
| public readonly Angle Opposite() | ||
| public Angle Opposite() |
| if (ang < 0) // convert -PI > PI to 0 > 2PI | ||
| ang += TwoPi; | ||
|
|
||
| return (Direction) ((int) ((ang + Offset) * InvSegment) & 7); |
Member
There was a problem hiding this comment.
Suggested change
| return (Direction) ((int) ((ang + Offset) * InvSegment) & 7); | |
| return (Direction)((int) ((ang + Offset) * InvSegment) & 7); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Benchmark code is pretty scrunkly and verbose but it's the old methods copy-pasted into it. Can add it if we want to keep it for history.