前言
在数学中,几何向量是指具有大小和方向的几何对象。
在编程中,向量应用广泛,尤其适用于图形编程和游戏物理引擎。
第一节 构造函数
通过创建一个二维向量的类(或结构体),实现向量的表示及其运算。
1. 首先,将类命名为“Vector2D”
2. 添加两个属性 X 和 Y ,分别表示二维向量的两个分量
3. 实现构造函数,实例化时即初始化 X,Y 的值
Public Class Vector2D Public Property x As Double 'x分量 Public Property y As Double 'y分量 ''' <summary> ''' 二维向量类,能够实现平面向量的表示与运算 ''' </summary> ''' <param name="x">向量的X初始值,默认为0</param> ''' <param name="y">向量的Y初始值,默认为0</param> Public Sub New(Optional x As Double = 0, Optional y As Double = 0) Me.x = x Me.y = y End Sub End Class
第二节 四则运算
1.添加 Public 方法“Add()”,实现向量与向量加法
''' <summary> ''' 加上一个向量 ''' </summary> ''' <param name="vector">要加的向量</param> ''' <remarks></remarks> Public Sub Add(ByVal vector As Vector2D) x += vector.x y += vector.y End Sub
2.添加 Public 方法“Minus()”,实现向量与向量减法
''' <summary> ''' 减去一个向量 ''' </summary> ''' <param name="vector">要减的向量</param> ''' <remarks></remarks> Public Sub Minus(ByVal vector As Vector2D) x -= vector.x y -= vector.y End Sub
3.添加 Public 方法“Multiply()”,实现向量与标量乘法
''' <summary> ''' 乘以一个标量 ''' </summary> ''' <param name="number">要乘的标量</param> ''' <remarks></remarks> Public Sub Multiply(ByVal number As Double) x *= number y *= number End Sub
4.添加 Public 方法“Divide()”,实现向量与标量除法
''' <summary> ''' 除以一个标量 ''' </summary> ''' <param name="number">要除的标量</param> ''' <remarks></remarks> Public Sub Divide(ByVal number As Double) x /= number y /= number End Sub
第三节 重载运算符
利用运算符重载(而不是调用方法)可以更简便的实现向量运算。
1. 直接在 Vector2D 类里重载运算符。
'重载向量与向量加法运算符 Public Overloads Shared Operator +(ByVal LeftVector As Vector2D, ByVal RightVector As Vector2D) As Vector2D Return New Vector2D(LeftVector.x + RightVector.x, LeftVector.y + RightVector.y) End Operator '重载向量与向量减法运算符 Public Overloads Shared Operator -(ByVal LeftVector As Vector2D, ByVal RightVector As Vector2D) As Vector2D Return New Vector2D(LeftVector.x - RightVector.x, LeftVector.y - RightVector.y) End Operator '重载向量与标量乘法运算符 Public Overloads Shared Operator *(ByVal LeftVector As Vector2D, ByVal RightNum As Double) As Vector2D Return New Vector2D(LeftVector.x * RightNum, LeftVector.y * RightNum) End Operator '重载标量与向量乘法运算符(交换律) Public Overloads Shared Operator *(ByVal LeftNum As Double, ByVal RightVector As Vector2D) As Vector2D Return New Vector2D(RightVector.x * LeftNum, RightVector.y * LeftNum) End Operator '重载向量与标量除法运算符 Public Overloads Shared Operator /(ByVal LeftVector As Vector2D, ByVal RightNum As Double) As Vector2D Return New Vector2D(LeftVector.x / RightNum, LeftVector.y / RightNum) End Operator
第四节 模的计算
1.添加 Public 函数“Magnitude()”,实现计算模长
''' <summary> ''' 返回向量的模长 ''' </summary> ''' <returns></returns> Public Function Magnitude() As Double Return Math.Sqrt(x * x + y * y) End Function
2.添加 Public 方法“SetMag()”,实现设定模长
''' <summary> ''' 指定向量的模长 ''' </summary> ''' <param name="sPutNum">指定的长度</param> Public Sub SetMag(ByVal sPutNum As Double) Dim tempMag As Double = Me.Magnitude x = x * (sPutNum / tempMag) y = y * (sPutNum / tempMag) End Sub
3.添加 Public 方法“LimitMag()”,实现限制模长
''' <summary> ''' 限制向量模长,小于或等于某一值 ''' </summary> ''' <param name="lUponNum">指定的最大值</param> Public Sub LimitMag(ByVal lUponNum As Double) Dim tempMag As Double = Me.Magnitude If tempMag > lUponNum Then x = x * (lUponNum / tempMag) y = y * (lUponNum / tempMag) End If End Sub
第五节 夹角与旋转
1.添加 Public Shared 函数“GetHeading()”,实现计算向量的方向角
''' <summary> ''' '求向量的方向角 ''' </summary> ''' <param name="gVector">指定的向量</param> ''' <returns></returns> Public Shared Function GetHeading(ByVal gVector As Vector2D) As Double Dim Angle As Double Angle = Math.Asin(gVector.x / Math.Sqrt(gVector.x * gVector.x + gVector.y * gVector.y)) * (180 / Math.PI) Return Angle End Function
2.添加 Public Shared 函数“GetAngleBetween()”,实现计算两个向量的夹角
''' <summary> ''' '求两向量的夹角 ''' </summary> ''' <param name="gLeftVector">第一个向量</param> ''' <param name="gRightVector">第二个向量</param> ''' <returns></returns> Public Shared Function GetAngleBetween(ByVal gLeftVector As Vector2D, ByVal gRightVector As Vector2D) As Double Dim Angle As Double Angle = Math.Asin((gLeftVector.x * gRightVector.x + gLeftVector.y * gRightVector.y) / (Math.Sqrt(gLeftVector.x * gLeftVector.x + gLeftVector.y * gLeftVector.y) * Math.Sqrt(gRightVector.x * gRightVector.x + gRightVector.y * gRightVector.y))) * (180 / Math.PI) Return Angle End Function
3.添加Public方法“Rotate()”,实现向量旋转
''' <summary> ''' 向量旋转 ''' </summary> ''' <param name="gAngle">指定旋转的角度,弧度制</param> Public Sub Rotate(ByVal gAngle As Double) Dim x1, y1 As Double x1 = x * Math.Cos(gAngle) - y * Math.Sin(gAngle) y1 = y * Math.Cos(gAngle) + x * Math.Sin(gAngle) x = x1 y = y1 End Sub
附录
System.Numerics 命名空间提供了一套启用了 SIMD 的矢量类型。
Vector2 结构:System.Numerics.Vector2