Created
May 8, 2022 13:46
-
-
Save abiriadev/3a843d8cd8e6375c27e0090f4cfa80df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Imports System.Collections.Generic | |
Module Vec | |
Public Class Vector | |
Protected _v As Double() | |
Public Sub New( | |
ByVal ParamArray elements _ | |
As Double() | |
) | |
MyClass._v = elements : End Sub | |
Public Sub New( | |
ByVal initArr As Double() | |
) | |
MyClass._v = initArr : End Sub | |
NotOverridable Public ReadOnly Property Elements() As Double() | |
Get : Return MyClass._v : End Get : End Property | |
NotOverridable Public ReadOnly Property Length() As Long | |
Get : Return UBound( | |
MyClass._v, | |
1) + 1 : End Get : End Property | |
Public Shared Operator + ( | |
ByVal a As Vector, | |
ByVal b As Vector | |
) As Vector | |
Try | |
Dim tmp(a.Length - 1) As Double | |
For i As Integer = 0 To a.Length - 1 | |
tmp(i) = _ | |
a.elements(i) + _ | |
b.elements(i) : Next i | |
Return New Vector(tmp) | |
Catch _err As System.IndexOutOfRangeException | |
Throw New System _ | |
.Exception( | |
"two vectors must have same length" | |
) | |
Catch err As Exception | |
Throw err | |
End Try | |
End Operator | |
End Class | |
Public Class Vector2D : Inherits Vector | |
Public Sub New( | |
ByVal x As Double, | |
ByVal y As Double | |
) | |
Dim arr2(1) As Double | |
arr2(0) = x : arr2(1) = y | |
MyClass._v = arr2 | |
End Sub | |
Public Property x() As Double | |
Get : Return _v(0) : End Get | |
Set(ByVal newX As Double) | |
_v(0) = newX : End Set | |
End Property | |
Public Property y() As Double | |
Get : Return _v(1) : End Get | |
Set(ByVal newY As Double) | |
_v(1) = newY : End Set | |
End Property | |
Public Shared Operator + ( | |
ByVal a As Vector2D, | |
ByVal b As Vector2D | |
) As Vector2D | |
Return New Vector2D( | |
a.x + b.x, | |
a.y + b.y | |
) : End Operator | |
End Class | |
Public Class Box | |
Private _w As Double | |
Private _h As Double | |
Public Sub New( | |
ByVal width As Double, | |
ByVal height As Double | |
) | |
MyClass._w = width : MyClass._h = height | |
End Sub | |
NotOverridable Public ReadOnly Property Width() As Double | |
Get : Return _w : End Get : End Property | |
NotOverridable Public ReadOnly Property Height() As Double | |
Get : Return _h : End Get : End Property | |
Public Sub SetWidth( | |
ByVal width As Double | |
) | |
MyClass._w = width : End Sub | |
Public Sub SetHeight( | |
ByVal height As Double | |
) | |
MyClass._h = height : End Sub | |
End Class | |
Public Class Style | |
Public Color As String | |
End Class | |
Public Interface IDrawable | |
Function Move() | |
End Interface | |
Public Class Pos | |
Public x As Double | |
Public y As Double | |
End Class | |
Public Class Ball : Implements IDrawable | |
Public Function Move() Implements IDrawable.Move | |
End Function | |
Public pos As Pos | |
End Class | |
End Module | |
Module mainModule | |
Public Function Main( | |
ByVal args As String() | |
) As Integer | |
Dim v As New Vector2D(10, 20) | |
Dim v2 As New Vector2D(30, 40) | |
' For Each el As Double In (v + v2).Elements | |
' Console.WriteLine(el) | |
' Next | |
Return 0 : End Function | |
End Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment