Recently I started working with C#, and I noticed, that a code I write with about 10 rows in JASS can be written with 1 row in C#, and I figured, that the usage of Vectors was allowing me to do that, so I made a little library for Vectors.
Today I searched for such library, and found another one, but it uses 3d vectors, which I find to be unneeded for most purposes in Warcraft, so I'll just post what I've done
Today I searched for such library, and found another one, but it uses 3d vectors, which I find to be unneeded for most purposes in Warcraft, so I'll just post what I've done
JASS:
library Points
struct Point
real x
real y
static method create takes real x, real y returns thistype
local thistype this = thistype.allocate()
set this.x = x
set this.y = y
return this
endmethod
method move takes real x, real y returns nothing
set this.x = x
set this.y = y
endmethod
static method distance takes Point pt1, Point pt2 returns real
local real w = pt2.x - pt1.x
local real h = pt2.y - pt1.y
if w != 0 then
if w > 0 then
return w/Cos(Atan2(h, w))
else
return -w/Cos(Atan2(h, w))
endif
elseif h != 0 then
if h > 0 then
return h/Sin(Atan2(h, w))
else
return -h/Sin(Atan2(h, w))
endif
else
return 0.
endif
endmethod
static method angle takes Point pt1, Point pt2 returns real
return Atan2(pt2.y - pt1.y, pt2.x - pt1.x)
endmethod
method moveUnit takes unit u returns nothing
call SetUnitX(u, this.x)
call SetUnitY(u, this.y)
endmethod
method moveUnit2 takes unit u returns nothing
call SetUnitPosition(u, .x, .y)
endmethod
method moveByVector takes Vector v returns nothing
set this.x = this.x + v.x
set this.y = this.y + v.y
endmethod
static method polarOffset takes Point p, real d, real a returns Point
local Point p2 = Point.allocate()
set p2.x = p.x + d*Cos(a)
set p2.y = p.y + d*Sin(a)
return p2
endmethod
static method withOffset takes Point p, real x, real y returns Point
local Point p2 = Point.allocate()
set p2.x = p.x + x
set p2.y = p.y + y
return p2
endmethod
endstruct
endlibrary
library Vectors
struct Vector
real x
real y
static method create takes real x, real y returns thistype
local thistype this = thistype.allocate()
set this.x = x
set this.y = y
return this
endmethod
static method create2 takes real a, real l returns thistype
local thistype this = thistype.allocate()
set this.x = l*Cos(a)
set this.y = l*Sin(a)
return this
endmethod
method getLength takes nothing returns real
return SquareRoot(.x*.x + .y*.y)
endmethod
method getAngle takes nothing returns real
return Atan2(y, x)
endmethod
method getSquaredLength takes nothing returns real
return .x*.x + .y*.y
endmethod
static method betweenPoints takes Point pt1, Point pt2 returns thistype
return thistype.create(pt2.x - pt1.x, pt2.y - pt1.y)
endmethod
static method getNormalized takes real x, real y returns thistype
local thistype this = thistype.allocate()
local real a = Atan2(y, x)
set this.y = Sin(a)
set this.x = Cos(a)
return this
endmethod
method normalize takes nothing returns nothing
local real a = Atan2(.y, .x)
set .x = Cos(a)
set .y = Sin(a)
endmethod
static method fromAngle takes real a returns thistype
local thistype this = thistype.allocate()
set this.x = Cos(a)
set this.y = Sin(a)
return this
endmethod
method replace takes real x, real y returns nothing
set this.x = x
set this.y = y
endmethod
method replace2 takes real a, real l returns nothing
set this.x = l*Cos(a)
set this.y = l*Sin(a)
endmethod
method rotate takes real a returns nothing
local real l = SquareRoot(.x*.x + .y*.y)
set a = Atan2(.y, .x) + a
set .x = l*Cos(a)
set .y = l*Sin(a)
endmethod
method scale takes real s returns nothing
set this.x = this.x*s
set this.y = this.y*s
endmethod
method moveUnit takes unit u returns nothing
call SetUnitX(u, GetUnitX(u) + this.x)
call SetUnitY(u, GetUnitY(u) + this.y)
endmethod
method moveUnitBy takes unit u, real s returns nothing
local real a = Atan2(.y, .x)
call SetUnitX(u, GetUnitX(u) + s*Cos(a))
call SetUnitY(u, GetUnitY(u) + s*Sin(a))
endmethod
method sum takes thistype v returns nothing
set this.x = v.x + this.x
set this.y = v.y + this.y
endmethod
method add takes real w, real h returns nothing
set .x = .x + w
set .y = .y + h
endmethod
method difference takes thistype v returns nothing
set .x = .x - v.x
set .y = .y - v.y
endmethod
method subtract takes real w, real h returns nothing
set .x = .x - w
set .y = .y - h
endmethod
static method dotProduct takes thistype v1, thistype v2 returns real
return v1.x*v2.x + v1.y*v2.y
endmethod
method setLength takes real l returns nothing
set l = l/SquareRoot(.x*.x + .y*.y)
set .x = .x*l
set .y = .y*l
endmethod
method setAngle takes real a returns nothing
local real l = SquareRoot(.x*.x + .y*.y)
set .x = l*Cos(a)
set .y = l*Sin(a)
endmethod
endstruct
endlibrary
Last edited: