?? vector2d.vb
字號:
Public Class Vector2D
Public x As Double
Public y As Double
Public Sub New()
x = 0.0
y = 0.0
End Sub
Public Sub New(ByVal x1 As Double, ByVal y1 As Double)
x = x1
y = y1
End Sub
Public ReadOnly Property IsZero() As Boolean
Get
Return x * x + y * y < Double.MinValue
End Get
End Property
Public Sub Zero()
x = 0.0
y = 0.0
End Sub
'返回向量的長度
Public ReadOnly Property Length() As Double
Get
Return Math.Sqrt(x * x + y * y)
End Get
End Property
'返回向量的長度的平方
Public ReadOnly Property LengthSQ() As Double
Get
Return x * x + y * y
End Get
End Property
Public Sub Normalize()
Dim vector_length As Double = Length
If vector_length > Double.Epsilon Then
x /= vector_length
y /= vector_length
End If
End Sub
Public Function Dot(ByVal v2 As Vector2D) As Double
Return x * v2.x + y * v2.y
End Function
Enum enumSign
clockwise = 1 '順時針
anticlockwise = -1 '逆時針
End Enum
'返回向量的方向
Public Function Sign(ByVal v2 As Vector2D) As enumSign
If (y * v2.x > x * v2.y) Then
Return enumSign.anticlockwise
Else
Return enumSign.clockwise
End If
End Function
'返回該向量的正交向量
Public Function Perp() As Vector2D
Return New Vector2D(-y, x)
End Function
'調整向量,使它的長度不超過max
Public Sub Truncate(ByVal max As Double)
If Length > max Then
Normalize()
'TODO:需要重寫操作符 *=
'*this *= max
Me.MutiplyEqual(max)
End If
End Sub
Public Sub Reflect(ByVal norm As Vector2D)
'TODO:需要重寫操作符 +=
'*this += 2.0 * this->Dot(norm) * norm.GetReverse();
Me.PlusEqual(norm.GetReverse().Mutiply(2.0 * Dot(norm)))
End Sub
Public Function GetReverse() As Vector2D
Return New Vector2D(-x, -y)
End Function
Public Function Distance(ByVal v2 As Vector2D) As Double
Dim ySeparation As Double = v2.y - y
Dim xSeparation As Double = v2.x - x
Return Math.Sqrt(ySeparation * ySeparation + xSeparation * xSeparation)
End Function
Public Function DistanceSq(ByVal v2 As Vector2D) As Double
Dim ySeparation As Double = v2.y - y
Dim xSeparation As Double = v2.x - x
Return ySeparation * ySeparation + xSeparation * xSeparation
End Function
'''''''''''''''''''
'模仿操作符重載程序
'''''''''''''''''''
'+=
Public Sub PlusEqual(ByVal v As Vector2D)
x += v.x
y += v.y
End Sub
'-=
Public Sub MinusEqual(ByVal v As Vector2D)
x -= v.x
y -= v.y
End Sub
'*=
Public Sub MutiplyEqual(ByVal rhs As Double)
x *= rhs
y *= rhs
End Sub
'/=
Public Sub DividedEqual(ByVal rhs As Double)
x /= rhs
y /= rhs
End Sub
'==
Public Function IsEqual(ByVal v As Vector2D) As Boolean
Return DoubleEqual(x, v.x) And DoubleEqual(y, v.y) ' System.Double.Equals(x, v.x) And System.Double.Equals(y, v.y)
End Function
'!=
Public Function NotEqual(ByVal v As Vector2D) As Boolean
Return x <> v.x And y <> v.y
End Function
Public Shared Function DoubleEqual(ByVal d1 As Double, ByVal d2 As Double)
Return Math.Abs(d1 - d2) < 0.000000000001
End Function
'+
Public Function Plus(ByVal v As Vector2D) As Vector2D
Return New Vector2D(x + v.x, y + v.y)
End Function
'-
Public Function Minus(ByVal v As Vector2D) As Vector2D
Return New Vector2D(x - v.x, y - v.y)
End Function
'*
Public Function Mutiply(ByVal rhs As Double) As Vector2D
Dim v As New Vector2D(x, y)
v.MutiplyEqual(rhs)
Return v
End Function
'/
Public Function Divided(ByVal rhs As Double) As Vector2D
Dim v As New Vector2D(x, y)
v.DividedEqual(rhs)
Return v
End Function
'''''''''''''''''''
'模仿操作符重載程序結束
'''''''''''''''''''
'''''''''''''''''''
'實用程序
''''''''''''''''''
Public Shared Function Vec2DNormalize(ByVal v As Vector2D) As Vector2D
Dim vec As Vector2D = v
Dim vector_length As Double = vec.Length()
If vector_length > Double.Epsilon Then
vec.x /= vector_length
vec.y /= vector_length
End If
Return vec
End Function
Public Shared Function Vec2DDistance(ByVal v1 As Vector2D, ByVal v2 As Vector2D) As Double
Dim ySeparation As Double = v2.y - v1.y
Dim xSeparation As Double = v2.x - v1.x
Return Math.Sqrt(ySeparation * ySeparation + xSeparation * xSeparation)
End Function
Public Shared Function Vec2DDistanceSQ(ByVal v1 As Vector2D, ByVal v2 As Vector2D) As Double
Dim ySeparation As Double = v2.y - v1.y
Dim xSeparation As Double = v2.x - v1.x
Return ySeparation * ySeparation + xSeparation * xSeparation
End Function
Public Shared Function Vec2DLength(ByVal v As Vector2D) As Double
Return Math.Sqrt(v.x * v.x + v.y * v.y)
End Function
Public Shared Function Vec2DLengthSQ(ByVal v As Vector2D) As Double
Return v.x * v.x + v.y * v.y
End Function
Public Shared Function POINTStoVector(ByVal p As PointF) As Vector2D
Return New Vector2D(p.X, p.Y)
End Function
Public Shared Function POINTtoVector(ByVal p As Point) As Vector2D
Return New Vector2D(p.X, p.Y)
End Function
Public Shared Function VectorToPOINTS(ByVal v As Vector2D) As PointF
Return New PointF(v.x, v.y)
End Function
Public Shared Function VectorToPOINT(ByVal v As Vector2D) As Point
Return New Point(v.x, v.y)
End Function
Public Shared Sub WrapAround(ByVal pos As Vector2D, ByVal MaxX As Integer, ByVal MaxY As Integer)
If pos.x > MaxX Then pos.x = 0.0
If pos.x < 0 Then pos.x = MaxX
If pos.y < 0 Then pos.y = MaxY
If pos.y > MaxY Then pos.y = 0.0
End Sub
'測試點p是否在top_left與bot_rgt所在的區域
Public Shared Function NotInsideRegion(ByVal p As Vector2D, ByVal top_left As Vector2D, ByVal bot_rgt As Vector2D) As Boolean
Return (p.x < top_left.x) Or (p.x > bot_rgt.x) Or (p.y < top_left.y) Or (p.y > bot_rgt.y)
End Function
Public Shared Function InsideRegion(ByVal p As Vector2D, ByVal top_left As Vector2D, ByVal bot_rgt As Vector2D) As Boolean
Return Not ((p.x < top_left.x) Or (p.x > bot_rgt.x) Or (p.y < top_left.y) Or (p.y > bot_rgt.y))
End Function
Public Shared Function InsideRegion(ByVal p As Vector2D, ByVal left As Integer, ByVal top As Integer, ByVal right As Integer, ByVal bottom As Integer) As Boolean
Return Not ((p.x < left) Or (p.x > right) Or (p.y < top) Or (p.y > bottom))
End Function
Public Shared Function isSecondInFOVOfFirst(ByVal posFirst As Vector2D, ByVal facingFirst As Vector2D, ByVal posSecond As Vector2D, ByVal fov As Double) As Boolean
Dim toTarget As Vector2D = Vec2DNormalize(posSecond.Minus(posFirst))
Return facingFirst.Dot(toTarget) >= Math.Cos(fov / 2.0)
End Function
Public Function Copy() As Vector2D
Return New Vector2D(x, y)
End Function
End Class
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -