Skip to content

Commit da46873

Browse files
authored
Created IAngle interface (#1583)
* Created `IAngle` interface * Added comments to values that are wrapped around * Added operators to interface
1 parent a09ac91 commit da46873

4 files changed

Lines changed: 45 additions & 23 deletions

File tree

Pinta.Core/Algorithms/Utility.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,8 @@ public static RadiansAngle GetNearestStepAngle (RadiansAngle angle, int steps)
430430
{
431431
ArgumentOutOfRangeException.ThrowIfLessThan (steps, 1);
432432

433-
const double FULL_TURN = RadiansAngle.MAX_RADIANS;
434-
double stepAngle = FULL_TURN / steps;
433+
double fullTurn = RadiansAngle.FullTurn;
434+
double stepAngle = fullTurn / steps;
435435
double sector = Math.Round (angle.Radians / stepAngle) * stepAngle;
436436

437437
return new (sector);

Pinta.Core/Classes/Angle.cs

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,32 @@
22

33
namespace Pinta.Core;
44

5-
public readonly struct RadiansAngle
5+
public interface IAngle<TAngle> where TAngle : IAngle<TAngle>
66
{
7-
public const double MAX_RADIANS = Math.PI * 2;
7+
static abstract double FullTurn { get; }
8+
static abstract bool operator == (TAngle a, TAngle b);
9+
static abstract bool operator != (TAngle a, TAngle b);
10+
static abstract TAngle operator + (TAngle a, TAngle b);
11+
static abstract TAngle operator - (TAngle a, TAngle b);
12+
}
13+
14+
public readonly struct RadiansAngle : IAngle<RadiansAngle>
15+
{
16+
public static double FullTurn => Math.PI * 2;
17+
18+
/// <remarks>The value can only be in the range [0, <see cref="FullTurn" />)</remarks>
819
public readonly double Radians { get; }
920

21+
/// <remarks>
22+
/// <paramref name="radians"/> is wrapped around to be within the allowable
23+
/// range of the <see cref="Radians"/> property
24+
/// </remarks>
1025
public RadiansAngle (double radians)
1126
{
1227
Radians = radians switch {
1328
0 => 0,
14-
>= 0 => radians % MAX_RADIANS,
15-
_ => (MAX_RADIANS + (radians % MAX_RADIANS)) % MAX_RADIANS
29+
>= 0 => radians % FullTurn,
30+
_ => (FullTurn + (radians % FullTurn)) % FullTurn
1631
};
1732
}
1833

@@ -26,8 +41,6 @@ public RadiansAngle (double radians)
2641
public static RadiansAngle operator - (RadiansAngle a, RadiansAngle b) => new (a.Radians - b.Radians);
2742
public static bool operator == (RadiansAngle a, RadiansAngle b) => a.Equals (b);
2843
public static bool operator != (RadiansAngle a, RadiansAngle b) => !a.Equals (b);
29-
public static bool operator > (RadiansAngle a, RadiansAngle b) => a.Radians > b.Radians;
30-
public static bool operator < (RadiansAngle a, RadiansAngle b) => a.Radians < b.Radians;
3144
public override readonly int GetHashCode () => Radians.GetHashCode ();
3245
public override readonly bool Equals (object? obj)
3346
{
@@ -36,17 +49,24 @@ public override readonly bool Equals (object? obj)
3649
}
3750
}
3851

39-
public readonly struct DegreesAngle
52+
public readonly struct DegreesAngle : IAngle<DegreesAngle>
4053
{
41-
public const double MAX_DEGREES = 360;
54+
public static double FullTurn => 360;
55+
56+
57+
/// <remarks>The value can only be in the range [0, <see cref="FullTurn" />)</remarks>
4258
public readonly double Degrees { get; }
4359

60+
/// <remarks>
61+
/// <paramref name="degrees"/> is wrapped around to be within the allowable
62+
/// range of the <see cref="Degrees"/> property
63+
/// </remarks>
4464
public DegreesAngle (double degrees)
4565
{
4666
Degrees = degrees switch {
4767
0 => 0,
48-
>= 0 => degrees % MAX_DEGREES,
49-
_ => (MAX_DEGREES + (degrees % MAX_DEGREES)) % MAX_DEGREES
68+
>= 0 => degrees % FullTurn,
69+
_ => (FullTurn + (degrees % FullTurn)) % FullTurn
5070
};
5171
}
5272

@@ -60,8 +80,6 @@ public DegreesAngle (double degrees)
6080
public static DegreesAngle operator - (DegreesAngle a, DegreesAngle b) => new (a.Degrees - b.Degrees);
6181
public static bool operator == (DegreesAngle a, DegreesAngle b) => a.Equals (b);
6282
public static bool operator != (DegreesAngle a, DegreesAngle b) => !a.Equals (b);
63-
public static bool operator > (DegreesAngle a, DegreesAngle b) => a.Degrees > b.Degrees;
64-
public static bool operator < (DegreesAngle a, DegreesAngle b) => a.Degrees < b.Degrees;
6583
public override readonly int GetHashCode () => Degrees.GetHashCode ();
6684
public override readonly bool Equals (object? obj)
6785
{
@@ -70,17 +88,23 @@ public override readonly bool Equals (object? obj)
7088
}
7189
}
7290

73-
public readonly struct RevolutionsAngle
91+
public readonly struct RevolutionsAngle : IAngle<RevolutionsAngle>
7492
{
75-
public const double MAX_REVOLUTIONS = 1;
93+
public static double FullTurn => 1;
94+
95+
/// <remarks>The value can only be in the range [0, <see cref="FullTurn" />)</remarks>
7696
public readonly double Revolutions { get; }
7797

98+
/// <remarks>
99+
/// <paramref name="revolutions"/> is wrapped around to be within the allowable
100+
/// range of the <see cref="Revolutions"/> property
101+
/// </remarks>
78102
public RevolutionsAngle (double revolutions)
79103
{
80104
Revolutions = revolutions switch {
81105
0 => 0,
82-
>= 0 => revolutions % MAX_REVOLUTIONS,
83-
_ => (MAX_REVOLUTIONS + (revolutions % MAX_REVOLUTIONS)) % MAX_REVOLUTIONS
106+
>= 0 => revolutions % FullTurn,
107+
_ => (FullTurn + (revolutions % FullTurn)) % FullTurn
84108
};
85109
}
86110

@@ -94,8 +118,6 @@ public RevolutionsAngle (double revolutions)
94118
public static RevolutionsAngle operator - (RevolutionsAngle a, RevolutionsAngle b) => new (a.Revolutions - b.Revolutions);
95119
public static bool operator == (RevolutionsAngle a, RevolutionsAngle b) => a.Revolutions == b.Revolutions;
96120
public static bool operator != (RevolutionsAngle a, RevolutionsAngle b) => a.Revolutions != b.Revolutions;
97-
public static bool operator > (RevolutionsAngle a, RevolutionsAngle b) => a.Revolutions > b.Revolutions;
98-
public static bool operator < (RevolutionsAngle a, RevolutionsAngle b) => a.Revolutions < b.Revolutions;
99121
public override readonly int GetHashCode () => Revolutions.GetHashCode ();
100122
public override readonly bool Equals (object? obj)
101123
{

Pinta.Effects/Effects/DentsEffect.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public Warp.TransformData InverseTransform (
110110

111111
double scaleR = 400.0 / settings.defaultRadius / dentsData.Scale;
112112
double refractionScale = dentsData.Refraction / 100.0 / scaleR;
113-
double theta = RadiansAngle.MAX_RADIANS * turbulence / 10.0;
113+
double theta = RadiansAngle.FullTurn * turbulence / 10.0;
114114
double effectiveRoughness = roughness / 100.0;
115115

116116
// We don't want the perlin noise frequency components exceeding the nyquist limit, so we will limit 'detail' appropriately

Pinta.Effects/Effects/FragmentEffect.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ private static ImmutableArray<PointI> RecalcPointOffsets (
5555
RadiansAngle rotation,
5656
int distance)
5757
{
58-
RadiansAngle pointStep = new (RadiansAngle.MAX_RADIANS / fragments);
59-
RadiansAngle adjustedRotation = rotation - new RadiansAngle (RadiansAngle.MAX_RADIANS / 4);
58+
RadiansAngle pointStep = new (RadiansAngle.FullTurn / fragments);
59+
RadiansAngle adjustedRotation = rotation - new RadiansAngle (RadiansAngle.FullTurn / 4);
6060
var pointOffsets = ImmutableArray.CreateBuilder<PointI> (fragments);
6161
pointOffsets.Count = fragments;
6262
for (int i = 0; i < fragments; i++) {

0 commit comments

Comments
 (0)