Jump to content

Vector UDF


jaberwacky
 Share

Recommended Posts

I am not exactly sure how useful this UDF will prove to be, but maybe one of you AutoIt wizards could make use of this UDF. Anyways, I'm only making this to learn vectors because I'd like to learn more about computer graphics. Please post any improvements, comments, bug reports, derision, etc. Anyway, whatever, here goes:

***Undergoing extensive refactoring. Do not use until furthur notice. Thank you.***

Edit (07-19-2010) - Updated to actually work.

Requirement(s):

1) AutoItObject.au3 (http://www.autoitscript.com/forum/index.php?showtopic=110379)

Example:

#include <AutoItObject.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <Misc.au3>
#include <Vector UDF.au3>

_Singleton(@ScriptName)

#region Set up the GUI
Opt("GuiOnEventMode", 1)

Global Const $hGUI = GUICreate("Vector Example", 1000, 1000, 10, 10)
GUISetOnEvent($GUI_EVENT_CLOSE, "term")

GUICtrlCreateLabel("Distance:", 800, 20, 50, 20)
Global Const $lengthLbl = GUICtrlCreateLabel('', 855, 20, 20, 40)

GUICtrlCreateLabel("Unit A:", 800, 40, 50, 20)
Global Const $unitALbl = GUICtrlCreateLabel('', 855, 40, 100, 40)

GUICtrlCreateLabel("Unit B:", 800, 60, 50, 20)
Global Const $unitBLbl = GUICtrlCreateLabel('', 855, 60, 100, 40)

GUICtrlCreateLabel("Product:", 800, 80, 50, 20)
GUICtrlCreateLabel("x:", 800, 100, 50, 20)
Global Const $productXLbl = GUICtrlCreateLabel('0', 810, 100, 30, 40)
GUICtrlCreateLabel("y:", 855, 100, 50, 20)
Global Const $productYLbl = GUICtrlCreateLabel('0', 865, 100, 30, 40)
#endregion Set up the GUI

#region The set of two coordinates from which to calculate a vector
Global Const $v1[2] = [80, 85]
Global Const $v2[2] = [600, 750]
#endregion The set of two coordinates from which to calculate a vector

_AutoItObject_Startup()

Global $v = Vector()

$v.Init($v1, $v2)

Global $unit

GUICtrlSetData($lengthLbl, $v.Length())

Global Const $product = $v.Multiply(2)

GUICtrlSetData($productXLbl, $product[0])
GUICtrlSetData($productYLbl, $product[1])

$unit = $v.Unit()
GUICtrlSetData($unitALbl, $unit[0])
GUICtrlSetData($unitBLbl, $unit[1])

GUISetState()

_GDIPlus_Startup()

Global Const $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
Global Const $hPen = _GDIPlus_PenCreate()

_GDIPlus_GraphicsDrawLine($hGraphic, $v1[0], $v1[1], $v2[0], $v2[1])

While 1
    Sleep(100)
WEnd

$v = ''

_AutoItObject_Shutdown()

_GDIPlus_PenDispose($hPen)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_Shutdown()

Func term()
    Exit
EndFunc ;==>term

Vector UDF:

; =======================================================================================
; Name ....: Vector UDF
; Desc. ...: Use this UDF to take care of your vector computing needs.
; Syntax ..: Vector()
; Author ..: Matthew G.
; Remarks .: This UDF requires AutoItObject.au3.
; Links ...; AutoItObject.au3 can be had here: (http://www.autoitscript.com/forum/index.php?showtopic=110379)
;   Got the math from here: (http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/)
; Example .; Yes
; Available functions .: Add | Cross | Distance | Dot | Magnitude | Normalize
Func Vector()
    Local $this = _AutoItObject_Create()

    _AutoItObject_AddMethod($this, "Add", "Vector_Addition")
    _AutoItObject_AddMethod($this, "Subtract", "Vector_Subtract")
    _AutoItObject_AddMethod($this, "Multiply", "Vector_Multiply")
    _AutoItObject_AddMethod($this, "Divide", "Vector_Divide")
    _AutoItObject_AddMethod($this, "Cross", "Vector_CrossProduct")
    _AutoItObject_AddMethod($this, "Distance", "Vector_Distance")
    _AutoItObject_AddMethod($this, "Dot", "Vector_DotProduct")
    _AutoItObject_AddMethod($this, "Length", "Vector_Length")
    _AutoItObject_AddMethod($this, "Normalize", "Vector_Normalize")

    Return $this
EndFunc ;==>Vector

;#MEMBER# ===============================================================================
; Name .......: Vector_Addition
; Desc. ......: Adds the components of two vectors.
; Syntax .....: $oObject.Add($vector1, $vector2)
; Params .....: $vector1: an array containing the desired coordinates
;   $vector2: an array containing the desired coordinates
; RetVal(s) ..: Success: Returns a new vector containing the added components of two vectors.
;   Failure: -1 | @error 1
Func Vector_Addition($this, Const $vector1 = '', Const $vector2 = '')
    Select
        Case IsArray($vector1) And IsArray($vector2)
            Local Const $upbound = UBound($vector1) - 1
            Local $AddVector[$upbound + 1]

            For $i = 0 To $upbound
                $AddVector[$i] = $vector1[$i] + $vector2[$i]
            Next

            Return $AddVector
        Case Not IsArray($vector1) Or Not IsArray($vector2)
            Return SetError(1, 0, -1)
    EndSelect
EndFunc ;==>Vector_Addition

;#MEMBER# ===============================================================================
; Name  ....: Vector_Subtract
; Desc. ...: Subtracts the components of a vector.
; Syntax    ..: $oObject.Subtract($vector)
; Params    ..: $vector - an array containing the desired coordinates
; RetVal(s) ..: Success - Returns a vector containing the difference of the components of a vector.
;   Failure: -1 | @error 1
Func Vector_Subtract($this, Const $vector1 = '', Const $vector2 = '')
    Select
        Case IsArray($vector1) And IsArray($vector2)
            Local Const $upbound = UBound($vector1) - 1
            Local $SubVector[$upbound + 1]

            For $i = 0 To $upbound
                $SubVector[$i] = $vector1[$i] - $vector2[$i]
            Next

            Return $SubVector
        Case Not IsArray($vector1) Or Not IsArray($vector2)
            Return SetError(1, 0, -1)
    EndSelect
EndFunc ;==>Vector_Subtract

;#MEMBER# ===============================================================================
; Name .......: Vector_Multiply
; Desc. ......: Multiplies the components of a vector.
; Syntax .....: $oObject.Multiply($vector1, $vector2)
; Params .....: $vector1: an array containing the desired coordinates.
;   $vector2: an array containing the desired coordinates. Alternatively, this can be a scalar.
; RetVal(s) ..: Success: Returns the product of the components of a vector.
;   Failure: -1 | @error 1
Func Vector_Multiply($this, Const $vector1 = '', Const $vector2 = '')
    Select
        Case IsArray($vector1) And IsArray($vector2)
            Local Const $upbound = UBound($vector1) - 1
            Local $MultVector[$upbound + 1]

            For $i = 0 To $upbound
                $MultVector[$i] = $vector1[$i] * $vector2[$i]
            Next

            Return $MultVector
        Case IsArray($vector1) And IsNumber($vector2)
            For $i = 0 To $upbound
                $MultVector[$i] = $vector1[$i] * $vector2
            Next

            Return $MultVector
        Case Else
            Return SetError(1, 0, -1)
    EndSelect
EndFunc ;==>Vector_Multiply

;#MEMBER# ===============================================================================
; Name .......: Vector_Divide
; Desc. ......: Divides the components of a vector.
; Syntax .....: $oObject.Divide($vector1, $vector2)
; Params .....: $vector1: an array containing the desired coordinates.
;   $vector2: an array containing the desired coordinates. Alternatively, this can be a scalar.
; RetVal(s) ..: Success: Returns the of the components of a vector.
;   Failure: -1 | @error 1
Func Vector_Divide($this, Const $vector1 = '', Const $vector2 = '')
    Select
        Case IsArray($vector1) And IsArray($vector2)
            Local Const $upbound = UBound($vector1) - 1
            Local $DivVector[$upbound + 1]

            For $i = 0 To $upbound
                $DivVector[$i] = $vector1[$i] / $vector2[$i]
            Next

            Return $DivVector
        Case IsArray($vector1) And IsNumber($vector2)
            For $i = 0 To $upbound
                $DivVector[$i] = $vector1[$i] / $vector2
            Next

            Return $DivVector
        Case Else
            Return SetError(1, 0, -1)
    EndSelect
EndFunc ;==>Vector_Divide

;#MEMBER# ===============================================================================
; Name .......: Vector_Normalize
; Desc. ......: Normalizes the components of a vector.
; Syntax .....: $oObject.Normalize($vector)
; Params .....: $vector: an array containing the desired coordinates.
; RetVal(s) ..: Success: returns a normalized vector.
;   Failure: -1 | @error 1
Func Vector_Normalize($this, Const $vector = '')
    Switch IsArray($vector)
        Case 1
            If $vector[0] = 0 And $vector[1] = 0 Then Return 0

            Local Const $length = $this.Length($vector)
            Local $v[2]

            $v[0] = $vector[0] / $length
            $v[1] = $vector[1] / $length

            Return $v
        Case 0
            Return SetError(1, 0, -1)
    EndSwitch
EndFunc ;==>Vector_Normalize

;#MEMBER# ===============================================================================
; Name .......: Vector_CrossProduct
; Desc. ......: Computes the cross product of the components of two vectors.
; Syntax .....: $oObject.Cross($vector1, $vector2)
; Params .....: $vector1: an array containing the desired coordinates
;   $vector2: an array containing the desired coordinates
; RetVal(s) ..: Success: Returns a three element array containing the cross product of the components of two vectors.
;   Failure: -1 | @error 1
Func Vector_CrossProduct($this, $vector1 = '', $vector2 = '')
    If IsArray($vector1) And IsArray($vector2) Then
        If (UBound($vector1) = 2) And (UBound($vector2) = 2) Then
            Return ($vector1[0] * $vector2[1]) - ($vector1[1] * $vector2[0])
        ElseIf (UBound($vector1) = 3) And (UBound($vector2) = 3) Then
            Local $v[3]

            $v[0] = ($vector1[1] * $vector2[2]) - ($vector1[2] * $vector2[1])
            $v[1] = ($vector1[2] * $vector2[0]) - ($vector1[0] * $vector2[2])
            $v[2] = ($vector1[0] * $vector2[1]) - ($vector1[1] * $vector2[0])

            Return $v
        EndIf
    ElseIf Not IsArray($vector1) And Not IsArray($vector2) Then
        Return SetError(1, 0, -1)
    EndIf
EndFunc ;==>Vector_CrossProduct

;#MEMBER# ===============================================================================
; Name .......: Vector_Distance
; Desc. ......: Returns the distance between two vectors
; Syntax .....: $oObject.Distance($vector1, $vector2)
; Params .....: $vector1: an array containing the desired coordinates
;   $vector2: an array containing the desired coordinates
; RetVal(s) ..: Success: Returns the distance between two vectors.
;   Failure: -1 | @error 1
Func Vector_Distance($this, $vector1 = '', $vector2 = '')
    If IsArray($vector1) And IsArray($vector2) Then
        Local Const $v = $this.Subtract($vector1, $vector2)
        Return $this.Length($v)
    Else
        Return SetError(1, 0, -1)
    EndIf
EndFunc ;==>Vector_Distance

;#MEMBER# ===============================================================================
; Name .......: Vector_DotProduct
; Desc. ......:
; Syntax .....: $oObject.Dot($vector1, $vector2)
; Params .....: $vector1: an array containing the desired coordinates
;   $vector2: an array containing the desired coordinates
; RetVal(s) ..: Success:
;   Failure: -1 | @error 1
Func Vector_DotProduct($this, $vector1 = '', $vector2 = '')
    Switch IsArray($vector1) And IsArray($vector2)
        Case 1
            Switch UBound($vector1) >= 3 And UBound($vector1) >= 3
                Case 1
                    Local $DotProduct[3]
                Case 0

            EndSwitch
        Case 0
            Return SetError(1, 0, -1)
    EndSwitch
EndFunc ;==>Vector_DotProduct

;#MEMBER# ===============================================================================
; Name .......: Vector_Length
; Desc. ......:
; Syntax .....: $oObject.Length($vector1)
; Params .....: $vector1: a two or three element array containing the desired coordinates.
; RetVal(s) ..: Success:
;   Failure: -1 | @error 1
Func Vector_Length($this, Const $vector = '')
    Switch IsArray($vector)
        Case 1
            Local $v

            For $i = 0 To UBound($vector) - 1
                $v += $vector[$i] ^ 2
            Next

            Return Sqrt($v)
        Case Else
            Return SetError(1, 0, -1)
    EndSwitch
EndFunc ;==>Vector_Length

Edited by jaberwocky6669
Link to comment
Share on other sites

whrd the attachment go?

Spoiler

Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX Builder
Misc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retreive SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose Array
Projects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalc
Cool Stuff: AutoItObject UDF â—Š Extract Icon From Proc â—Š GuiCtrlFontRotate â—Š Hex Edit Funcs â—Š Run binary â—Š Service_UDF

 

Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

Changes made... I haven't tested it much, but it seems good...

If you see any improvements that need to be made please make them. I think it's still very buggy. I was just using this to learn about vectors.

Edited by jaberwocky6669
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...