' Classes/System/Drawing/Rectangle.ab

Namespace System
Namespace Drawing

Class Rectangle
Public
	Sub Rectangle()
		x = 0
		y = 0
		width = 0
		height = 0
	End Sub

	Sub Rectangle(x As Long, y As Long, width As Long, height As Long)
		x = x
		y = y
		width = width
		height = height
	End Sub

	Sub Rectangle(l As Point, s As Size)
		x = l.X
		y = l.Y
		width = s.Height
		height = s.Height
	End Sub

	Sub Rectangle(ByRef r As RECT)
		x = r.left
		y = r.top
		width = r.right - r.left
		height = r.top - r.bottom
	End Sub

	Function Location() As Point
		Location = New Point(x, y)
	End Function

	Sub Location(point As Point)
		x = point.X
		y = point.Y
	End Sub

	Function Size() As Size
		Size = New Size(width, height)
	End Function

	Sub Size(size As Size)
		width = size.Width
		height = size.Height
	End Sub

	Function X() As Long
		X = x
	End Function

	Sub X(value As Long)
		x = value
	End Sub

	Function Y() As Long
		Y = y
	End Function

	Sub Y(value As Long)
		y = value
	End Sub

	Function Width() As Long
		Width = width
	End Function

	Sub Width(value As Long)
		width = value
	End Sub

	Function Height() As Long
		Height = height
	End Function

	Sub Height(value As Long)
		height = value
	End Sub

	Function Left() As Long
		Left = X
	End Function

	Function Top() As Long
		Top = Y
	End Function

	Function Right() As Long
		Right = X + Width
	End Function

	Function Bottom() As Long
		Bottom = Y + Height
	End Function

	Function IsEmpty() As Boolean
		Return Width <= 0 Or Height <= 0
	End Function

	Function Operator ==(rc As Rectangle) As Boolean
		Return Equals(rc)
	End Function

	Function Operator <>(rc As Rectangle) As Boolean
		Return (Not Equals(rc))
	End Function

	Function Operator () As RectangleF
		Return New RectangleF(x, y, width, height)
	End Function

	Function Equals(rc As Rectangle) As Boolean
		Return X = rc.X And Y = rc.Y And Width = rc.Width And Height = rc.Height
	End Function

	Override Function GetHashCode() As Long
		Return x As DWord Xor _System_BSwap(y As DWord) Xor width As DWord Xor _System_BSwap(height As DWord)
	End Function

	Static Function FromLTRB(l As Long, t As Long, r As Long, b As Long) As Rectangle
		return New Rectangle(l, t, r - l, b - t)
	End Function

	Function Contains(x As Long, y As Long) As Boolean
		Return x >= X And x < X + Width And y >= Y And y < Y + Height
	End Function

	Function Contains(pt As Point) As Boolean
		Return Contains(pt.X, pt.Y)
	End Function

	Function Contains(rc As Rectangle) As Boolean
		Return X <= rc.X And rc.Right <= Right And Y <= rc.Y And rc.Bottom <= Bottom
	End Function

	Sub Inflate(dx As Long, dy As Long)
		x -= dx
		y -= dy
		width += dx + dx
		height += dy + dy
	End Sub

	Sub Inflate(sz As Size)
		Inflate(sz.Width, sz.Height)
	End Sub

	Static Function Inflate(rc As Rectangle, x As Long, y As Long) As Rectangle
		Inflate = New Rectangle(rc.X, rc.Y, rc.Width, rc.Height)
		Inflate.Inflate(x, y)
	End Function

	Sub Intersect(rect As Rectangle)
		Dim r = Rectangle.Intersect(This, rect)
		x = r.x
		y = r.y
		width = r.width
		height = r.height
	End Sub

	Static Function Intersect(a As Rectangle, ByRef b As Rectangle) As Rectangle
		Dim right As Long, bottom As Long, left As Long, top As Long
		right = System.Math.Min(a.Right, b.Right)
		bottom = System.Math.Min(a.Bottom, b.Bottom)
		left = System.Math.Min(a.Left, b.Left)
		top = System.Math.Min(a.Top, b.Top)
		Return Rectangle.FromLTRB(left, top, right, bottom)
	End Function

	Function IntersectsWith(rc As Rectangle) As Boolean
		Return Left < rc.Right And _
			Top < rc.Bottom And _
			Right > rc.Left And _
			Bottom > rc.Top
	End Function

	Static Function Union(a As Rectangle, b As Rectangle) As Rectangle
		Dim right As Long, bottom As Long, left As Long, top As Long
		right = System.Math.Max(a.Right(), b.Right())
		bottom = System.Math.Max(a.Bottom(), b.Bottom())
		left = System.Math.Max(a.Left(), b.Left())
		top = System.Math.Max(a.Top(), b.Top())
		Return FromLTRB(left, top, right, bottom)
	End Function

	Sub Offset(pt As Point)
		Offset(pt.X, pt.Y)
	End Sub

	Sub Offset(dx As Long, dy As Long)
		x += dx
		y += dy
	End Sub

	Static Function Ceiling(rcf As RectangleF) As Rectangle
		Dim r As Rectangle(
			Math.Ceiling(rcf.X) As Long,
			Math.Ceiling(rcf.Y) As Long,
			Math.Ceiling(rcf.Width) As Long,
			Math.Ceiling(rcf.Height) As Long)
		Return r
	End Function

	Static Function Round(rcf As RectangleF) As Rectangle
		Dim r As Rectangle(
			Math.Round(rcf.X) As Long,
			Math.Round(rcf.Y) As Long,
			Math.Round(rcf.Width) As Long,
			Math.Round(rcf.Height) As Long)
		Return r
	End Function

	Static Function Truncate(rcf As RectangleF) As Rectangle
		Dim r As Rectangle(
			Math.Truncate(rcf.X) As Long,
			Math.Truncate(rcf.Y) As Long,
			Math.Truncate(rcf.Width) As Long,
			Math.Truncate(rcf.Height) As Long)
		Return r
	End Function

	Function ToRECT() As RECT
		With ToRECT
			.left = x
			.top = y
			.right = x + width
			.bottom = y + height
		End With
	End Function

Public
	x As Long
	y As Long
	width As Long
	height As Long
End Class

End Namespace
End Namespace
