' Classes/System/Drawing/Rectangle.ab

#ifndef __SYSTEM_DRAWING_RECTANGLE_AB__
#define __SYSTEM_DRAWING_RECTANGLE_AB__

'#require <Classes/System/Math.ab>
#require <Classes/System/Drawing/RectangleF.ab>
#require <Classes/System/Drawing/Point.ab>
#require <Classes/System/Drawing/Size.ab>

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
		Dim pt As Point(x, y)
		Return pt
	End Function

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

	Function Size() As Size
		Dim size As Size(width, height)
	End Function

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

	Function X() As Long
		Return x
	End Function

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

	Function Y() As Long
		Return y
	End Function

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

	Function Width() As Long
		Return width
	End Function

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

	Function Height() As Long
		Return height
	End Function

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

	Function Left() As Long
		Return X
	End Function

	Function Top() As Long
		Return Y
	End Function

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

	Function Bottom() As Long
		Return 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(
			System.Math.Ceiling(rcf.X) As Long,
			System.Math.Ceiling(rcf.Y) As Long,
			System.Math.Ceiling(rcf.Width) As Long,
			System.Math.Ceiling(rcf.Height) As Long)
		Return r
	End Function

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

	Static Function Truncate(rcf As RectangleF) As Rectangle
		Dim r As Rectangle(
			System.Math.Truncate(rcf.X) As Long,
			System.Math.Truncate(rcf.Y) As Long,
			System.Math.Truncate(rcf.Width) As Long,
			System.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

#endif '__SYSTEM_DRAWING_RECTFANGLE_AB__
