VEX —— Functions|Math

28 篇文章 3 订阅

目录

sign —— 返回给定数的符号标签

abs —— 返回绝对值

avg —— 返回平均值

sum —— 求和

max —— 返回最大值

min —— 返回最小值

rint —— 返回四舍五入后的整数

ceil —— 返回最近的最大整数

floor —— 返回最近的最小整数

frac —— 返回浮点值的小数部分

trunc —— 返回移除浮点值的小数部分

pow —— 返回给定数指定指数

sqrt —— 返回平方根

cbrt —— 返回立方根

exp —— 返回E指数

log —— 返回自然数E的对数

log10 —— 返回10的对数

length —— 计算vector的长度大小

length2 —— 计算vector/vector4的长度平方

normalize —— 标准化矢量

dot —— 点乘(内积)

cross —— 叉乘(外积)


sin —— 返回正弦值

cos —— 返回余弦值

tan —— 返回正切值

asin —— 返回反正弦

acos —— 返回反余弦

atan —— 返回反正切

atan2 —— 返回y/x反正切

sinh —— 返回双曲线正弦

cosh —— 返回双曲线余弦

tanh —— 返回双曲线正切


shl —— 对整数向左移动指定位

shr —— 对整数向右移动指定位

shrz —— 对整数向右移动指定位


resample_linear —— 根据线性插值返回新数组

spline —— 沿polyline、spline采样值

spline_cdf —— 通过采样spline曲线生成CDF

kspline —— 返回沿着曲线的插值

isfinite —— 检测值是否为正常的有限值

isnan —— 检测值是否为数字


Du —— 返回给定值的导数(相对于U)

Dv —— 返回给定值的导数(相对于V)

Dw —— 返回给定值的导数(相对于W)

erf —— 高斯误差函数

erf_inv —— 反向高斯误差函数

erfc —— 高斯误差函数补数


makebasis —— 对给定的Z轴创建正交基准

predicate_incircle —— 确定点是在圆环位置

predicate_insphere —— 确定点是在圆位置

predicate_orient2d —— 确定点方向是在线上

predicate_orient3d —— 确定点方向是在平面上

product —— 返回数字列表的乘积

ptlined —— 返回点到线段的最近距离

sliderframe —— 应用两矢量的最小旋转

solvecubic —— 解算立方函数

solvequadratic —— 解算平方函数

solvepoly —— 解算函数

solvetriangleSSS —— 从边查找三角形角度


quaternion —— 创建四元数

qdistance —— 返回两四元数间的距离

qinvert —— 反转四元数旋转

qmultiply —— 两四元数相乘

qrotate —— 使用四元数旋转矢量


ident —— 返回单位矩阵

invert —— 逆矩阵

determinant —— 计算矩阵的行列式

transpose —— 翻转矩阵

premul —— 预乘矩阵

outerproduct —— 返回外积

diagonalizesymmetric —— 对称矩阵的对角化

eigenvalues —— 计算3*3矩阵的特征值

svddecomp —— 计算3*3矩阵的奇异值分解


planesphereintersect —— 计算3D球与3D平面的相交

combinelocaltransform —— 合并local和parent变换

extractlocaltransform —— 从world变换中提取local变换


sign —— 返回给定数的符号标签

  • 负数-1,零0,整数1;
int sign(int n)
float sign(float n)
vector2 sign(vector2 v)
vector sign(vector v)
vector4 sign(vector4 v)

abs —— 返回绝对值

int abs(int n)
float abs(float n)
<vector> abs(<vector>v)

avg —— 返回平均值

//返回a
int avg(int a)
float avg(float a)
float avg(float a, float b, ...)
//返回分量中的平均值
float avg(vector2 v)
float avg(vector v)
float avg(vector4 v)
vector2 avg(vector2 a, vector2 b, ...)
vector avg(vector a, vector b, ...)
vector4 avg(vector4 a, vector4 b, ...)
<type> avg(<type>arr[])

sum —— 求和

float sum(float n)
int sum(int n)
float sum(<vector>v)
int sum(int nums[])
float sum(float nums[])
<vector> sum(<vector>arr[])

max —— 返回最大值

int max(int value1, int value2, ...)
float max(float value1, float value2, ...)
<vector> max(<vector>value1, <vector>value2, ...)
<type> max(<type>values[])
float max(<vector>values)
<type> max(<type>value)

min —— 返回最小值

int min(int value1, int value2, ...)
float min(float value1, float value2, ...)
<vector> min(<vector>value1, <vector>value2, ...)
<type> min(<type>values[])
float min(<vector>v)
<type> min(<type>value)

rint —— 返回四舍五入后的整数

float rint(float n)
<vector> rint(<vector>v)

ceil —— 返回最近的最大整数

