找回密码
 点一下
查看: 3864|回复: 19

[代码]向量与复数函数

[复制链接]
发表于 2006-4-14 23:50:39 | 显示全部楼层 |阅读模式
以下是我写得向量与复数函数
自我感觉已经很完备了
大家看看有没有需要补充的地方

[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这种脚本语言也是很重要的

以下是我珍藏的几个网站:
数学中国
http://www.madio.net/
讨论数学与算法的专业网站


西安信息资源网
http://www.infoxa.com/index.htm
有许多编程方面的电子书


三思
http://www.oursci.org/index.html
不错的科普网站


大家如果知道这方面的优秀网站也不妨介绍给我
发表于 2006-4-15 00:34:00 | 显示全部楼层
嗯嗯。主要收获是找到几个好的网站。
楼主说的内容。完全都不明白。。。
回复

使用道具 举报

发表于 2006-4-15 13:25:34 | 显示全部楼层
不错不错 哈哈~这东西以后我可能会用到 ~~省得我自己去写了。。

向量里还差个叉乘吧~~不过叉乘写起来要麻烦一些。。。

[ 本帖最后由 zyl910 于 2006-4-15 13:36 编辑 ]
回复

使用道具 举报

 楼主| 发表于 2006-4-15 13:37:01 | 显示全部楼层
原帖由 illlusion 于 2006-4-15 13:25 发表
不错不错 哈哈~这东西以后我可能会用到 ~~省得我自己去写了。。

向量里还差个叉乘吧~~不过叉乘写起来要麻烦一些。。。



只有三维向量才有叉乘,且返回值是一个三维向量

可惜不能修改location的Z分量啊


PS:又点错编辑了:L
回复

使用道具 举报

发表于 2006-4-15 13:39:58 | 显示全部楼层
最后那个科普网站里竟然有费曼讲义 但很遗憾是中文的翻译实在太烂了 例如:
原文: love is not a science
翻译:爱好就不是科学
这有什么好含蓄地要这样翻译。。。
回复

使用道具 举报

发表于 2006-4-15 13:41:26 | 显示全部楼层
原帖由 zyl910 于 2006-4-15 13:37 发表



只有三维向量才有叉乘,且返回值是一个三维向量

可惜不能修改location的Z分量啊


PS:又点错编辑了:L


哦 我后面看到你用到z分量 就以为可以有叉乘了 原来z不能改阿~~~
回复

使用道具 举报

 楼主| 发表于 2006-4-15 13:43:05 | 显示全部楼层
原帖由 illlusion 于 2006-4-15 13:39 发表
最后那个科普网站里竟然有费曼讲义 但很遗憾是中文的翻译实在太烂了 例如:
原文: love is not a science
翻译:爱好就不是科学
这有什么好含蓄地要这样翻译。。。


我英文菜:L
回复

使用道具 举报

发表于 2006-4-15 14:11:28 | 显示全部楼层
估计它就会登前面几章 共三卷 每卷约40章
后面的登出来就不是科普了~~登出来也没人看 ~~看了也不会有几个人看得懂。。。这世道阿。。。
回复

使用道具 举报

 楼主| 发表于 2006-4-15 14:13:40 | 显示全部楼层
你知道有什么好的科技研究网站吗?

我要的不是科普
我要的是专业

[ 本帖最后由 zyl910 于 2006-4-15 14:14 编辑 ]
回复

使用道具 举报

发表于 2006-4-15 14:20:09 | 显示全部楼层
这个貌似是外国某大学物理系的http://aesop.phys.utk.edu/

这方面最系统最专业的估计还是只有书本里有 网站上的感觉都太零碎。。。
回复

使用道具 举报

 楼主| 发表于 2006-4-15 14:22:40 | 显示全部楼层

我英文菜

网络上就是信息垃圾多
科学技术资料反而很零碎
回复

使用道具 举报

发表于 2006-4-15 14:29:03 | 显示全部楼层
。。。要找到这方面中文的好网站简直不太可能

[ 本帖最后由 illlusion 于 2006-5-3 01:09 编辑 ]
回复

使用道具 举报

发表于 2006-4-16 11:10:20 | 显示全部楼层
嗯,嗯..有向量的就方便多了...我以前也写过一个,不过是用LOCALVARS传数据的..结果现在找不到了
回复

使用道具 举报

发表于 2006-4-24 01:37:22 | 显示全部楼层
原帖于 2006-4-23 22:33 发表
// [弧度]取得向量的角度
function VectorGetAngle takes location rhs returns real
    return Atan2(GetLocationY(rhs), GetLocationX(rhs))
endfunction


刚才发现这里最好加个return bj_RADTODEG * Atan2(GetLocationY(rhs), GetLocationX(rhs))
因为Atan2返回的是弧度制的 而we基本用到的都是角度 所以转化一下..
回复

使用道具 举报

发表于 2006-4-24 03:23:20 | 显示全部楼层
Z轴可以变相的来改
创建一个可破坏物(x,y,z),
再创建点(x,y)
这个点就是x,y,z+可破坏物高度了,常用的不可见平台小,高度1.948
回复

使用道具 举报

 楼主| 发表于 2006-4-24 11:56:19 | 显示全部楼层
原帖由 illlusion 于 2006-4-24 01:37 发表
[quote]
原帖于 2006-4-23 22:33 发表
// [弧度]取得向量的角度
function VectorGetAngle takes location rhs returns real
    return Atan2(GetLocationY(rhs), GetLocationX(rhs))
endfunction


刚才发现这里最好加个return bj_RADTODEG * Atan2(GetLocationY(rhs), GetLocationX(rhs))
因为Atan2返回的是弧度制的 而we基本用到的都是角度 所以转化一下.. [/quote]

注意看下面那个带BJ的函数:

  1. // [弧度]取得向量的角度
  2. function VectorGetAngle takes location rhs returns real
  3.     return Atan2(GetLocationY(rhs), GetLocationX(rhs))
  4. endfunction

  5. // [角度]取得向量的角度
  6. function VectorGetAngleBJ takes location rhs returns real
  7.     return Rad2Deg(Atan2(GetLocationY(rhs), GetLocationX(rhs)))
  8. endfunction

复制代码


只有BJ函数才用角度
数学标准是弧度制
回复

使用道具 举报

 楼主| 发表于 2006-4-24 11:56:51 | 显示全部楼层
原帖由 Red_Wolf 于 2006-4-24 03:23 发表
Z轴可以变相的来改
创建一个可破坏物(x,y,z),
再创建点(x,y)
这个点就是x,y,z+可破坏物高度了,常用的不可见平台小,高度1.948



运算效率........
回复

使用道具 举报

发表于 2006-4-24 16:34:23 | 显示全部楼层
原帖由 zyl910 于 2006-4-24 11:56 发表


刚才发现这里最好加个return bj_RADTODEG * Atan2(GetLocationY(rhs), GetLocationX(rhs))
因为Atan2返回的是弧度制的 而we基本用到的都是角度 所以转化一下..

注意看下面那个带BJ的函数:

// 取 ...


我知道阿 ~但we里面基本都是用的角度阿..
回复

使用道具 举报

 楼主| 发表于 2006-4-24 18:18:14 | 显示全部楼层
数学标准是弧度制
如果我写Jass绝对用弧度
回复

使用道具 举报

发表于 2006-4-25 09:10:53 | 显示全部楼层
这个系统很有价值呢~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 点一下

本版积分规则

Archiver|移动端|小黑屋|地精研究院

GMT+8, 2024-5-2 14:38 , Processed in 0.065329 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表