Library: Vectors2D

krainert

Member
Reaction score
10
EDIT:
I've posted version 2.1 in the thread Vectors and will post any future updates there as well.

Greetings,

Here's Vectors version 2.0:

JASS:
library Vectors

//******************************************************************************************************************************************************
//* Vectors
//*  
//*  ABOUT
//*   Author:   krainert
//*   Version:  2.0
//*   
//*  DESCRIPTION
//*   This library provides two vector classes:
//*   - Vector2D representing a two-dimensional vector
//*   - Vector3D representing a three-dimensional vector
//*   Note: All angles are in radians.
//*   
//*  CLASSES
//*   ======== Vector2D ========
//*    -- Description
//*     Represents a two-dimensional vector.
//*     Note: Vector2D[0] and Vector2D[1] refer to the x and y coordinates of a vector, respectively.
//*    -- Static methods
//*     static Vector2D create(real x, real y)          - instantiates a new two-dimensional vector with x coordinate x and y coordinate y
//*     static Vector2D fromDM(real d, real m)          - instantiates a new two-dimensional vector with direction d and magnitude m
//*     static Vector2D createNull()                    - instantiates a new two-dimensional vector with x and y coordinates 0
//*     static Vector2D from3D(Vector3D v)              - instantiates a new two-dimensional vector with an x coordinate equal to the x coordinate of v and a y coordinate equal to the y coordinate of v
//*    -- Properties
//*     real x                                          - the x coordinate of this vector
//*     real y                                          - the y coordinate of this vector
//*    -- Methods
//*     real     getDir()                               - returns the direction of this vector
//*     real     getMag()                               - returns the magnitude of this vector
//*     void     setDir(real d)                         - sets the direction of this vector
//*     void     setMag(real m)                         - sets the magnitude of this vector
//*     real     angle(Vector2D v)                      - returns the angle between this vector and v
//*     real     dotProduct(Vector2D v)                 - returns the dot product of this vector and v
//*     Vector2D clone()                                - returns a cloned vector
//*     Vector2D unit()                                 - returns a vector with a direction equal to the direction of this vector and magnitude 1
//*     Vector2D flip()                                 - returns a vector with an x coordinate equal to the y coordinate of this vector and a y coordinate equal to the x coordinate of this vector
//*     Vector2D inv()                                  - returns an inverted cloned vector
//*     Vector2D scale(real s)                          - returns a cloned vector scaled by s
//*     Vector2D align(Vector2D v)                      - returns a vector with a direction equal to the direction of v and a magnitude equal to the magnitude of this vector
//*     Vector2D add(Vector2D v)                        - returns the sum of this vector and v
//*     Vector2D sub(Vector2D v)                        - returns the difference between this vector and v
//*     Vector2D addXY(real x, real y)                  - returns the sum of this vector and a vector with x coordinate x and y coordinate y
//*     Vector2D subXY(real x, real y)                  - returns the difference between this vector and a vector x coordinate x and y coordinate y
//*     Vector2D addDM(real d, real m)                  - returns the sum of this vector and a vector with direction d and magnitude m
//*     Vector2D subDM(real d, real m)                  - returns the difference between this vector and a vector with direction d and magnitude m
//*     Vector2D mUnit()                                - sets the magnitude of this vector to 1 and returns the vector
//*     Vector2D mFlip()                                - sets the x coordinate of this vector to its previous y coordinate and the y coordinate of this vector to its previous x coordinate and returns the vector
//*     Vector2D mInv()                                 - inverts this vector and returns the result
//*     Vector2D mScale(real s)                         - scales this vector by an amount equal to s and returns the result
//*     Vector2D align(Vector2D v)                      - sets the direction of this vector to the direction of v and returns the result
//*     Vector2D mAdd(Vector2D v)                       - adds v to this vector and returns the result
//*     Vector2D mSub(Vector2D v)                       - subtracts v from this vector and returns the result
//*     Vector2D mAddXY(real x, real y)                 - adds a vector with x coordinate x and y coordinate y to this vector and returns the result
//*     Vector2D mSubXY(real x, real y)                 - subtracts a vector with x coordinate x and y coordinate y from this vector and returns the result
//*     Vector2D mAddDM(real d, real m)                 - adds a vector with direction d and magnitude m to this vector and returns the result
//*     Vector2D mSubDM(real d, real m)                 - subtracts a vector with direction d and magnitude m from this vector and returns the result
//*   ======== Vector3D ========
//*    -- Description
//*     Represents a three-dimensional vector.
//*     Note: Vector3D[0], Vector3D[1], and Vector3D[2] refer to the x, y, and z coordinates of a vector, respectively.
//*    -- Static methods
//*     static Vector3D create(real x, real y, real z)  - instantiates a new three-dimensional vector with x coordinate x, y coordinate y, and z coordinate z
//*     static Vector3D createNull()                    - instantiates a new three-dimensional vector with x, y, and z coordinates 0
//*     static Vector3D from2D(Vector2D v)              - instantiates a new three-dimensional vector with an x coordinate equal to the x coordinate of v, a y coordinate equal to the y coordinate of v, and z coordinate 0
//*    -- Properties
//*     real x                                          - the x coordinate of this vector
//*     real y                                          - the y coordinate of this vector
//*     real z                                          - the z coordinate of this vector
//*    -- Methods
//*     real     getMag()                               - returns the magnitude of this vector
//*     real     angle(Vector3D v)                      - returns the angle between this vector and v
//*     real     dotProduct(Vector3D v)                 - returns the dot product of this vector and v
//*     Vector3D clone()                                - returns a cloned vector
//*     Vector3D unit()                                 - returns a vector with a direction equal to the direction of this vector and magnitude 1
//*     Vector3D inv()                                  - returns an inverted cloned vector
//*     Vector3D scale(real s)                          - returns a cloned vector scaled by s
//*     Vector3D add(Vector3D v)                        - returns the sum of this vector and v
//*     Vector3D sub(Vector3D v)                        - returns the difference between this vector and v
//*     Vector3D addXYZ(real x, real y, real z)         - returns the sum of this vector and a vector with x coordinate x and y coordinate y
//*     Vector3D subXYZ(real x, real y, real z)         - returns the difference between this vector and a vector x coordinate x and y coordinate y
//*     Vector3D mUnit()                                - sets the magnitude of this vector to 1 and returns the vector
//*     Vector3D mInv()                                 - inverts this vector and returns the result
//*     Vector3D mScale(real s)                         - scales this vector by an amount equal to s and returns the result
//*     Vector3D mAdd(Vector3D v)                       - adds v to this vector and returns the result
//*     Vector3D mSub(Vector3D v)                       - subtracts v from this vector and returns the result
//*     Vector3D mAddXYZ(real x, real y, real z)        - adds a vector with x coordinate x and y coordinate y to this vector and returns the result
//*     Vector3D mSubXYZ(real x, real y, real z)        - subtracts a vector with x coordinate x and y coordinate y from this vector and returns the result
//*     
//*  CHANGELOG
//*   1.0  2011-07-08  Original release
//*   2.0  2011-07-13  Vector2D: Renamed from vector
//*                    Vector2D: Miscellaneous changes
//*                    Vector3D: Added class
//*   
//******************************************************************************************************************************************************

globals
    private constant integer MAX_2D = 8191
    private constant integer MAX_3D = 8191
endglobals

struct Vector2D[MAX_2D]
    real x
    real y
    
    method operator [] takes integer key returns real
        if key == 0 then
            return x
        else
            return y
        endif
    endmethod
    
    method operator []= takes integer key, real value returns nothing
        if key == 0 then
            set .x = value
        else
            set .y = value
        endif
    endmethod
    
    static method create takes real x, real y returns Vector2D
        local Vector2D r = Vector2D.allocate()
        set r.x = x
        set r.y = y
        return r
    endmethod
    
    static method fromDM takes real d, real m returns Vector2D
        local Vector2D r = Vector2D.allocate()
        set r.x = Cos(d)*m
        set r.y = Sin(d)*m
        return r
    endmethod
    
    static method createNull takes nothing returns Vector2D
        return Vector2D.allocate()
    endmethod
    
    static method from3D takes Vector3D v returns Vector2D
        local Vector2D r = Vector2D.allocate()
        set r.x = v.x
        set r.y = v.y
        return r
    endmethod
    
    method getDir takes nothing returns real
        return Atan2(.y, .x)
    endmethod
    
    method getMag takes nothing returns real
        return SquareRoot(Pow(.x, 2)+Pow(.y, 2))
    endmethod
    
    method setDir takes real d returns nothing
        local real m = .getMag()
        set .x = Cos(d)*m
        set .y = Sin(d)*m
    endmethod
    
    method setMag takes real m returns nothing
        local real d = .getDir()
        set .x = Cos(d)*m
        set .y = Sin(d)*m
    endmethod
    
    method angle takes Vector2D v returns real
        return Acos(dotProduct(v))
    endmethod
    
    method dotProduct takes Vector2D v returns real
        return .x*v.x+.y*v.y
    endmethod
    
    method clone takes nothing returns Vector2D
        local Vector2D r = Vector2D.allocate()
        set r.x = .x
        set r.y = .y
        return r
    endmethod
    
    method unit takes nothing returns Vector2D
        local real m = .getMag()
        local Vector2D r = Vector2D.allocate()
        set r.x = .x/m
        set r.y = .y/m
        return r
    endmethod
    
    method flip takes nothing returns Vector2D
        local Vector2D r = Vector2D.allocate()
        set r.x = .y
        set r.y = .x
        return r
    endmethod
    
    method inv takes nothing returns Vector2D
        local Vector2D r = Vector2D.allocate()
        set r.x = -.x
        set r.y = -.y
        return r
    endmethod
    
    method scale takes real s returns Vector2D
        local Vector2D r = Vector2D.allocate()
        set r.x = s*.x
        set r.y = s*.y
        return r
    endmethod
    
    method align takes Vector2D v returns Vector2D
        local real m = .getMag()/v.getMag()
        local Vector2D r = v.clone()
        set r.x = r.x*m
        set r.y = r.y*m
        return r
    endmethod
    
    method add takes Vector2D v returns Vector2D
        local Vector2D r = Vector2D.allocate()
        set r.x = .x+v.x
        set r.y = .y+v.y
        return r
    endmethod
    
    method sub takes Vector2D v returns Vector2D
        local Vector2D r = Vector2D.allocate()
        set r.x = .x-v.x
        set r.y = .y-v.y
        return r
    endmethod
    
    method addXY takes real x, real y returns Vector2D
        local Vector2D r = Vector2D.allocate()
        set r.x = .x+x
        set r.y = .y+y
        return r
    endmethod
    
    method subXY takes real x, real y returns Vector2D
        local Vector2D r = Vector2D.allocate()
        set r.x = .x-x
        set r.y = .y-y
        return r
    endmethod
    
    method addDM takes real d, real m returns Vector2D
        local Vector2D r = Vector2D.allocate()
        set r.x = .x+Cos(d)*m
        set r.y = .y+Sin(d)*m
        return r
    endmethod
    
    method subDM takes real d, real m returns Vector2D
        local Vector2D r = Vector2D.allocate()
        set r.x = .x-Cos(d)*m
        set r.y = .y-Sin(d)*m
        return r
    endmethod
    
    method mUnit takes nothing returns Vector2D
        local real m = .getMag()
        set .x = .x/m
        set .y = .y/m
        return this
    endmethod
    
    method mFlip takes nothing returns Vector2D
        local real x = .x
        set .x = .y
        set .y = x
        return this
    endmethod
    
    method mInv takes nothing returns Vector2D
        set .x = -.x
        set .y = -.y
        return this
    endmethod
    
    method mScale takes real s returns Vector2D
        set .x = s*.x
        set .y = s*.y
        return this
    endmethod
    
    method mAlign takes Vector2D v returns Vector2D
        local real d = v.getDir()
        local real m = .getMag()
        set .x = Cos(d)*m
        set .y = Sin(d)*m
        return this
    endmethod
    
    method mAdd takes Vector2D v returns Vector2D
        set .x = .x+v.x
        set .y = .y+v.y
        return this
    endmethod
    
    method mSub takes Vector2D v returns Vector2D
        set .x = .x-v.x
        set .y = .y-v.y
        return this
    endmethod
    
    method mAddXY takes real x, real y returns Vector2D
        set .x = .x+x
        set .y = .y+y
        return this
    endmethod
    
    method mSubXY takes real x, real y returns Vector2D
        set .x = .x-x
        set .y = .y-y
        return this
    endmethod
    
    method mAddDM takes real d, real m returns Vector2D
        set .x = .x+Cos(d)*m
        set .y = .y+Sin(d)*m
        return this
    endmethod
    
    method mSubDM takes real d, real m returns Vector2D
        set .x = .x-Cos(d)*m
        set .y = .y-Sin(d)*m
        return this
    endmethod
    
    method to3D takes nothing returns Vector3D
        return Vector3D.from2D(this)
    endmethod