float ceil(float n)
<vector> ceil(<vector>v)

floor —— 返回最近的最小整数

float|int floor(float n)
<vector> floor(<vector>v)

frac —— 返回浮点值的小数部分

float frac(float n)
<vector> frac(<vector>v)

trunc —— 返回移除浮点值的小数部分

//如参数位负值,返回ceil(x),否则返回floor(x)
float trunc(float x)
vector2 trunc(vector2 x)
vector trunc(vector x)
vector4 trunc(vector4 x)

pow —— 返回给定数指定指数

float pow(float n, float exponent)
<vector> pow(<vector>v, float exponent)

sqrt —— 返回平方根

float sqrt(float value)
<vector> sqrt(<vector>value)
  • 负数的平方根为0;

cbrt —— 返回立方根

float cbrt(float n)
vector2 cbrt(vector2 v)
vector cbrt(vector v)
vector4 cbrt(vector4 v)

exp —— 返回E指数

float exp(float n)
<vector> exp(<vector>n)

log —— 返回自然数E的对数

float log(float n)
<vector> log(<vector>v)

log10 —— 返回10的对数

float log10(float n)
<vector> log10(<vector>n)

length —— 计算vector的长度大小

  • 计算字符串长度或数组个数,使用len
float length(float f)
float length(vector2 v)
float length(vector v)
float length(vector4 v)

length2 —— 计算vector/vector4的长度平方

float length2(vector2 v)
float length2(vector v)
float length2(vector4 v)

normalize —— 标准化矢量

<vector> normalize(<vector>v)

dot —— 点乘(内积)

float dot(vector2 a, vector2 b)
float dot(vector a, vector b)
float dot(vector4 a, vector4 b)
float dot(vector a, vector4 b)
float dot(vector4 a, vector b)
float dot(matrix2 a, matrix2 b)
float dot(matrix3 a, matrix3 b)
float dot(matrix a, matrix b)
  • 当vector和vector4点乘时,仅前三个分量被使用;
float dot(<type>a[], <type>b[])
int dot(int a[], int b[])
  • dot(a, b) = dot(a[0], b[0]) + ... + dot(a[n-1], b[n-1]) ,n = min(len(a), len(b));
  • 向量的点乘,即对应分量相乘后在求和,值为标量;
  • 几何意义为,向量在另一向量上的投影(a\bulletb = |a||b|\cos \theta);

cross —— 叉乘(外积)

vector cross(vector a, vector b)
  • 向量叉乘仍为向量,且垂直与原向量的平面;
  • 向量叉乘的模长 |a\timesb| = |a||b|\sin \theta ;
//以新点为原点并设置坐标轴
vector xaxis = normalize(v@v1);
vector zaxis = normalize(cross(xaxis, v@v2));
vector yaxis = normalize(cross(xaxis, zaxis));

for(int i=0; i<100; i++){
    float ang = -ch('ang')*$PI*2*i*0.1;
    if(ang>-@ang){
        vector pos = xaxis*cos(ang)+yaxis*sin(ang)+@P;
        addpoint(0,pos);
    }
}    


sin —— 返回正弦值

//n单位为弧度
float sin(float n)
<vector> sin(<vector>n)

cos —— 返回余弦值

//n单位为弧度
float cos(float n)
vector2 cos(vector2 n)
vector cos(vector n)
vector4 cos(vector4 n)

tan —— 返回正切值

//n单位为弧度
float tan(float n)
vector2 tan(vector2 v)
vector tan(vector v)
vector4 tan(vector4 v)

asin —— 返回反正弦

//单位弧度,返回值-π/2~π/2;
float asin(float n)
vector2 asin(vector2 n)
vector asin(vector n)
vector4 asin(vector4 n)

acos —— 返回反余弦

//单位弧度,返回值0~π;
float acos(float v)
vector2 acos(vector2 v)
vector4 acos(vector4 v)
vector acos(vector v)

atan —— 返回反正切

//单位弧度,返回值-π/2~π/2;
float atan(float n)
//等价于atan2
float atan(float y, float x)
<vector> atan(<vector>v)

atan2 —— 返回y/x反正切

//返回值-π~π;
float atan2(float y, float x)

sinh —— 返回双曲线正弦

float sinh(float n)
vector2 sinh(vector2 v)
vector sinh(vector v)
vector4 sinh(vector4 v)

cosh —— 返回双曲线余弦

float cosh(float n)
vector2 cosh(vector2 v)
vector cosh(vector v)
vector4 cosh(vector4 v)

tanh —— 返回双曲线正切

float tanh(float n)
vector2 tanh(vector2 n)
vector tanh(vector n)
vector4 tanh(vector4 n)

shl —— 对整数向左移动指定位

int shl(int a, int bits)

shr —— 对整数向右移动指定位

int shr(int a, int bits)

