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:
Original post:
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.
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?
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?