0 Members and 1 Guest are viewing this topic.
/'****************************************************************************** Name: vec.bas** Synopsis: 2D Vector object** Description: This implements a 2D vector as an object along with useful operators.** Copyright 2006, Richard D. Clark** The Wide Open License (WOL)** Permission to use, copy, modify, distribute and sell this software and its* documentation for any purpose is hereby granted without fee, provided that* the above copyright notice and this license appear in all source copies. * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF* ANY KIND. See http://www.dspguru.com/wol.htm for more information.******************************************************************************'/'2D Vector type.Type vec Private: _x As Integer _y As Integer Public: Declare Constructor () Declare Constructor (x As Integer, y As Integer) Declare Property vx (x As Integer) Declare Property vx () As Integer Declare Property vy (y As Integer) Declare Property vy () As Integer Declare Operator Let (rhs As vec) Declare Operator += (rhs As vec) Declare Operator += (rhs As Integer) Declare Operator -= (rhs As vec) Declare Operator -= (rhs As Integer) Declare Operator *= (rhs As Double) Declare Sub ClearVec()End Type'Empty constructor. Used when part of another object.Constructor vec () _x = 0 _y = 0End Constructor'Initialzed vector.Constructor vec (x As Integer, y As Integer) _x = x _y = yEnd Constructor'Properties to set and return the x and y components.Property vec.vx (x As Integer) _x = xEnd PropertyProperty vec.vx () As Integer Return _xEnd PropertyProperty vec.vy (y As Integer) _y = yEnd PropertyProperty vec.vy () As Integer Return _yEnd Property'Assignment operator.Operator vec.Let (rhs As vec) _x = rhs.vx _y = rhs.vyEnd Operator'Translation operators. These operate on both components.Operator vec.+= (rhs As vec) _x += rhs.vx _y += rhs.vyEnd OperatorOperator vec.+= (rhs As Integer) _x += rhs _y += rhsEnd OperatorOperator vec.-= (rhs As vec) _x -= rhs.vx _y -= rhs.vyEnd OperatorOperator vec.-= (rhs As Integer) _x -= rhs _y -= rhsEnd Operator'Scaling operator.Operator vec.*= (rhs As Double) _x *= rhs _y *= rhs End Operator'Translation operators.Operator + (lhs As vec, rhs As vec) As vec Dim tmp As vec tmp.vx = lhs.vx + rhs.vx tmp.vy = lhs.vy + rhs.vy Return tmpEnd OperatorOperator - (lhs As vec, rhs As vec) As vec Dim tmp As vec tmp.vx = lhs.vx - rhs.vx tmp.vy = lhs.vy - rhs.vy Return tmpEnd Operator'Scale operator.Operator * (lhs As vec, rhs As Double) As vec Dim tmp As vec tmp.vx = lhs.vx * rhs tmp.vy = lhs.vy * rhs Return tmpEnd Operator'Sets vector to 0.Sub vec.Clearvec () _x = 0 _y = 0End Sub'*****************************************************************************'*****************************************************************************'Test program.Dim v1 As vecDim v2 As vec = vec(10, 10)Dim v3 As vec'Show initial values.Print "Inital Values"Print "Vector 1 x: " & v1.vx & " y: " & v1.vyPrint "Vector 2 x: " & v2.vx & " y: " & v2.vyPrint'Translate vec1 by 1.v1 += 1v2 += 1Print "Translate +1"Print "Vector 1 x: " & v1.vx & " y: " & v1.vyPrint "Vector 2 x: " & v2.vx & " y: " & v2.vyPrint'Translate by -1.v2 -= 1Print "Translate -1"Print "Vector 2 x: " & v2.vx & " y: " & v2.vyPrint'Scale by .5v2 *= .5Print "Scale .5"Print "Vector 2 x: " & v2.vx & " y: " & v2.vyPrint'Translate operatorsv1 += v2Print "Translate by Vector (+=)"Print "Vector 1 x: " & v1.vx & " y: " & v1.vyPrintv1 -= v2Print "Translate by Vector (-=)"Print "Vector 1 x: " & v1.vx & " y: " & v1.vyPrintv3 = v1 + v2Print "Translate by Vector (+)"Print "Vector 3 x: " & v3.vx & " y: " & v3.vyPrintv3 = v1 - v2Print "Translate by Vector (-)"Print "Vector 3 x: " & v3.vx & " y: " & v3.vyPrintv2 += 1v3 = v2 * .5Print "Scale by Vector (*)"Print "Vector 3 x: " & v3.vx & " y: " & v3.vyPrintv3.ClearVecPrint "Clear Vector"Print "Vector 3 x: " & v3.vx & " y: " & v3.vyPrintSleep