shrz —— 对整数向右移动指定位

int shrz(int a, int bits)

resample_linear —— 根据线性插值返回新数组

float [] resample_linear(float input[], int new_length)
vector [] resample_linear(vector input[], int new_length)
vector2 [] resample_linear(vector2 input[], int new_length)
vector4 [] resample_linear(vector4 input[], int new_length)

spline —— 沿polyline、spline采样值

float spline(string basis, float sample_pos, float value1, ...)
vector spline(string basis, float sample_pos, vector value1, ...)
vector4 spline(string basis, float sample_pos, vector4 value1, ...)
float spline(string basis, float sample_pos, float values[], ...)
vector spline(string basis, float sample_pos, vector values[], ...)
vector4 spline(string basis, float sample_pos, vector4 values[], ...)
float spline(string bases[], float sample_pos, float values[], ...)
vector spline(string bases[], float sample_pos, vector values[], ...)
vector4 spline(string bases[], float sample_pos, vector4 values[], ...)
float spline(string bases[], float sample_pos, float values[], float positions[], ...)
vector spline(string bases[], float sample_pos, vector values[], float positions[], ...)
vector4 spline(string bases[], float sample_pos, vector4 values[], float positions[], ...)

spline_cdf —— 通过采样spline曲线生成CDF

float [] spline_cdf(string bases[], float values[], float positions[], ...)

kspline —— 返回沿着曲线的插值

float kspline(string basis, float sample_pos, float value1, float key_pos1, ...)
vector kspline(string basis, float sample_pos, vector value1, float key_pos1, ...)
vector4 kspline(string basis, float sample_pos, vector4 value1, float key_pos1, ...)

isfinite —— 检测值是否为正常的有限值

int isfinite(float x)

isnan —— 检测值是否为数字

int isnan(float x)

Du —— 返回给定值的导数(相对于U)

float Du(float n, ...)
vector Du(vector n, ...)
vector4 Du(vector4 n, ...)

Dv —— 返回给定值的导数(相对于V)

float Dv(float n, ...)
vector Dv(vector n, ...)
vector4 Dv(vector4 n, ...)

Dw —— 返回给定值的导数(相对于W)

float Dw(float p, ...)
vector Dw(vector p, ...)
vector4 Dw(vector4 p, ...)

erf —— 高斯误差函数

float erf(float v)
vector2 erf(vector2 v)

erf_inv —— 反向高斯误差函数

float erf_inv(float v)
  • erf_inv(erf(v)) = v = erf(erf_inv(v))

erfc —— 高斯误差函数补数

float erfc(float v)
  • 等价于1 - erf(v);

makebasis —— 对给定的Z轴创建正交基准

void makebasis(vector &xaxis, vector &yaxis, vector zaxis)
void makebasis(vector &xaxis, vector &yaxis, vector zaxis, vector u)

predicate_incircle —— 确定点是在圆环位置

float predicate_incircle(vector2 a, vector2 b, vector2 c, vector2 d)

predicate_insphere —— 确定点是在圆位置

float predicate_insphere(vector a, vector b, vector c, vector d, vector e)

predicate_orient2d —— 确定点方向是在线上

float predicate_orient2d(vector2 a, vector2 b, vector2 c)

predicate_orient3d —— 确定点方向是在平面上

float predicate_orient3d(vector a, vector b, vector c, vector d)

product —— 返回数字列表的乘积

//直接返回参数
float product(float n)
int product(int n)
//返回分量的乘积
float product(vector2 v)
float product(vector v)
float product(vector4 v)
//返回数组元素的乘积
int product(int arr[])
float product(float arr[])
//返回对应分量的乘积
<vector> product(<vector>arr[])

ptlined —— 返回点到线段的最近距离

float ptlined(vector P0, vector P1, vector Q)

sliderframe —— 应用两矢量的最小旋转

vector slideframe(vector t0, vector t1, vector v0)
vector slideframe(vector x0, vector t0, vector v0, vector x1, vector t1)

solvecubic —— 解算立方函数

