Class
Rect
Description
A way to represent an axis-aligned rectangular area. Rect can be used for window size/position, control size/position and drawing of controls.
Properties
Name |
Type |
Read-Only |
Shared |
---|---|---|---|
Methods
Name |
Parameters |
Returns |
Shared |
---|---|---|---|
Rect |
|||
p As Point |
|||
other As Rect |
|||
Other As Rect |
Rect |
||
Other As Rect |
|||
P As Point |
|||
P As Rect |
Rect |
||
X As Double |
Rect |
||
Y As Double |
Rect |
||
Other As Rect |
Rect |
Property descriptions
Rect.Bottom
Bottom As Double
The bottom edge of the Rect on the Y axis. Changing this value will resize the Rect.
This example enlarges the Rect by 50 points:
myRect.Bottom = myRect.Bottom + 50
Note
The value of Bottom is one point beyond the bounds of the Rect.
Rect.Center
Center As Point
The center of the Rect. Changing this value will move the Rect.
This sample works in a Canvas.Paint event. It computes the center of a Rect that represents the union of two rects.
Var i, j As Integer
' To demonstrate the Union feature, 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)
theSize = u.Size
Var theCenter As Point = u.Center
i = theCenter.X
j = theCenter.Y
Rect.Height
Height As Double
The height of the Rect.
This example increases the height of rectone by 50 points.
myRect.Height = myRect.Height + 50
Rect.HorizontalCenter
HorizontalCenter As Double
The center of the Rect on the X axis. Changing this value will move the Rect.
This example is in the MouseDrag event. It gets the horizontal and vertical centers of the Rect being dragged.
Var i, j As Double
i = DraggingRect.HorizontalCenter
j = DraggingRect.VerticalCenter
Rect.Left
Left As Double
The left edge of the Rect on the X axis.
This example moves the Rect to the left by 50 points, keeping its height and width intact.
myRect.Left = myRect.Left + 50
Rect.Origin
Origin As Point
The upper left coordinate of the Rect.
This example is in the MouseDrag event. The position of the Rect being dragged is updated via by reseting the value of the Origin property.
' Update our mouse position.
mouseposition = New Point(x, y)
' If we're actually dragging something, move the rect.
' We update the rect's origin with a CLONE of the mouse position,
' because MousePosition is an instance of the Point class.
' Without the clone, when we call the offset function, we'll also update
' the MousePosition property, since both DraggingRect.Origin and MousePosition
' would point to the same variable.
' The offset function shifts the rect. Positive for right/down,
' negative for left/up.
If draggingrect <> Nil Then
draggingrect.Origin = mouseposition.Clone
draggingrect.Offset(mouseoffset.x, mouseoffset.y)
End If
' refresh, without erasing the background
Me.Refresh(False)
Rect.Right
Right As Double
The right edge of the Rect on the X axis. Changing this value will resize the Rect.
This example moves the Rect to the right by 50 points, keeping its height and width intact:
myRect.Left = myRect.Left + 50
Note
The value of Right is one point beyond the bounds of the Rect.
Rect.Size
Size As Size
The dimensions of the Rect.
This example is in the Paint event of a project. It computes the Size of the union of two rects.
' To demonstrate the Union feature, we'll draw a Cyan rectangle
' around both the draggable rects.
Var theSize As New Size
Var u As Rect = rectone.Union(recttwo)
buffer.Graphics.DrawingColor = CyanColor
buffer.Graphics.DrawRectangle(u.Left, u.Top, u.Width, u.Height)
theSize = u.Size
Rect.Top
Top As Double
The top edge of the Rect on the Y axis.
This example is in the Paint event. It computes the top of a Rect that represents the union of two rects.
Var i, j As Integer
' 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)
theSize = u.Size
Var theTop As Point = u.Top
i = theTop.x
j = theTop.y
Rect.VerticalCenter
VerticalCenter As Double
The center of the Rect on the Y axis. Changing this value will move the Rect.
This example is in the MouseDrag event. It gets the horizontal and vertical centers of the Rect being dragged.
Var i, j As Double
i = DraggingRect.HorizontalCenter
j = DraggingRect.VerticalCenter
Rect.Width
Width As Double
The width of the Rect.
This example increases the width by 50 points:
myRect.Width = myWidth.Width + 50
Method descriptions
Rect.Clone
Clone As Rect
Creates a duplicate of the Rect.
Rect.Constructor
Constructor(X As Double, Y As Double, Width As Double, Height As Double)
Note
Constructors are special methods called when you create an object with the New keyword and pass in the parameters above.
Creates a Rect object at X,Y and the passed width and height.
Rect.Constructor
Constructor(origin As Point, size As Size)
Note
Constructors are special methods called when you create an object with the New keyword and pass in the parameters above.
Creates a Rect object given the origin and size passed.
Create a Rect at 100, 100 that is 50 tall and 50 wide.
Var p As New Point(100, 100)
Var s As New Size(50, 50)
Var myRect As New Rect(p, s)
Rect.Contains
Contains(x As Double, y As Double) As Boolean
True if the Rect contains the point at the x/y coordinates passed.
Rect.Contains
Contains(p As Point) As Boolean
True if the Rect completely contains the Point passed.
Rect.Contains
Contains(other As Rect) As Boolean
True if the Rect completely contains the other Rect.
This 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)
Rect.Intersection
Intersection(Other As Rect) As Rect
Returns a new Rect representing the area the two Rects overlap.
Rect.Intersects
Intersects(Other As Rect) As Boolean
Returns True if the Rect overlaps the passed Rect.
Rect.LocalPoint
LocalPoint(p As Point) As Point
Returns a new Point whose coordinates are local to the calling Rect.
Var r As New Rect(100, 100, 200, 200)
Var p As New point(100, 100)
Var p1 As point = r.LocalPoint(p)
p1 is 0, 0.
Rect.LocalRect
LocalRect(P As Rect) As Rect
Returns a new Rect whose coordinates are local to the calling Rect.
Rect.Offset
Offset(DeltaX As Double, DeltaY As Double)
Moves the point the passed number of points. Positive numbers move the point down or to the right, negative move it up or to the left.
Var r As New Rect(100, 100, 200, 200)
r.OffSet(100, 100)
r is now at 200, 200. The width and height have not changed.
Rect.SplitHorizontal
SplitHorizontal(X As Double) As Rect
Divides the Rect into two rects at the given point on the X axis.
For example, imagine a Rect that is 200 points wide by 100 points tall. With a call to SplitHorizontal(50), the Rect will become 50 points wide, and a new Rect will be returned that is 150x100, positioned exactly to the right of the original Rect.
This example is taken from the Paint event of a Canvas. It takes a Rect and splits it horizontally at the midpoint of the width.
Var uright As Rect = u.SplitHorizontal(theSize.Width / 2)
Rect.SplitVertical
SplitVertical(Y As Double) As Rect
Divides the Rect into two rects at the given point on the Y axis.
For example, imagine a Rect that is 200 points wide by 100 points tall. A call to SplitVertical (50) on this Rect, the Rect will become 50 points wide, and a new Rect will be returned that is 150x100, positioned exactly to the top of the original Rect.
This example is taken from the Paint event of a Canvas. It takes a Rect and splits it horizontally at the midpoint of the height.
Var utop As Rect = u.SplitVertical(theSize.Height / 2)
Rect.Union
Union(Other As Rect) As Rect
Creates a new Rect containing both rects.
Notes
If you subclass Rect, there's a comparison operation you should be aware of. Comparing instances of two different subclasses of Rect would seem invalid because they are two different classes. However, this is a valid comparison because the Rect class has an Operator_Convert which returns a new instance of the Rect class. Thus despite the fact that two different classes are being compared, both instances being compared are from the same class. The downside to this of course is that what is returned is not an instance of your subclass. To resolve this there are two options. You can use your own class that is not a subclass of Rect or you can override the Operator_Convert in your subclasses to return an instance of themselves.
Sample code
Var myRect As New Rect(10, 10, 100, 50)
Compatibility
All project types on all supported operating systems.
See also
Object parent class; DesktopCanvas control; DesktopWindow, DesktopContainer, Point, Size classes.