# Selection rectangle that can go backwards

How can I make a selection rectangle? Basically, my only problem is getting the rectangle to work when the mouse is dragged to the left from the selection point or to the top from the selection point.

I wrote this:

``````    SelectionRectangle.Top = Min(SelectionPoint.Y, Y)
SelectionRectangle.Bottom = Max(SelectionPoint.Y, Y)

SelectionRectangle.Left = Min(SelectionPoint.X, X)
SelectionRectangle.Right = Max(SelectionPoint.X, X)``````

It does not look great. So I tried this:

``````    SelectionRectangle.Top = SelectionPoint.Y
SelectionRectangle.Bottom = Y

SelectionRectangle.Left = SelectionPoint.X
SelectionRectangle.Right = X``````

This worked fine, but only if I am extending the rectangle to the right. The first one looks shaky around the starting point.

The code for the ‘Bottom’ property is:

``````Bottom As Integer
Get
return me.Top + me.height
End Get
Set
me.height = value - me.top
End Set``````

And ‘Right’:

``````Right As Integer

Get
return me.Left + me.width
End Get
Set
me.width = value - me.left
End Set``````

Thanks

I had to solve this same problem some time ago. Here’s my method for converting screen coordinates into a Realbasic.Rect object whose Left/Top/Width/Height can be passed directly to `Graphics.DrawRect`.

``````Function GetSelectionRect(StartX As Integer, StartY As Integer, EndX As Integer, EndY As Integer) As REALbasic.Rect
Dim r As New REALbasic.Rect
If EndX < StartX Then
r.Left = EndX
r.Width = StartX - EndX
Else
r.Left = StartX
r.Width = EndX - StartX
End If
If EndY < StartY Then
r.Top = EndY
r.Height = StartY - EndY
Else
r.Top = StartY
r.Height = EndY - StartY
End If
Return r
End Function``````
``````Function GetSelectionRect(StartX As Integer, StartY As Integer, EndX As Integer, EndY As Integer) As REALbasic.Rect
Dim r As New REALbasic.Rect
r.left=Min(StartX,EndX)
r.top=Min(StartY,EndY)
r.width=Max(StartX,EndX)-r.left
r.height=Max(StartY,EndY)-r.top
Return r
End Function``````

No IF/THEN required

Not very readable, but you could also utilize realbasic.rect.union

```return REALbasic.Rect( new REALbasic.Rect( startx, starty, 0, 0).union( new REALbasic.Rect( endx, endy, 0, 0)) ```

[quote=162477:@Dave S]``` Function GetSelectionRect(StartX As Integer, StartY As Integer, EndX As Integer, EndY As Integer) As REALbasic.Rect Dim r As New REALbasic.Rect r.left=Min(StartX,EndX) r.top=Min(StartY,EndY) r.width=Max(StartX,EndX)-r.left r.height=Max(StartY,EndY)-r.top Return r End Function ```

No IF/THEN required[/quote]
I believe I tried something similar and it did not look good enough but I will give it a try.

Thanks

[quote=162471:@Andrew Lambert]I had to solve this same problem some time ago. Here’s my method for converting screen coordinates into a Realbasic.Rect object whose Left/Top/Width/Height can be passed directly to `Graphics.DrawRect`.

``` Function GetSelectionRect(StartX As Integer, StartY As Integer, EndX As Integer, EndY As Integer) As REALbasic.Rect Dim r As New REALbasic.Rect If EndX < StartX Then r.Left = EndX r.Width = StartX - EndX Else r.Left = StartX r.Width = EndX - StartX End If If EndY < StartY Then r.Top = EndY r.Height = StartY - EndY Else r.Top = StartY r.Height = EndY - StartY End If Return r End Function ```[/quote]
THANKS

[quote=162519:@jim mckay]Not very readable, but you could also utilize realbasic.rect.union

```return REALbasic.Rect( new REALbasic.Rect( startx, starty, 0, 0).union( new REALbasic.Rect( endx, endy, 0, 0)) ```[/quote]
THANKS

Oliver which function worked best for you? Thanks

All of the above functions return the exact same answer… so I don’t understand your statement “doesn’t look good enough”