|
以下是我写得向量与复数函数
自我感觉已经很完备了
大家看看有没有需要补充的地方
[jass]
// #######################################################
// ## Vector & Complex Function ##########################
// #######################################################
//
// Name: 向量与复数函数
// Author: zyl910
// Version: V1.0.0
// Updata: 2006-4-14
// 设置位置X
function LocationSetX takes location loc, real x returns nothing
call MoveLocation(loc, x, GetLocationY(loc))
endfunction
// 设置位置Y
function LocationSetY takes location loc, real y returns nothing
call MoveLocation(loc, GetLocationX(loc), y)
endfunction
// 复制location
function LocationCopy takes location lhs, location rhs returns nothing
call MoveLocation(lhs, GetLocationX(rhs), GetLocationY(rhs))
endfunction
// 克隆location。创建一个与原来的一样的location
function LocationClone takes location rhs returns location
return Location(GetLocationX(rhs), GetLocationY(rhs))
endfunction
// ## Vector Function ##############################
// 向量函数
//
// 向量相加
function VectorAdd takes location lhs, location rhs returns nothing
call MoveLocation(lhs, GetLocationX(lhs) + GetLocationX(rhs), GetLocationY(lhs) + GetLocationY(rhs))
endfunction
// 向量相减
function VectorSub takes location lhs, location rhs returns nothing
call MoveLocation(lhs, GetLocationX(lhs) - GetLocationX(rhs), GetLocationY(lhs) - GetLocationY(rhs))
endfunction
// 向量数乘
function VectorMulReal takes location lhs, real rhs returns nothing
call MoveLocation(lhs, GetLocationX(lhs)*rhs, GetLocationY(lhs)*rhs)
endfunction
// 向量点积
function VectorGetNumMul takes location lhs, location rhs returns real
return GetLocationX(lhs)*GetLocationX(rhs) + GetLocationY(lhs)*GetLocationY(rhs))
endfunction
// 向量点积3D
function VectorGetNumMul3D takes location lhs, location rhs returns real
return GetLocationX(lhs)*GetLocationX(rhs) + GetLocationY(lhs)*GetLocationY(rhs)) + GetLocationZ(lhs)*GetLocationZ(rhs))
endfunction
// 取得向量的长度(向量的模)
function VectorGetLength takes location rhs returns real
local real x = GetLocationX(rhs)
local real y = GetLocationY(rhs)
return SquareRoot(x*x + y*y)
endfunction
// 取得向量的长度(向量的模)3D
function VectorGetLength3D takes location rhs returns real
local real x = GetLocationX(rhs)
local real y = GetLocationY(rhs)
local real z = GetLocationZ(rhs)
return SquareRoot(x*x + y*y + z*z)
endfunction
// [弧度]取得向量的角度
function VectorGetAngle takes location rhs returns real
return Atan2(GetLocationY(rhs), GetLocationX(rhs))
endfunction
// [角度]取得向量的角度
function VectorGetAngleBJ takes location rhs returns real
return Rad2Deg(Atan2(GetLocationY(rhs), GetLocationX(rhs)))
endfunction
// 化为单位向量
function VectorSetStandard takes location v returns nothing
local real x = GetLocationX(v)
local real y = GetLocationY(v)
local real n = SquareRoot(x*x + y*y)
call MoveLocation(v, x / n, y / n)
endfunction
// [弧度]设置向量的数据(方向和长度)
function VectorSetData takes location lhs, real angle, real dist returns nothing
call MoveLocation(lhs, Cos(angle)*dist, Sin(angle)*dist)
endfunction
// [角度]设置向量的数据(方向和长度)
function VectorSetDataBJ takes location lhs, real angle, real dist returns nothing
call VectorSetData(lhs, Deg2Rad(angle), dist)
endfunction
// [弧度]设置向量的数据加强版(方向、长度、垂直方向长度)
function VectorSetDataEx takes location lhs, real angle, real distX, real distY returns nothing
local real fSin = Sin(angle)
local real fCos = Cos(angle)
call MoveLocation(lhs, fCos * distX - fSin * distY, fSin * distX + fCos * distY)
endfunction
// [角度]设置向量的数据加强版(方向、长度、垂直方向长度)
function VectorSetDataExBJ takes location lhs, real angle, real distX, real distY returns nothing
call VectorSetDataEx(lhs, Deg2Rad(angle), distX, distY)
endfunction
// 设置向量的长度(向量的模)
function VectorSetLength takes location lhs, real rhs returns nothing
call VectorMulReal(lhs, rhs / VectorGetLength(lhs))
endfunction
// [弧度]设置向量的角度
function VectorSetAngle takes location lhs, real rhs returns nothing
call VectorSetData(lhs, rhs, VectorGetLength(lhs))
endfunction
// [角度]设置向量的角度
function VectorSetAngleBJ takes location lhs, real rhs returns nothing
call VectorSetData(lhs, Deg2Rad(rhs), VectorGetLength(lhs))
endfunction
// [弧度]向量旋转
function VectorRotate takes location v, real angle returns nothing
local real fSin = Sin(angle)
local real fCos = Cos(angle)
local real x = GetLocationX(v)
local real y = GetLocationY(v)
call MoveLocation(v, fCos * x - fSin * y, fSin * x + fCos * y)
endfunction
// [角度]向量旋转
function VectorRotateBJ takes location v, real angle returns nothing
call VectorRotate(v, Deg2Rad(angle))
endfunction
// ## Complex Function ##############################
// 复数函数
//
// X: Real(实部)
// Y: Imag(虚部)
// 设置复数数据(实部和虚部)
function ComplexSetData takes location z, real fReal, real fImag returns nothing
call MoveLocation(z, fReal, fImag)
endfunction
// 取得复数实部
function ComplexGetReal takes location z returns real
return GetLocationX(z)
endfunction
// 取得复数虚部
function ComplexGetImag takes location z returns real
return GetLocationY(z)
endfunction
// 设置复数实部
function ComplexSetReal takes location z, real fReal returns nothing
call MoveLocation(z, fReal, GetLocationY(z))
endfunction
// 设置复数虚部
function ComplexSetImag takes location z, real fImag returns nothing
call MoveLocation(z, GetLocationX(z), fImag)
endfunction
// 化为共轭复数
function ComplexConjugate takes location z returns nothing
call MoveLocation(z, GetLocationX(z), -GetLocationY(z))
endfunction
// 复数加上实数
function ComplexAddReal takes location lhs, real rhs returns nothing
call MoveLocation(lhs, GetLocationX(lhs) + rhs, GetLocationY(lhs))
endfunction
// 复数乘以实数
function ComplexMulReal takes location lhs, real rhs returns nothing
call MoveLocation(lhs, GetLocationX(lhs) * rhs, GetLocationY(lhs) * rhs)
endfunction
// 复数加法
function ComplexAdd takes location lhs, location rhs returns nothing
call MoveLocation(lhs, GetLocationX(lhs) + GetLocationX(rhs), GetLocationY(lhs) + GetLocationY(rhs))
endfunction
// 复数减法
function ComplexSub takes location lhs, location rhs returns nothing
call MoveLocation(lhs, GetLocationX(lhs) - GetLocationX(rhs), GetLocationY(lhs) - GetLocationY(rhs))
endfunction
// 复数乘法
function ComplexMul takes location lhs, location rhs returns nothing
local real x1 = GetLocationX(lhs)
local real y1 = GetLocationY(lhs)
local real x2 = GetLocationX(rhs)
local real y2 = GetLocationY(rhs)
call MoveLocation(lhs, x1*x2 - y1*y2, x1*y2 + x2*y1)
endfunction
// 复数除法
function ComplexDiv takes location lhs, location rhs returns nothing
local real x1 = GetLocationX(lhs)
local real y1 = GetLocationY(lhs)
local real x2 = GetLocationX(rhs)
local real y2 = GetLocationY(rhs)
local real n = x2*x2 + y2*y2
call MoveLocation(lhs, (x1*x2 + y1*y2) / n, (x2*y1 - x1*y2) / n)
endfunction
// 复数求幂
function ComplexPowReal takes location lhs, real rhs returns nothing
local real x = GetLocationX(lhs)
local real y = GetLocationY(lhs)
local real r = Pow(SquareRoot(x*x + y*y), rhs)
local real th = Atan2(y, x) * rhs
call MoveLocation(lhs, r * Cos(th), r * Sin(th))
endfunction
[/jass]
PS:
我觉得阅读数学、物理、科普方面的书 能提高思维能力,特别对于编程,就算是对Jass这种脚本语言也是很重要的
以下是我珍藏的几个网站:
大家如果知道这方面的优秀网站也不妨介绍给我 |
|