gpt4 book ai didi

.net - 有没有更简单的方法从两点创建正矩形?

转载 作者:行者123 更新时间:2023-12-02 21:50:18 26 4
gpt4 key购买 nike

我有两组点,我想从这两个点创建一个始终为正的矩形,即最低的坐标对是起点,最高的坐标对是终点。

我创建了一个函数来执行此操作,但它看起来不太优雅 - 是执行此操作的更好方法/内置功能吗?

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

最佳答案

也许是这样的

Dim rX = Math.Min(startX, currentX)
Dim rY = Math.Min(startY, currentY)
Dim rWidth = Math.Abs(startX - currentX)
Dim rHeight = Math.Abs(startY - currentY)
Return New Rectangle(rX, rY, rWidth, rHeight)

关于.net - 有没有更简单的方法从两点创建正矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18768151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com