int solvecubic(float a, float b, float c, float d, float &t1, float &t2, float &t3)
int solvecubic(float a, float b, float c, float d, vector2 &t1, vector2 &t2, vector2 

solvequadratic —— 解算平方函数

int solvequadratic(float a, float b, float c, float &t1, float &t2)
int solvequadratic(float a, float b, float c, vector2 &t1, vector2 &t2)

solvepoly —— 解算函数

int solvepoly(float coef[], float &roots[], int maxiter=0)

solvetriangleSSS —— 从边查找三角形角度

vector solvetriangleSSS(vector sides)
vector solvetriangleSSS(float a, float b, float c)

quaternion —— 创建四元数

  • 另一种形式eulertoquaternion;
vector4 quaternion(matrix3 rotations) //仅应用矩阵的旋转信息
​vector4 quaternion(float angle, vector axis)
vector4 quaternion(vector angleaxis) //方向为旋转轴,大小为旋转角度
//旋转
float ang = @ptnum*ch('ang');
vector axis = chv('axis');
vector4 q = quaternion(ang, axis);
@P = qrotate(q, @P);
//类似
float ang = @ptnum*ch('ang');
@P.x = cos(ang) * @ptnum * ch('r');
@P.z = sin(ang) * @ptnum * ch('r');

qdistance —— 返回两四元数间的距离

float qdistance(vector4 q1, vector4 q2)

qinvert —— 反转四元数旋转

vector4 qinvert(vector4 quaternion)

qmultiply —— 两四元数相乘

vector4 qmultiply(vector4 q1, vector4 q2)
  •  先应用q2,在应用q1;

qrotate —— 使用四元数旋转矢量

vector qrotate(vector4 quaternion, vector v)
int pts[];
for(int i=0; i<(@Frame%$FEND); i++){
    float ang = radians(i);
    float h = cos(ang);
    float r = sqrt(1-pow(h,2));
    vector pos = set(r,h,0);
    
    vector4 q = quaternion(radians(ch('ang')*i), {0,1,0});
    pos = qrotate(q, pos);
    int pt = addpoint(0,pos); 
    pts[i] = pt;
}
addprim(0, "polyline", pts);


ident —— 返回单位矩阵

<matrix> ident()
//单位矩阵
matrix m = ident(); 
cout(m); // {1,0,0,0 0,1,0,0 0,0,1,0 0,0,0,1}

invert —— 逆矩阵

<matrix> invert(<matrix>m)
  • 逆矩阵可类似 a^{-1} (即 \frac{1}{a} ),a * a^{-1} = 1;
  • 如存在 AB = BA= I_{n} (单位矩阵) ,则A是可逆矩阵; 
  • 逆矩阵具有唯一性;
  • 可逆矩阵,行列式不为零 det(A) != 0 ;
  • 行列式不为零 det(A) != 0 ,一定可逆;
matrix m = set(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
@det = determinant(m); //值为0,不可逆
4@cm = m*invert(m); //非单位矩阵

determinant —— 计算矩阵的行列式

float determinant(matrix2 m)
float determinant(matrix3 m)
float determinant(matrix m)
  • 记作det(A)、det A或|A|;

  • 二维向量的行列式表示平行四边形的面积;

  • 三维向量的行列式表示平行六面体的体积;

transpose —— 翻转矩阵

void  transpose(<matrix>&m)
<matrix> transpose(<matrix>m)

premul —— 预乘矩阵

void premul(matrix2 &a, matrix2 b)
void premul(matrix &a, matrix b)
void premul(matrix3 &a, matrix3 b)
void premul(matrix2 &m, matrix2 a, matrix2 b)
void premul(matrix &m, matrix a, matrix b)
void premul(matrix3 &m, matrix3 a, matrix3 b)

outerproduct —— 返回外积

matrix2  outerproduct(vector2 v, vector2 v)
matrix3  outerproduct(vector v, vector v)
matrix  outerproduct(vector4 v, vector4 v)
  • 如两个矢量V1(m\times1)、V2(n\times1),则它们的外积为V1中每个元素与V2中的每个元素相乘所组成矩阵M(m\timesn);

diagonalizesymmetric —— 对称矩阵的对角化

matrix3 diagonalizesymmetric(matrix3 symmat, vector &diag)

eigenvalues —— 计算3*3矩阵的特征值

void eigenvalues(int &nroot, matrix3 mat, vector &real, vector &imaginary)

svddecomp —— 计算3*3矩阵的奇异值分解

void svddecomp(matrix3 input_M, matrix3 &output_U, vector &output_S, matrix3 &output_V)
vector svddecomp(matrix3 input_M)

planesphereintersect —— 计算3D球与3D平面的相交

int planesphereintersect(vector plane_pos, vector plane_normal, vector sphere_pos, float sphere_radius, vector &intersect_pos, float &intersect_radius, float &intersect_distance)
  • 相交返回1,否则返回0;

combinelocaltransform —— 合并local和parent变换

matrix combinelocaltransform(matrix local, matrix parent_world, matrix parent_local, int scale_inherit_mode)
matrix combinelocaltransform(matrix local, matrix parent_world, matrix parent_local, int scale_inherit_mode, matrix &effective_local_transform)

extractlocaltransform —— 从world变换中提取local变换

matrix extractlocaltransform(matrix world, matrix parent_world, matrix parent_local, int scale_inherit_mode)
matrix extractlocaltransform(matrix world, matrix parent_world, matrix parent_local, int mode, matrix &effective_local_transform)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值