endstruct

struct Vector3D[MAX_3D]
    real x
    real y
    real z
    
    method operator [] takes integer key returns real
        if key == 0 then
            return x
        elseif key == 1 then
            return y
        else
            return z
        endif
    endmethod
    
    method operator []= takes integer key, real value returns nothing
        if key == 0 then
            set .x = value
        elseif key == 1 then
            set .y = value
        else
            set .z = value
        endif
    endmethod
    
    static method create takes real x, real y, real z returns Vector3D
        local Vector3D r = Vector3D.allocate()
        set r.x = x
        set r.y = y
        set r.z = z
        return r
    endmethod
    
    static method from2D takes Vector2D v returns Vector3D
        local Vector3D r = Vector3D.allocate()
        set r.x = v.x
        set r.y = v.y
        return r
    endmethod
    
    static method createNull takes nothing returns Vector3D
        return Vector3D.allocate()
    endmethod
    
    method getMag takes nothing returns real
        return SquareRoot(Pow(.x, 2)+Pow(.y, 2)+Pow(.z, 2))
    endmethod
    
    method angle takes Vector3D v returns real
        return Acos(dotProduct(v))
    endmethod
    
    method dotProduct takes Vector3D v returns real
        return .x*v.x+.y*v.y+.z*v.z
    endmethod
    
    method clone takes nothing returns Vector3D
        local Vector3D r = Vector3D.allocate()
        set r.x = .x
        set r.y = .y
        set r.z = .z
        return r
    endmethod
    
    method unit takes nothing returns Vector3D
        local real m = .getMag()
        local Vector3D r = Vector3D.allocate()
        set r.x = .x/m
        set r.y = .y/m
        set r.z = .z/m
        return r
    endmethod
    
    method inv takes nothing returns Vector3D
        local Vector3D r = Vector3D.allocate()
        set r.x = -.x
        set r.y = -.y
        set r.z = -.z
        return r
    endmethod
    
    method scale takes real s returns Vector3D
        local Vector3D r = Vector3D.allocate()
        set r.x = s*.x
        set r.y = s*.y
        set r.z = s*.z
        return r
    endmethod
    
    method add takes Vector3D v returns Vector3D
        local Vector3D r = Vector3D.allocate()
        set r.x = .x+v.x
        set r.y = .y+v.y
        set r.y = .z+v.z
        return r
    endmethod
    
    method sub takes Vector3D v returns Vector3D
        local Vector3D r = Vector3D.allocate()
        set r.x = .x-v.x
        set r.y = .y-v.y
        set r.y = .z-v.z
        return r
    endmethod
    
    method addXYZ takes real x, real y, real z returns Vector3D
        local Vector3D r = Vector3D.allocate()
        set r.x = .x+x
        set r.y = .y+y
        set r.z = .z+z
        return r
    endmethod
    
    method subXYZ takes real x, real y, real z returns Vector3D
        local Vector3D r = Vector3D.allocate()
        set r.x = .x-x
        set r.y = .y-y
        set r.z = .z-z
        return r
    endmethod
    
    method mUnit takes nothing returns Vector3D
        local real m = .getMag()
        set .x = .x/m
        set .y = .y/m
        set .z = .z/m
        return this
    endmethod
    
    method mInv takes nothing returns Vector3D
        set .x = -.x
        set .y = -.y
        set .z = -.z
        return this
    endmethod
    
    method mScale takes real s returns Vector3D
        set .x = s*.x
        set .y = s*.y
        set .z = s*.z
        return this
    endmethod
    
    method mAdd takes Vector3D v returns Vector3D
        set .x = .x+v.x
        set .y = .y+v.y
        set .z = .z+v.z
        return this
    endmethod
    
    method mSub takes Vector3D v returns Vector3D
        set .x = .x-v.x
        set .y = .y-v.y
        set .z = .z-v.z
        return this
    endmethod
    
    method mAddXYZ takes real x, real y, real z returns Vector3D
        set .x = .x+x
        set .y = .y+y
        set .z = .z+z
        return this
    endmethod
    
    method mSubXYZ takes real x, real y, real z returns Vector3D
        set .x = .x-x
        set .y = .y-y
        set .z = .z-z
        return this
    endmethod
    
    method to2D takes nothing returns Vector2D
        return Vector2D.from3D(this)
    endmethod
