' Classes/System/Drawing/RectangleF.ab

Namespace System
Namespace Drawing

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

	Sub RectangleF(x As Single, y As Single, width As Single, height As Single)
		x = x
		y = y
		width = width
		height = height
	End Sub

	Sub RectangleF(location As PointF, size As SizeF)
		x = location.X
		y = location.Y
		width = size.Height
		height = size.Height
	End Sub

	Sub RectangleF(rc As RectangleF)
		x = rc.x
		y = rc.y
		width = rc.width
		height = rc.height
	End Sub

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

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

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

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

	Function X() As Single
		X = x
	End Function

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

	Function Y() As Single
		Y = y
	End Function

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

	Function Width() As Single
		Width = width
	End Function

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

	Function Height() As Single
		Height = height
	End Function

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

	Function Left() As Single
		Left = X
	End Function

	Function Top() As Single
		Top = Y
	End Function

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

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

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

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

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

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

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

	Static Function FromLTRB(l As Single, t As Single, r As Single, b As Single) As RectangleF
		return New RectangleF(l, t, r - l, b - t)
	End Function

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

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

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

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

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

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

	Sub Intersect(rect As RectangleF)
		Dim r = RectangleF.Intersect(This, rect)
		x = r.x
		y = r.y
		width = r.width
		height = r.height
	End Sub
	
	Static Function Intersect(a As RectangleF, b As RectangleF) As RectangleF
		Dim right As Single, bottom As Single, left As Single, top As Single
		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 FromLTRB(left, top, right, bottom)
	End Function

	Function IntersectsWith(rc As RectangleF) As Boolean
		If Left < rc.Right And _
			Top < rc.Bottom And _
			Right > rc.Left And _
			Bottom > rc.Top Then
			IntersectsWith = True
		Else
			IntersectsWith = False
		End If
	End Function

	Static Function Union(a As RectangleF, b As RectangleF) As RectangleF
		Dim right As Single, bottom As Single, left As Single, top As Single
		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 PointF)
		Offset(pt.X, pt.Y)
	End Sub

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

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

End Namespace
End Namespace
