1 // =================================
  2 // Copyright (c) 2022 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 using System;
  7 
  8 namespace System.Screen
  9 {
 10     public class Point
 11     {
 12         public nothrow Point() : x(0)y(0)
 13         {
 14         }
 15         public nothrow Point(int x_int y_) : x(x_)y(y_)
 16         {
 17         }
 18         public static nothrow Point Default()
 19         {
 20             return Point(-1-1);
 21         }
 22         public nothrow bool IsDefault() const
 23         {
 24             return x == -1 && y == -1;
 25         }
 26         public nothrow void Offset(int dxint dy)
 27         {
 28             x = x + dx;
 29             y = y + dy;
 30         }
 31         public nothrow string ToString() const
 32         {
 33             return "(" + ToString(x) + ", " + ToString(y) + ")";
 34         }
 35         public int x;
 36         public int y;
 37     }
 38     
 39     public nothrow bool operator==(const Point& leftconst Point& right)
 40     {
 41         return left.x == right.x && left.y == right.y;
 42     }
 43     
 44     public class Size
 45     {
 46         public nothrow Size() : w(0)h(0)
 47         {
 48         }
 49         public nothrow Size(int w_int h_) : w(w_)h(h_)
 50         {
 51         }
 52         public static nothrow Size Default()
 53         {
 54             return Size(-1-1);
 55         }
 56         public nothrow bool IsDefault() const
 57         {
 58             return w == -1 && h == -1;
 59         }
 60         public nothrow string ToString() const
 61         {
 62             return "(" + ToString(w) + ", " + ToString(h) + ")";
 63         }
 64         public int w;
 65         public int h;
 66     }
 67     
 68     public nothrow bool operator==(const Size& leftconst Size& right)
 69     {
 70         return left.w == right.w && left.h == right.h;
 71     }
 72     
 73     public class Rect
 74     {
 75         public nothrow Rect() : location()size()
 76         {
 77         }
 78         public nothrow Rect(const Point& location_const Size& size_) : location(location_)size(size_)
 79         {
 80         }
 81         public static nothrow Rect Default()
 82         {
 83             return Rect(Point.Default()Size.Default());
 84         }
 85         public nothrow bool IsDefault() const
 86         {
 87             return location.IsDefault() && size.IsDefault();
 88         }
 89         public nothrow bool IsEmpty() const
 90         {
 91             return size.w == 0 && size.h == 0;
 92         }
 93         public inline nothrow int Left() const
 94         {
 95             return location.x;
 96         }
 97         public inline nothrow int Right() const
 98         {
 99             return location.x + size.w;
100         }
101         public inline nothrow int Top() const
102         {
103             return location.y;
104         }
105         public inline nothrow int Bottom() const
106         {
107             return location.y + size.h;
108         }
109         public inline nothrow bool Contains(const Point& p)
110         {
111             return p.x >= location.x && p.x < location.x + size.w && p.y >= location.y && p.y < location.y + size.h;
112         }
113         public inline nothrow void Inflate(int dxint dy)
114         {
115             location.x = location.x - dx;
116             location.y = location.y - dy;
117             size.w = size.w + 2 * dx;
118             size.h = size.h + 2 * dy;
119         }
120         public inline nothrow void Offset(int dxint dy)
121         {
122             location.Offset(dxdy);
123         }
124         public nothrow bool IntersectsWith(const Rect& that) const
125         {
126             return Left() < that.Right() && Top() < that.Bottom() && Right() > that.Left() && Bottom() > that.Top();
127         }
128         public static nothrow Rect Union(const Rect& aconst Rect& b)
129         {
130             int right = Max(a.Right()b.Right());
131             int bottom = Max(a.Bottom()b.Bottom());
132             int left = Min(a.Left()b.Left());
133             int top = Min(a.Top()b.Top());
134             return Rect(Point(lefttop)Size(right - leftbottom - top));
135         }
136         public static nothrow Rect Intersection(const Rect& aconst Rect& b)
137         {
138             int right = Min(a.Right()b.Right());
139             int bottom = Min(a.Bottom()b.Bottom());
140             int left = Max(a.Left()b.Left());
141             int top = Max(a.Top()b.Top());
142             return Rect(Point(lefttop)Size(right - leftbottom - top));
143         }
144         public string ToString() const
145         {
146             return "[" + location.ToString() + ", " + size.ToString() + "]";
147         }
148         public Point location;
149         public Size size;
150     }
151     
152     public nothrow bool operator==(const Rect& leftconst Rect& right)
153     {
154         return left.location == right.location && left.size == right.size;
155     }
156 }