22
33namespace Sims1WidescreenPatcher . Core . Models ;
44
5- public class AspectRatio : IEqualityComparer < AspectRatio > , IComparable < AspectRatio >
5+ public record AspectRatio : IComparable < AspectRatio >
66{
7- protected bool Equals ( AspectRatio other )
8- {
9- return Numerator == other . Numerator && Denominator == other . Denominator ;
10- }
7+ private readonly BigInteger _gcd ;
118
12- public override bool Equals ( object ? obj )
9+ public AspectRatio ( int Width , int Height )
1310 {
14- if ( ReferenceEquals ( null , obj ) ) return false ;
15- if ( ReferenceEquals ( this , obj ) ) return true ;
16- if ( obj . GetType ( ) != this . GetType ( ) ) return false ;
17- return Equals ( ( AspectRatio ) obj ) ;
11+ this . Width = Width ;
12+ this . Height = Height ;
13+ _gcd = BigInteger . GreatestCommonDivisor ( Width , Height ) ;
1814 }
1915
20- public override int GetHashCode ( )
21- {
22- unchecked
23- {
24- return ( Numerator * 397 ) ^ Denominator ;
25- }
26- }
16+ public int Numerator => Width / ( int ) _gcd ;
17+ public int Denominator => Height / ( int ) _gcd ;
18+ public int Width { get ; init ; }
19+ public int Height { get ; init ; }
2720
28- public int Numerator ;
29- public int Denominator ;
30-
31- public AspectRatio ( int width , int height )
32- {
33- CalculateAspectRatio ( width , height ) ;
34- }
21+ public override string ToString ( ) => $ "{ Numerator } :{ Denominator } ";
3522
36- public override string ToString ( )
37- {
38- return $ "{ Numerator } :{ Denominator } ";
39- }
40-
41- public static bool operator == ( AspectRatio obj1 , AspectRatio obj2 )
42- {
43- return ( obj1 . Numerator == obj2 . Numerator
44- && obj1 . Denominator == obj2 . Denominator ) ;
45- }
23+ public virtual bool Equals ( AspectRatio ? other ) =>
24+ other is not null && Numerator == other . Numerator && Denominator == other . Denominator ;
4625
47- public static bool operator != ( AspectRatio obj1 , AspectRatio obj2 )
48- {
49- return ! ( obj1 == obj2 ) ;
50- }
51-
52- private void CalculateAspectRatio ( int width , int height )
53- {
54- var gcd = BigInteger . GreatestCommonDivisor ( width , height ) ;
55- Numerator = width / ( int ) gcd ;
56- Denominator = height / ( int ) gcd ;
57- }
58-
59- public bool Equals ( AspectRatio x , AspectRatio y )
60- {
61- if ( ReferenceEquals ( x , y ) ) return true ;
62- if ( ReferenceEquals ( x , null ) ) return false ;
63- if ( ReferenceEquals ( y , null ) ) return false ;
64- if ( x . GetType ( ) != y . GetType ( ) ) return false ;
65- return x . Numerator == y . Numerator && x . Denominator == y . Denominator ;
66- }
67-
68- public int GetHashCode ( AspectRatio obj )
69- {
70- unchecked
71- {
72- return ( obj . Numerator * 397 ) ^ obj . Denominator ;
73- }
74- }
26+ public override int GetHashCode ( ) => HashCode . Combine ( Numerator , Denominator ) ;
7527
7628 public int CompareTo ( AspectRatio ? other )
7729 {
7830 if ( ReferenceEquals ( this , other ) ) return 0 ;
79- if ( ReferenceEquals ( null , other ) ) return 1 ;
31+ if ( other is null ) return 1 ;
8032 var numeratorComparison = Numerator . CompareTo ( other . Numerator ) ;
8133 if ( numeratorComparison != 0 ) return numeratorComparison ;
8234 return Denominator . CompareTo ( other . Denominator ) ;
8335 }
36+
37+ public void Deconstruct ( out int Width , out int Height )
38+ {
39+ Width = this . Width ;
40+ Height = this . Height ;
41+ }
8442}
0 commit comments