endstruct

endlibrary


Original post:

Greetings,

Since I started working with vJass, and I found out I needed vector functionality, I decided to make a 2D vector library of my own.

JASS:
library Vectors2D

//******************************************************************************************************************************************************
//* Vectors2D
//*
//*  ABOUT
//*   Author:   krainert
//*   Version:  1.0
//*   Release:  2011-07-08
//*
//*  DESCRIPTION
//*   This library provides the vector class as described in this documentation
//*
//*  NOTES
//*   All angles are in radians
//*
//*  STATIC METHODS
//*   static vector create(real x, real y)  - instantiates a new vector with x coordinate x and y coordinate y
//*   static vector fromDM(real d, real m)  - instantiates a new vector with direction d and magnitude m
//*
//*  PROPERTIES
//*   real x                                - the x coordinate of the vector
//*   real y                                - the y coordinate of the vector
//*
//*  METHODS
//*   real   getDir()                       - returns the direction of the vector
//*   real   getMag()                       - returns the magnitude of the vector
//*   vector clone()                        - returns a cloned vector
//*   vector inv()                          - returns an inverted cloned vector
//*   vector scale(real s)                  - returns a cloned vector scaled by s
//*   vector add(vector v)                  - returns a vector equal to the sum of the vector and v
//*   vector sub(vector v)                  - returns a vector equal to the difference between the vector and v
//*   vector addXY(real x, real y)          - returns a vector equal to the sum of the vector and a vector with x coordinate x and y coordinate y
//*   vector subXY(real x, real y)          - returns a vector equal to the difference between the vector and a vector x coordinate x and y coordinate y
//*   vector addDM(vector v)                - returns a vector equal to the sum of the vector and a vector with direction d and magnitude m
//*   vector subDM(vector v)                - returns a vector equal to the difference between the vector and a vector with direction d and magnitude m
//*   vector setDir(real d)                 - returns a cloned vector with direction d
//*   vector setMag(real m)                 - returns a cloned vector with magnitude m
//*   vector mInv()                         - inverts the vector and returns the resulting vector
//*   vector mScale(real s)                 - scales the vector by s and returns the resulting vector
//*   vector mAdd(vector v)                 - adds v to the vector and returns the resulting vector
//*   vector mSub(vector v)                 - subtracts v from the vector and returns the resulting vector
//*   vector mAddXY(real x, real y)         - adds a vector with x coordinate x and y coordinate y from the vector and returns the resulting vector
//*   vector mSubXY(real x, real y)         - subtracts a vector with x coordinate x and y coordinate y from the vector and returns the resulting vector
//*   vector mAddDM(vector v)               - adds a vector with direction d and magnitude m to the vector and returns the resulting vector
//*   vector mSubDM(vector v)               - subtracts a vector with direction d and magnitude m from the vector and returns the resulting vector
//*   vector mSetDir(real d)                - sets the direction of the vector to d and returns the resulting vector
//*   vector mSetMag(real m)                - sets the magnitude of the vector to m and returns the resulting vector
//*   void   destroy()                      - destroys the vector
//*
//******************************************************************************************************************************************************

globals
    private constant integer MAX_VECTORS = 8191
endglobals

struct vector[MAX_VECTORS]
    real x
    real y
    
    static method create takes real x, real y returns vector
        local vector r = vector.allocate()
        set r.x = x
        set r.y = y
        return r
    endmethod
    
    static method fromDM takes real d, real m returns vector
        local vector r = vector.allocate()
        set r.x = Cos(d)*m
        set r.y = Sin(d)*m
        return r
    endmethod
    
    method getDir takes nothing returns real
        return Atan2(.y, .x)
    endmethod
    
    method getMag takes nothing returns real
        return SquareRoot(Pow(.x, 2)+Pow(.y, 2))
    endmethod
    
    method clone takes nothing returns vector
        local vector r = vector.allocate()
        set r.x = .x
        set r.y = .y
        return r
    endmethod
    
    method invert takes nothing returns vector
        local vector r = vector.allocate()
        set r.x = -.x
        set r.y = -.y
        return r
    endmethod
    
    method scale takes real s returns vector
        local vector r = vector.allocate()
        set r.x = s*.x
        set r.y = s*.y
        return r
    endmethod
    
    method add takes vector v returns vector
        local vector r = vector.allocate()
        set r.x = .x+v.x
        set r.y = .y+v.y
        return r
    endmethod
    
    method sub takes vector v returns vector
        local vector r = vector.allocate()
        set r.x = .x-v.x
        set r.y = .y-v.y
        return r
    endmethod
    
    method addXY takes real x, real y returns vector
        local vector r = vector.allocate()
        set r.x = .x+x
        set r.y = .y+y
        return r
    endmethod
    
    method subXY takes real x, real y returns vector
        local vector r = vector.allocate()
        set r.x = .x-x
        set r.y = .y-y
        return r
    endmethod
    
    method addDM takes real d, real m returns vector
        local vector r = vector.allocate()
        set r.x = .x+Cos(d)*m
        set r.y = .y+Sin(d)*m
        return r
    endmethod
    
    method subDM takes real d, real m returns vector
        local vector r = vector.allocate()
        set r.x = .x-Cos(d)*m
        set r.y = .y-Sin(d)*m
        return r
    endmethod
    
    method setDir takes real d returns vector
        local real m = .getMag()
        local vector r = vector.allocate()
        set r.x = Cos(d)*m
        set r.y = Sin(d)*m
        return r
    endmethod
    
    method setMag takes real m returns vector
        local real d = .getDir()
        local vector r = vector.allocate()
        set r.x = Cos(d)*m
        set r.y = Sin(d)*m
        return r
    endmethod
    
    method mInv takes nothing returns vector
        set .x = -.x
        set .y = -.y
        return this
    endmethod
    
    method mScale takes real s returns vector
        set .x = s*.x
        set .y = s*.y
        return this
    endmethod
    
    method mAdd takes vector v returns vector
        set .x = .x+v.x
        set .y = .y+v.y
        return this
    endmethod
    
    method mSub takes vector v returns vector
        set .x = .x-v.x
        set .y = .y-v.y
        return this
    endmethod
    
    method mAddXY takes real x, real y returns vector
        set .x = .x+x
        set .y = .y+y
        return this
    endmethod
    
    method mSubXY takes real x, real y returns vector
        set .x = .x-x
        set .y = .y-y
        return this
    endmethod
    
    method mAddDM takes real d, real m returns vector
        set .x = .x+Cos(d)*m
        set .y = .y+Sin(d)*m
        return this
    endmethod
    
    method mSubDM takes real d, real m returns vector
        set .x = .x-Cos(d)*m
        set .y = .y-Sin(d)*m
        return this
    endmethod
    
    method mSetDir takes real d returns vector
        local real m = .getMag()
        set .x = Cos(d)*m
        set .y = Sin(d)*m
        return this
    endmethod
    
    method mSetMag takes real m returns vector
        local real d = .getDir()
        set .x = Cos(d)*m
        set .y = Sin(d)*m
        return this
    endmethod
endstruct

endlibrary


There's probably a bunch of those already, but I needed the training anyway.

How does it look? Is it decently coded? Does it have leaks?
 

Dirac

22710180
Reaction score
147
Well, leaks?: no, you're only working with reals.
I think these kind of things are called "snippets".
Also use
JASS:
 tags for us to read it propperly.
And this should be submitted in the Tutorials and Resources area

Thanks for your work, i'm sure someone might find it helpful
 

Romek

Super Moderator
Reaction score
963
> [ljass]return SquareRoot(Pow(.x, 2)+Pow(.y, 2))[/ljass]
[ljass]return SquareRoot(.x*.x + .y*.y)[/ljass] is faster.

You're also missing a few standard vector functions. Why not make this support 3D vectors too? ;)
 

Sevion

The DIY Ninja
Reaction score
413
> [ljass]return SquareRoot(Pow(.x, 2)+Pow(.y, 2))[/ljass]
[ljass]return SquareRoot(.x*.x + .y*.y)[/ljass] is faster.

Actually, there's evidence that Pow for anything less than 3 is more efficient.

I'm not sure if I can still dig up this thread though. It seems as if with that last patch they made Pow better.

I was actually working on a Vector Math library for Galaxy; however, the limited structs screwed it over and it isn't even possible to do anymore.
 

