1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
// Copyright (c) 2013-2022 Bruyère Jean-Philippe jp_bruyere@hotmail.com
//
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
using System;
namespace Drawing2D
{
public struct Size : IEquatable<Size>, IEquatable<int>
{
public int Width, Height;
#region CTOR
public Size (int width, int height)
{
Width = width;
Height = height;
}
public Size (int size)
{
Width = size;
Height = size;
}
#endregion
#region operators
public static implicit operator Rectangle(Size s)=> new Rectangle (s);
public static implicit operator Size(int i)=> new Size(i, i);
public static implicit operator string(Size s)=> s.ToString ();
public static implicit operator Size(string s)=> Parse (s);
public static bool operator == (Size s1, Size s2) => s1.Equals(s2);
public static bool operator != (Size s1, Size s2) => !s1.Equals (s2);
public static bool operator > (Size s1, Size s2) => (s1.Width > s2.Width && s1.Height > s2.Height);
public static bool operator >= (Size s1, Size s2) => (s1.Width >= s2.Width && s1.Height >= s2.Height);
public static bool operator < (Size s1, Size s2) => (s1.Width < s2.Width) ? s1.Height <= s2.Height :
(s1.Width == s2.Width && s1.Height < s2.Height);
public static bool operator < (Size s, int i) => s.Width < i && s.Height < i;
public static bool operator <= (Size s, int i) => s.Width <= i && s.Height <= i;
public static bool operator > (Size s, int i) => s.Width > i && s.Height > i;
public static bool operator >= (Size s, int i) => s.Width >= i && s.Height >= i;
public static bool operator <= (Size s1, Size s2) => (s1.Width <= s2.Width && s1.Height <= s2.Height);
/*public static bool operator == (Size s, int i) => (s.Width == i && s.Height == i);
public static bool operator != (Size s, int i) => (s.Width != i || s.Height != i);*/
public static Size operator + (Size s1, Size s2) => new Size (s1.Width + s2.Width, s1.Height + s2.Height);
public static Size operator + (Size s, int i) => new Size (s.Width + i, s.Height + i);
public static Size operator - (Size s1, Size s2) => new Size (s1.Width - s2.Width, s1.Height - s2.Height);
public static Size operator - (Size s, int i) => new Size (s.Width - i, s.Height - i);
public static Size operator * (Size s, int i) => new Size (s.Width * i, s.Height * i);
public static Size operator / (Size s, int i) => new Size (s.Width / i, s.Height / i);
public static Size operator -(Size s) => new Size (-s.Width, -s.Height);
public static Size operator * (Size s, double i) => new Size ((int)(s.Width * i), (int)(s.Height * i));
#endregion
public bool IsValid => Width > 0 && Height > 0;
public bool Equals (Size other) => Width == other.Width && Height == other.Height;
public bool Equals (int other) => Width == other && Height == other;
public override int GetHashCode () => HashCode.Combine (Width, Height);
public override bool Equals (object obj) => obj is Size s ? Equals(s) : false;
public override string ToString () => $"{Width},{Height}";
public static Size Parse(string s)
{
ReadOnlySpan<char> tmp = s.AsSpan ();
if (tmp.Length == 0)
return default (Size);
int ioc = tmp.IndexOf (',');
return ioc < 0 ? new Size (int.Parse (tmp)) : new Size (
int.Parse (tmp.Slice (0, ioc)),
int.Parse (tmp.Slice (ioc + 1)));
}
}
}
|