Rect.Contains

From Xojo Documentation

Method

Rect.Contains(p As Point) As Boolean

New in 2019r2

Supported for all project types and targets.

True if the Rect contains the passed point.


Method

Realbasic.Rect.Contains(Other As Rect) As Boolean

New in 2019r2

Supported for all project types and targets.

True if the Rect completely contains the passed Rect.

Sample Code

This example is from the CanvasDragRect example that comes with Xojo (Examples/Graphics and Multimedia/CanvasDragRect).

The Paint event contains code that handles all the cases in which the two rectangles interact. The code uses the Union, Contains, and Intersect methods to detect these conditions and applies a color to the picture, buffer. This code is in the Paint event of the Canvas.

Const RedColor = &cFF0000
Const GreenColor = &c00FF00
Const BlueColor = &c0000FF
Const CyanColor = &c00FFFF
Const WhiteColor = &cFFFFFF

// Create a pure white picture, to prevent flicker on Windows.
Var buffer As New Picture(g.Width, g.Height, 32)
buffer.Graphics.DrawingColor = WhiteColor

// To demonstrate the Union feature, we'll draw a Cyan rectangle
// around both the draggable rects.
Var u As Rect = rectOne.Union(rectTwo)
buffer.Graphics.DrawingColor = CyanColor
buffer.Graphics.DrawRectangle(u.Left, u.Top, u.Width, u.Height)

// If the cursor is inside RectOne, and we're not dragging
// RectTwo, then we'll use blue. Otherwise, the rect will be red.
If rectOne.Contains(MousePosition) And DraggingRect <> rectTwo Then
buffer.Graphics.DrawingColor = BlueColor
Else
buffer.Graphics.DrawingColor = RedColor
End If
buffer.Graphics.FillRectangle(rectOne.Left, rectOne.Top, rectOne.Width, rectOne.Height)

// Same as above, but for RectTwo
If rectTwo.Contains(MousePosition) And DraggingRect <> rectOne Then
buffer.Graphics.DrawingColor = BlueColor
Else
buffer.Graphics.DrawingColor = RedColor
End If
buffer.Graphics.FillRectangle(rectTwo.Left, rectTwo.Top, rectTwo.Width, rectTwo.Height)

// To demonstrate the Intersection feature, we'll draw a green rectangle
// where the two draggable rects overlap. The Intersection function will
// always return a rect, even if the two provided do not actually intersect.
// That's why we use the Intersects function first. Remove the "If" block to
// see a neat trick.
If rectOne.Intersects(rectTwo) Then
Var r As Rect = rectOne.Intersection(rectTwo)
buffer.Graphics.DrawingColor = GreenColor
buffer.Graphics.FillRectangle(r.Left, r.Top, r.Width, r.Height)
End If

g.DrawPicture(buffer, 0, 0)