tooltiperror

Super Moderator
Reaction score
231
I thought a vector only had an angle and a distance, not an X,Y?
 

Sevion

The DIY Ninja
Reaction score
413
The x,y defines the origin I believe. I find it strange how the vector struct doesn't include members to store angle and magnitude?
 

tooltiperror

Super Moderator
Reaction score
231
I thought the point of vectors was that they always started at the origin (0,0).
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Actually, there's evidence that Pow for anything less than 3 is more efficient.

I'm not sure if I can still dig up this thread though. It seems as if with that last patch they made Pow better.

I seem to remember that it was something like more than 3 was more efficient with Pow, not the other way around. It is, after all, the overhead of a function call.

I thought a vector only had an angle and a distance, not an X,Y?

the distance between the origin and x,y is the length/distance/magnituded, and the angle between the origin and x,y is its angle.

@krainert

It could be improved with overriding the [] and []= operator, where 0 and 1 refer to x and y. Also, I think that the struct name should be changed from 'vector' to 'Vector2d' or '2dVector', or something similar. Capital struct names is a common standard, and since it doesn't support 3d vectors, it makes sense to separate it this way, in case someone has a 3d vector library in their map. While you're at it, why not add a Vector3d struct? :p I'm not sure how I would 'support' 3d vectors any other way.
 

Dirac

22710180
Reaction score
147
Actually, there's evidence that Pow for anything less than 3 is more efficient.
Please clarify this, and if true then is it safe to use Pow(GetUnitX(u),2)? or is it still best to store it inside a local
 

Sevion

The DIY Ninja
Reaction score
413
I seem to remember that it was something like more than 3 was more efficient with Pow, not the other way around. It is, after all, the overhead of a function call.

Ah, yes. You're right. I knew there was something like this >_< :p
 

krainert

Member
Reaction score
10
Thanks for all the great feedback. I'm currently trying to decide whether to build a system capable of handling vectors of arbitrary dimensions or add 3D vectors to the current 2D system. Honestly I don't think the potential comfort of having support for arbitrary vectors in the same system used for 2D/3D vectors outweighs the loss of intuition and directness in simply employing the structs Vector2D and Vector3D and thereby restricting the library to 2D/3D functionality. Also by working with two strictly defined structs I can restrict usage to avoid confusion (for example, I can ensure that only vectors in three dimensions can be added to other 3D vectors). Thoughts?
Also: What functionality am I currently missing for 2D vectors?
Also also: 3D vectors should be definable by two direction angles and a magnitude scale (where 2D vectors can be defined by one direction angle and a magnitude scale). What are the two direction angles called, if they have any common names in math? Yeah, I'm a bit rusty...

EDIT: By the way, I put the thread in JASS Help because I'm still new to vJass, and hence the library might need corrections or more work (of which at least the latter turned out to be the case). Particularly I figured I wouldn't post it in any way indicating that it was finished when it was the first version of the first script I wrote :)
 

krainert

Member
Reaction score
10
Anyone?
EDIT: Never mind, I'll put in the functionality I imagine people would find useful and update later of need be.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top