# GAMBAS BOOK 3.19.1

## 2D-Graphic

### Chapters

08.10.2022
k25:k25.2.4:start

## 25.2.4 Classes Rect and RectF

The Rect (gb.clipper) class represents a rectangle with integer coordinates. This chapter presents the properties and methods of the Rect class as well as some examples for using the class.

## 25.2.4.1 Properties

The Rect class has these properties:

PropertyDescription
X AS IntegerSets the x-coordinate of the rectangle or returns this value.
Y AS IntegerSets the y-coordinate of the rectangle or returns this value.
Height As Integer or H As IntegerSets the height of the rectangle or returns this value.
Width As Integer or W As Integer Sets the width of the rectangle or returns this value.
Left As IntegerSets the position of the left edge of the rectangle or returns this value. In contrast to the X property, this value changes the width of the rectangle, as the right boundary of the rectangle does not change.
Right As IntegerSets the position of the right edge of the rectangle or returns this value.
Top As IntegerSets the position of the top edge of the rectangle or returns this value. In contrast to the Y property, this value changes the height of the rectangle, as the lower boundary of the rectangle does not change.
Bottom As IntegerSets the position of the bottom edge of the rectangle or returns this value.

Table 25.2.4.1.1 : Properties of the Rect class

## 25.2.4.2 Selected methods

The methods for the Rect class are described here.

MethodDescription
Center( ) As PointReturns the coordinates of the centre point of the rectangle as the intersection of the diagonals.
Clear( ) Clears the rectangle. All values are set to 0.
IsVoid( ) As Boolean Returns True if the rectangle is empty.
Copy( ) As Rect Returns a copy of the original rectangle.
Translate( DX As Integer, DY As Integer )Moves the rectangle (relative to the original coordinates) by DX and DY.
Move( X As Integer, Y As Integer [ , Width As Integer, Height As Integer ] )Moves the rectangle to the new coordinates Y, Y (BasePoint) and (optionally) also changes the width and height of the rectangle.
Resize( Width As Integer, Height As Integer ) Only changes the width and height of the rectangle.
Adjust( Left As Integer [ , Top As Integer, Right As Integer, Bottom As Integer ] )Reduces or enlarges the rectangle. A positive value causes a reduction in size, while a negative value causes an enlargement. If no value is specified for Top, it assumes the value of Left. If no value is set for Right, it assumes the value of Left. If no value is set for Bottom, it assumes the value of Top.
Contains( X As Integer, Y As Integer ) As Boolean Returns True if the point P(X|Y) is in the rectangle.
Intersection( Rect1 As Rect ) As Rect Returns a rectangle if the rectangle Rect1 intersects with the current rectangle in a rectangle (average). If the two rectangles do not overlap, NULL is returned.
Union( Rect1 As Rect ) As Rect Returns the (smallest) enclosing rectangle that the two rectangles Rect1 and the current rectangle form.

Table 25.2.4.2.1 : Methods of the Rect class

## 25.2.4.3 Examples

The Rect class can be created and used like a (static) function:

```Dim hRect As Rect
hRect = New Rect(100, 100, 300, 200)```

In the next example, several rectangles and a point P are created and methods of the Point and Rect classes are used. The results of the operations with the rectangles and the point as operands are output or visualised in the console:

```[1] Public Sub ScriptRectangles()
[2]   Dim PointP As Point
[3]   Dim RectRed, RectGreen, RectCopy, RectIntersection, RectUnion As Rect
[4]
[5]   PointP = New Point(177, 144)
[6]   RectRed = New Rect(60, 60, 400, 180)
[7]   RectGreen = New Rect(130, 20, 90, 170)
[8]   RectIntersection = New Rect
[9]   RectUnion = New Rect
[10]
[11]   Print "P in RectRed? --> "; RectRed.Contains(PointP.X, PointP.Y)
[12]   Print "P in RectRed? --> "; PointP.InRect(RectRed)
[13]   Print "Cx = "; RectRed.Center().X; " , Cy = "; RectRed.Center().Y
[14]   RectCopy = RectRed.Copy()
[15]   Print "R3x = "; RectCopy.X; " , R3y = "; RectCopy.Y
[16]   RectCopy.Clear
[17]
[18]   RectIntersection = RectRed.Intersection(RectGreen)
[19]   Print "RIx = "; RectIntersection.X; " , RIy = "; RectIntersection.Y
[20]   Print "RIW = "; RectIntersection.Width; " , RIH = "; RectIntersection.Height
[21]   RectUnion = RectRed.Union(RectGreen)
[22]   Print "RIx = "; RectUnion.X; " , RIy = "; RectUnion.Y
[23]   Print "RIW = "; RectUnion.Width; " , RIH = "; RectUnion.Height
[24]
[25]   GenerateNewPicture()
[26]   SetPictureBorder()
[27]   Paint.Begin(hPicture)
[28]     Paint.Translate(xTranslate, yTranslate)
[29]     Paint.Scale(xScale, yScale) ' +y ▲
[30]     Paint.AntiAlias = False
[31]     DrawCoordinateSystem() ' +y ▲
[32] '-- Original coordinate system --> +y-direction downwards
[33] '-- Red rectangle
[34]     Paint.Brush = Paint.Color(Color.Red)
[35]     Paint.Rectangle(RectRed.X, RectRed.Y, RectRed.W, RectRed.H)
[36]     Paint.Fill(True)
[37]     Paint.Brush = Paint.Color(Color.DarkGray)
[38]     Paint.LineWidth = 2
[39]     Paint.Stroke()
[40] '-- Green rectangle
[41]     Paint.Brush = Paint.Color(Color.Green)
[42]     Paint.Rectangle(RectGreen.X, RectGreen.Y, RectGreen.W, RectGreen.H)
[43]     Paint.Fill(True)
[44]     Paint.Brush = Paint.Color(Color.DarkGray)
[45]     Paint.LineWidth = 2
[46]     Paint.Stroke()
[47] '-- Blue rectangle (intersection)
[48]     Paint.Brush = Paint.Color(Color.Blue)
[49]     Paint.Rectangle(RectIntersection.X, RectIntersection.Y, RectIntersection.W, RectIntersection.H)
[50]     Paint.Fill(True)
[51]     Paint.Brush = Paint.Color(Color.DarkGray)
[52]     Paint.LineWidth = 2
[53]     Paint.Stroke()
[54] '-- Bordered rectangle (union - union set)
[55]     Paint.Brush = Paint.Color(Color.Magenta)
[56]     Paint.Rectangle(RectUnion.X, RectUnion.Y, RectUnion.W, RectUnion.H)
[57]     Paint.Dash = [2, 2]
[58]       Paint.LineWidth = 2
[59]       Paint.Stroke()
[60]     Paint.Dash = Null
[61]     Paint.FillRect(260, 150, 3, 3, Color.Black) ' Mittelpunkt C einzeichnen
[62] '-- Label the centre point C of the diagonal in the red rectangle
[63]     Paint.Scale(1, -1) ' +y ▼
[64]       Paint.Font = Font["Monospace, 10"]
[65]       Paint.Brush = Paint.Color(Color.Black)
[66]       Paint.DrawText("C(260|150)", 270, -145)
[67]       Paint.Font = Font["Monospace, 8"]
[68]     Paint.Scale(1, -1) ' +y ▲
[69]   Paint.End()
[70]
[71] End  ```

These results are displayed in the console in the IDE:

```P in RectRed? --> True
P in RectRed? --> True
Cx = 260 , Cy = 150
R3x = 60 , R3y = 60
RIx = 130 , RIy = 60
RIW = 90 , RIH = 130
RIx = 60 , RIy = 20
RIW = 400 , RIH = 220```

In the following illustration, you can also read these results with sufficient accuracy:

Figure 25.2.4.3.1: Mutual position of the rectangles RectRed and RectGreen

Lines 48 to 61 also show the two rectangles (blue and with a dashed magenta border) that were created in lines 18 and 22 when using the methods Rect.Intersection(..) and Rect.Union(..) from the two rectangles RectRed and RectGreen:

Figure 25.2.4.3.2: Created rectangles: Rect.Intersection(..) and Rect.Union(..)

## 25.2.4.4 Class RectF

While only integers are permitted as coordinates for the Rect class, you can use real numbers for the coordinates X and Y as well as for the width and height for the RectF class.

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.