Tipp-Upload: VB.NET 0350: Vektoren in VB
von Dario
Über den Tipp
Dieser Tippvorschlag ist noch unbewertet.
Der Vorschlag ist in den folgenden Kategorien zu finden:
- Mathematik
- Sonstiges
Dem Tippvorschlag wurden folgende Schlüsselwörter zugeordnet:
vector, vektor, vector2, vector3, 2d, 2d, lineare algebra, analytische geometrie, gerade, ebene, kugel, sphere, plane, vec, punkt, directx
Der Vorschlag wurde erstellt am: 19.02.2009 18:57.
Die letzte Aktualisierung erfolgte am 19.02.2009 18:58.
Beschreibung
Bei vielen Simulationsprogrammen benötigt man Vektorrechnung. Das ist in VB dank Operatorüberladung 1:1 formulierbar - Man kann Vektoren ohne jeden Umstand addieren, multiplizieren, abziehen, nur anhand der gewohnten +, -, * ... - Operatoren.
In diesem Tipp wird eine Implementierung von solchen Vektor-Klassen (für 2D und 3D) gezeigt und ein paar Körper wie Kugeln oder Ebenen im 3D-Raum implementiert. Alternativ kann man auch die Vektor-Klassen der DirectX-Bibliothek verwenden (Projekt->Verweis hinzufügen->Microsoft DirectX ...)
Schwierigkeitsgrad |
Verwendete API-Aufrufe: |
Download: |
' Dieser Source stammt von http://www.activevb.de ' und kann frei verwendet werden. Für eventuelle Schäden ' wird nicht gehaftet. ' Um Fehler oder Fragen zu klären, nutzen Sie bitte unser Forum. ' Ansonsten viel Spaß und Erfolg mit diesem Source! ' ' Beachten Sie, das vom Designer generierter Code hier ausgeblendet wird. ' In den Zip-Dateien ist er jedoch zu finden. ' ------------- Anfang Projektgruppe Vectors.sln ------------- ' ------------ Anfang Projektdatei Vectors.vbproj ------------ ' ----------------- Anfang Datei Module1.vb ----------------- Module Module1 ' Ein paar Körper Class Sphere Public m As Vector3 Public r As Double Public Function Contains(ByVal x As Vector3) As Boolean Return (x - m).Length <= r End Function End Class Class Plane Private p As Vector3 Private n As Vector3 Public Sub New(ByVal a As Vector3, ByVal b As Vector3, ByVal c As Vector3) p = a n = Vector3.Cross(b - a, c - a).Normalize End Sub Public Function Distance(ByVal x As Vector3) As Double Return Math.Abs((x - p) * n) End Function End Class Sub Main() ' Vektorrechnung: 2D Dim a As New Vector2(3, 1) Dim b As New Vector2(-4, 0) Dim O As Vector2 Console.WriteLine(3 * (2 * a + b - O) / 4) ' Vektorrechnung: 3D Dim x As New Vector3(-6, 0, 21) x *= 2 x -= New Vector3(-5, 2, 0) Console.WriteLine("|{0}| = {1}", x, x.Length) Dim Sphere = New Sphere With {.m = New Vector3(-6, 3, 3), .r = 50} Console.WriteLine("x in der Kugel enthalten? {0}", Sphere.Contains(x)) Dim y As New Vector3(4, 6, -1) Dim Plane = New Plane(New Vector3(-1, -1, -1), New Vector3(11, -1, 3), New Vector3(0, _ -1, 2)) Console.WriteLine("Abstand von {0} zur Ebene: {1}", y, Plane.Distance(y)) Console.ReadKey() End Sub End Module ' ------------------ Ende Datei Module1.vb ------------------ ' ----------------- Anfang Datei Vector2.vb ----------------- Option Strict On <DebuggerStepThrough()> <DebuggerDisplay("{ToString}")> Public Structure Vector2 Private ReadOnly m_x As Double Private ReadOnly m_y As Double Private ReadOnly m_LenSq As Double Public ReadOnly Property x() As Double Get Return m_x End Get End Property Public ReadOnly Property y() As Double Get Return m_y End Get End Property Public Sub New(ByVal v As Vector2) Me.New(v.x, v.y) End Sub Public Sub New(ByVal x As Double, ByVal y As Double) m_x = x m_y = y m_LenSq = x * x + y * y End Sub Public ReadOnly Property LengthSq() As Double Get Return m_LenSq End Get End Property Public ReadOnly Property Length() As Double Get Return Math.Sqrt(m_LenSq) End Get End Property Public ReadOnly Property Normalize() As Vector2 Get Return Me / Me.Length End Get End Property Public Shared Operator +(ByVal a As Vector2, ByVal b As Vector2) As Vector2 Return New Vector2(a.x + b.x, a.y + b.y) End Operator Public Shared Operator -(ByVal a As Vector2, ByVal b As Vector2) As Vector2 Return New Vector2(a.x - b.x, a.y - b.y) End Operator Public Shared Operator *(ByVal a As Vector2, ByVal b As Vector2) As Double Return a.x * b.x + a.y * b.y End Operator Public Shared Operator *(ByVal v As Vector2, ByVal r As Double) As Vector2 Return New Vector2(v.x * r, v.y * r) End Operator Public Shared Operator *(ByVal r As Double, ByVal v As Vector2) As Vector2 Return v * r End Operator Public Shared Operator /(ByVal v As Vector2, ByVal r As Double) As Vector2 Return New Vector2(v.x / r, v.y / r) End Operator Public Shared Operator =(ByVal a As Vector2, ByVal b As Vector2) As Boolean Return a.x = b.x AndAlso a.y = b.y End Operator Public Shared Operator <>(ByVal a As Vector2, ByVal b As Vector2) As Boolean Return Not a = b End Operator Public Overrides Function ToString() As String Return String.Format("({0}|{1})", x, y) End Function End Structure ' ------------------ Ende Datei Vector2.vb ------------------ ' ----------------- Anfang Datei Vector3.vb ----------------- Option Strict On <DebuggerStepThrough()> <DebuggerDisplay("{ToString}")> Public Structure Vector3 Private ReadOnly m_x As Double Private ReadOnly m_y As Double Private ReadOnly m_z As Double Private ReadOnly m_LenSq As Double Public ReadOnly Property x() As Double Get Return m_x End Get End Property Public ReadOnly Property y() As Double Get Return m_y End Get End Property Public ReadOnly Property z() As Double Get Return m_z End Get End Property Public Sub New(ByVal v As Vector3) Me.New(v.x, v.y, v.z) End Sub Public Sub New(ByVal x As Double, ByVal y As Double, ByVal z As Double) m_x = x m_y = y m_z = z m_LenSq = x * x + y * y + z * z End Sub Public ReadOnly Property LengthSq() As Double Get Return m_LenSq End Get End Property Public ReadOnly Property Length() As Double Get Return Math.Sqrt(m_LenSq) End Get End Property Public ReadOnly Property Normalize() As Vector3 Get Return Me / Me.Length End Get End Property Public Shared Function Cross(ByVal a As Vector3, ByVal b As Vector3) As Vector3 Return New Vector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x) End Function Public Shared Operator +(ByVal a As Vector3, ByVal b As Vector3) As Vector3 Return New Vector3(a.x + b.x, a.y + b.y, a.z + b.z) End Operator Public Shared Operator -(ByVal a As Vector3, ByVal b As Vector3) As Vector3 Return New Vector3(a.x - b.x, a.y - b.y, a.z - b.z) End Operator Public Shared Operator *(ByVal a As Vector3, ByVal b As Vector3) As Double Return a.x * b.x + a.y * b.y + a.z * b.z End Operator Public Shared Operator *(ByVal v As Vector3, ByVal r As Double) As Vector3 Return New Vector3(v.x * r, v.y * r, v.z * r) End Operator Public Shared Operator *(ByVal r As Double, ByVal v As Vector3) As Vector3 Return v * r End Operator Public Shared Operator /(ByVal v As Vector3, ByVal r As Double) As Vector3 Return New Vector3(v.x / r, v.y / r, v.z / r) End Operator Public Shared Operator =(ByVal a As Vector3, ByVal b As Vector3) As Boolean Return a.x = b.x AndAlso a.y = b.y AndAlso a.z = b.z End Operator Public Shared Operator <>(ByVal a As Vector3, ByVal b As Vector3) As Boolean Return Not a = b End Operator Public Overrides Function ToString() As String Return String.Format("({0}|{1}|{2})", x, y, z) End Function End Structure ' ------------------ Ende Datei Vector3.vb ------------------ ' ------------- Ende Projektdatei Vectors.vbproj ------------- ' -------------- Ende Projektgruppe Vectors.sln --------------
Diskussion
Diese Funktion ermöglicht es, Fragen, die die Veröffentlichung des Tipps betreffen, zu klären, oder Anregungen und Verbesserungsvorschläge einzubringen. Nach der Veröffentlichung des Tipps werden diese Beiträge nicht weiter verlinkt. Allgemeine Fragen zum Inhalt sollten daher hier nicht geklärt werden.
Um eine Diskussion eröffnen zu können, müssen sie angemeldet sein.