码迷,mamicode.com
首页 > 其他好文 > 详细

向量及其运算(一):二维向量的基本运算

时间:2015-07-22 22:07:37      阅读:385      评论:0      收藏:0      [点我收藏+]

标签:

  技术分享

前言

     在数学中,几何向量指具有大小(magnitude)和方向的几何对象,它在线性代数中经由抽象化有着更一般的概念。向量在编程中也有着及其广泛的应用,其作用在图形编程和游戏物理引擎方面尤为突出。

   本文以二维向量为例,基于面向对象编程语言,我们创建一个二维向量的类(Class),就能够在编程中轻松实现向量的表示及其运算

 


 

1.构造函数

  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="nX">向量的X初始值,不填写默认为0</param>
    ‘‘‘ <param name="nY">向量的Y初始值,不填写默认为0</param>
    Public Sub New(Optional nX As Double = 0, Optional nY As Double = 0)
        x = nX
        y = nY
    End Sub
End Class

 


 

2.四则运算函数

  1.添加Public方法"Add()",实现向量与向量加法

    ‘‘‘ <summary>
    ‘‘‘ 加上一个向量
    ‘‘‘ </summary>
    ‘‘‘ <param name="aVector">要加的向量</param>
    ‘‘‘ <remarks></remarks>
    Public Sub Add(ByVal aVector As Vector2D)
        x += aVector.x
        y += aVector.y
    End Sub

  

  2.添加Public方法"Minus()",实现向量与向量减法

    ‘‘‘ <summary>
    ‘‘‘ 减去一个向量
    ‘‘‘ </summary>
    ‘‘‘ <param name="mVector">要减的向量</param>
    ‘‘‘ <remarks></remarks>
    Public Sub Minus(ByVal mVector As Vector2D)
        x -= mVector.x
        y -= mVector.y
    End Sub

  

  3.添加Public方法"Multiply()",实现向量与标量乘法

    ‘‘‘ <summary>
    ‘‘‘ 乘以一个标量
    ‘‘‘ </summary>
    ‘‘‘ <param name="mNum">要乘的标量</param>
    ‘‘‘ <remarks></remarks>
    Public Sub Multiply(ByVal mNum As Double)
        x *= mNum
        y *= mNum
    End Sub

  

  4.添加Public方法"Divide()",实现向量与标量除法

    ‘‘‘ <summary>
    ‘‘‘ 除以一个标量
    ‘‘‘ </summary>
    ‘‘‘ <param name="dNum">要除的标量</param>
    ‘‘‘ <remarks></remarks>
    Public Sub Divide(ByVal dNum As Double)
        x /= dNum
        y /= dNum
    End Sub

 


 

3.重载四则运算符

  1.利用运算符可以更简便的实现向量运算(而不是调用方法),这就需要我们在类里重载运算符。

    重载向量与向量加法运算符
    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, LeftNum.y * RightVector)
    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

 


 

4.模的计算

  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

 


 

5.夹角与旋转

  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

 

向量及其运算(一):二维向量的基本运算

标签:

原文地址:http://www.cnblogs.com/experdot/p/4668573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!