793
792
internal double height;
794
793
private static readonly XRect s_empty;
798
// Old code, delete end of 2008
801
/// Stores a set of four floating-point numbers that represent the location and size of a rectangle.
803
[DebuggerDisplay("({X}, {Y}, {Width}, {Height})")]
806
// Called XRect and not XRectangle because XRectangle will get the name of a shape object
807
// in a forthcoming extension.
810
/// Initializes a new instance of the XRect class.
812
public XRect(double x, double y, double width, double height)
817
this.height = height;
821
/// Initializes a new instance of the XRect class.
823
public XRect(XPoint location, XSize size)
827
this.width = size.Width;
828
this.height = size.Height;
833
/// Initializes a new instance of the XRect class.
835
public XRect(PointF location, SizeF size)
839
this.width = size.Width;
840
this.height = size.Height;
846
/// Initializes a new instance of the XRect class.
848
public XRect(RectangleF rect)
852
this.width = rect.Width;
853
this.height = rect.Height;
859
/// Initializes a new instance of the XRect class.
861
public XRect(Rect rect)
865
this.width = rect.Width;
866
this.height = rect.Height;
871
/// Creates a rectangle from for straight lines.
873
public static XRect FromLTRB(double left, double top, double right, double bottom)
875
return new XRect(left, top, right - left, bottom - top);
879
/// Returns the hash code for this instance.
881
public override int GetHashCode()
883
// Lutz Roeder's .NET Reflector proudly presents:
884
// �THE ART OF HASH CODE PROGRAMMING�
887
// return (int) (((((uint) this.X) ^ ((((uint) this.Y) << 13) | (((uint) this.Y) >> 0x13))) ^ ((((uint) this.Width) << 0x1a) | (((uint) this.Width) >> 6))) ^ ((((uint) this.Height) << 7) | (((uint) this.Height) >> 0x19)));
889
// return (int) (x + y + width + height);
890
return (int)(x + y + width + height);
894
/// Indicates whether this instance and a specified object are equal.
896
public override bool Equals(object obj)
900
XRect rect = (XRect)obj;
901
return rect.x == this.x && rect.y == this.y && rect.width == this.width && rect.height == this.height;
907
/// Returns a string with the values of this rectangle.
909
public override string ToString()
911
return String.Format("{{X={0},Y={1},Width={2},Height={3}}}", this.x, this.y, this.width, this.height);
916
/// Converts this instance to a System.Drawing.RectangleF.
918
public RectangleF ToRectangleF()
920
return new RectangleF((float)this.x, (float)this.y, (float)this.width, (float)this.height);
925
/// Gets a value indicating whether this rectangle is empty.
930
// The .NET documentation differs from the actual implemention, which differs from the Mono
931
// implementation. This is my recommendation what an empty rectangle means:
932
get { return this.width <= 0.0 || this.height <= 0.0; }
936
/// Gets or sets the location of the rectangle.
939
public XPoint Location
941
get { return new XPoint(this.x, this.y); }
942
set { this.x = value.X; this.y = value.Y; }
946
/// Gets or sets the size of the rectangle.
951
get { return new XSize(this.width, this.height); }
952
set { this.width = value.Width; this.height = value.Height; }
956
/// Gets or sets the X value.
960
get { return this.x; }
961
set { this.x = value; }
965
/// Gets or sets the Y value.
969
get { return this.y; }
970
set { this.y = value; }
974
/// Gets or sets the width.
978
get { return this.width; }
979
set { this.width = value; }
983
/// Gets or sets the height.
987
get { return this.height; }
988
set { this.height = value; }
997
get { return this.x; }
1006
get { return this.y; }
1015
get { return this.x + this.width; }
1019
/// Gets the bottom.
1022
public double Bottom
1024
get { return this.y + this.height; }
1028
/// Gets the center of the rectangle.
1031
public XPoint Center
1033
get { return new XPoint(this.x + this.width / 2, this.y + this.height / 2); }
1037
/// Determines whether the rectangle contains the specified point.
1039
public bool Contains(XPoint pt)
1041
return Contains(pt.X, pt.Y);
1045
/// Determines whether the rectangle contains the specified point.
1047
public bool Contains(double x, double y)
1049
return this.x <= x && x < this.x + this.width && this.y <= y && y < this.y + this.height;
1053
/// Determines whether the rectangle completely contains the specified rectangle.
1055
public bool Contains(XRect rect)
1057
return this.x <= rect.x && rect.x + rect.width <= this.x + this.width &&
1058
this.y <= rect.y && rect.y + rect.height <= this.y + this.height;
1062
/// Inflates the rectangle by the specified size.
1064
public void Inflate(XSize size)
1066
Inflate(size.Width, size.Height);
1070
/// Inflates the rectangle by the specified size.
1072
public void Inflate(double x, double y)
1076
this.width += x * 2;
1077
this.height += y * 2;
1081
/// Inflates the rectangle by the specified size.
1083
public static XRect Inflate(XRect rect, double x, double y)
1090
/// Intersects the rectangle with the specified rectangle.
1092
public void Intersect(XRect rect)
1094
rect = XRect.Intersect(rect, this);
1097
this.width = rect.width;
1098
this.height = rect.height;
1102
/// Intersects the specified rectangles.
1104
public static XRect Intersect(XRect left, XRect right)
1106
double l = Math.Max(left.x, right.x);
1107
double r = Math.Min(left.x + left.width, right.x + right.width);
1108
double t = Math.Max(left.y, right.y);
1109
double b = Math.Min(left.y + left.height, right.y + right.height);
1110
if ((r >= l) && (b >= t))
1111
return new XRect(l, t, r - l, b - t);
1116
/// Determines whether the rectangle intersects with the specified rectangle.
1118
public bool IntersectsWith(XRect rect)
1120
return rect.x < this.x + this.width && this.x < rect.x + rect.width &&
1121
rect.y < this.y + this.height && this.y < rect.y + rect.height;
1125
/// Unites the specified rectangles.
1127
public static XRect Union(XRect left, XRect right)
1129
double l = Math.Min(left.X, right.X);
1130
double r = Math.Max(left.X + left.Width, right.X + right.Width);
1131
double t = Math.Min(left.Y, right.Y);
1132
double b = Math.Max(left.Y + left.Height, right.Y + right.Height);
1133
return new XRect(l, t, r - l, b - t);
1137
/// Translates the rectangle by the specifed offset.
1139
public void Offset(XPoint pt)
1145
/// Translates the rectangle by the specifed offset.
1147
public void Offset(double x, double y)
1154
/// Translates the rectangle by adding the specifed point.
1156
public static XRect operator +(XRect rect, XPoint point)
1158
return new XRect(rect.x + point.x, rect.Y + point.y, rect.width, rect.height);
1162
/// Translates the rectangle by subtracting the specifed point.
1164
public static XRect operator -(XRect rect, XPoint point)
1166
return new XRect(rect.x - point.x, rect.Y - point.y, rect.width, rect.height);
1171
/// Implicit conversion from a System.Drawing.Rectangle to an XRect.
1173
public static implicit operator XRect(Rectangle rect)
1175
return new XRect(rect.X, rect.Y, rect.Width, rect.Height);
1179
/// Implicit conversion from a System.Drawing.RectangleF to an XRect.
1181
public static implicit operator XRect(RectangleF rect)
1183
return new XRect(rect.X, rect.Y, rect.Width, rect.Height);
1188
public static implicit operator XRect(Rect rect)
1190
return new XRect(rect.X, rect.Y, rect.Width, rect.Height);
1195
/// Determines whether the two rectangles are equal.
1197
public static bool operator ==(XRect left, XRect right)
1199
return left.x == right.x && left.y == right.y && left.width == right.width && left.height == right.height;
1203
/// Determines whether the two rectangles not are equal.
1205
public static bool operator !=(XRect left, XRect right)
1207
return !(left == right);
1211
/// Represents the empty rectangle.
1213
public static readonly XRect Empty = new XRect();
1217
internal double width;
1218
internal double height;