我有两组点,我想从这两个点创建一个始终为正的矩形,即最低坐标对是起点,最高坐标对是终点。
我已经创建了一个执行此操作的函数,但它似乎不是很优雅 - 是更好的方法/内置功能吗?
Private Function CalculateDraggedRectangle(ByVal startX As Integer, ByVal startY As Integer, ByVal currentX As Integer, ByVal currentY As Integer) As Rectangle
Dim rX, rY, rWidth, rHeight As Integer
If currentX < startX Then
If currentY < startY Then
rX = currentX
rY = currentY
rWidth = startX - currentX
rHeight = startY - currentY
Else
rX = currentX
rY = startY
rWidth = startX - currentX
rHeight = currentY - startY
End If
Else
If currentY < startY Then
rX = startX
rY = currentY
rWidth = currentX - startX
rHeight = startY - currentY
Else
rX = startX
rY = startY
rWidth = currentX - startX
rHeight = currentY - startY
End If
End If
Return New Rectangle(rX, rY, rWidth, rHeight)
End Function