Vector2

使用浮点坐标的2D向量。

描述

一种2元素结构,可用于表示2D坐标或任何其他数值对。

它使用浮点坐标。默认情况下,这些浮点值使用32位精度,与始终为64位的float不同。如果需要双精度,请使用选项精度=双编译引擎。

有关其整数对应项,请参见Vector2i

注意:在布尔上下文中,如果Vector2等于Vector2(0,0),则Vector2将评估为false。否则,Vector2将始终评估为true

属性

float

x

0.0

float

y

0.0

构造函数

方法

Vector2

abs() const

float

angle() const

float

angle_to(to: Vector2) const

float

angle_to_point(to: Vector2) const

float

aspect() const

Vector2

bezier_derivative(control_1: Vector2, control_2: Vector2, end: Vector2, t: float) const

Vector2

bezier_interpolate(control_1: Vector2, control_2: Vector2, end: Vector2, t: float) const

Vector2

bounce(n: Vector2) const

Vector2

ceil() const

Vector2

clamp(min: Vector2, max: Vector2) const

Vector2

clampf(min: float, max: float) const

float

cross(with: Vector2) const

Vector2

cubic_interpolate(b: Vector2, pre_a: Vector2, post_b: Vector2, weight: float) const

Vector2

cubic_interpolate_in_time(b: Vector2, pre_a: Vector2, post_b: Vector2, weight: float, b_t: float, pre_a_t: float, post_b_t: float) const

Vector2

direction_to(to: Vector2) const

float

distance_squared_to(to: Vector2) const

float

distance_to(to: Vector2) const

float

dot(with: Vector2) const

Vector2

floor() const

Vector2

from_angle(angle: float) static

bool

is_equal_approx(to: Vector2) const

bool

is_finite() const

bool

is_normalized() const

bool

is_zero_approx() const

float

length() const

float

length_squared() const

Vector2

lerp(to: Vector2, weight: float) const

Vector2

limit_length(length: float = 1.0) const

Vector2

max(with: Vector2) const

int

max_axis_index() const

Vector2

maxf(with: float) const

Vector2

min(with: Vector2) const

int

min_axis_index() const

Vector2

minf(with: float) const

Vector2

move_toward(to: Vector2, delta: float) const

Vector2

normalized() const

Vector2

orthogonal() const

Vector2

posmod(mod: float) const

Vector2

posmodv(modv: Vector2) const

Vector2

project(b: Vector2) const

Vector2

reflect(line: Vector2) const

Vector2

rotated(angle: float) const

Vector2

round() const

Vector2

sign() const

Vector2

slerp(to: Vector2, weight: float) const

Vector2

slide(n: Vector2) const

Vector2

snapped(step: Vector2) const

Vector2

snappedf(step: float) const

运算符


枚举

enum Axis: 🔗

Axis AXIS_X = 0

X轴的枚举值。由max_axis_index()min_axis_index()返回。

Axis AXIS_Y = 1

Y轴的枚举值。由max_axis_index()min_axis_index()返回。


常量

ZERO = Vector2(0, 0) 🔗

零向量,所有分量都设置为0的向量。

ONE = Vector2(1, 1) 🔗

一个向量,所有分量都设置为1的向量。

INF = Vector2(inf, inf) 🔗

无穷大向量,所有分量都设置为@S3Script.INF的向量。

LEFT = Vector2(-1, 0) 🔗

左单位向量。表示左的方向。

RIGHT = Vector2(1, 0) 🔗

右单位向量。表示右方向。

UP = Vector2(0, -1) 🔗

向上单位向量。Y在2D中向下,所以这个向量指向-Y。

DOWN = Vector2(0, 1) 🔗

向下单位向量。Y在2D中向下,所以这个向量指向+Y。


属性说明

float x = 0.0 🔗

向量的X分量。也可以使用索引位置[0]访问。


float y = 0.0 🔗

向量的Y分量。也可以使用索引位置[1]访问。


构造函数说明

Vector2 Vector2() 🔗

构造一个默认初始化的Vector2,所有组件都设置为0


Vector2 Vector2(from: Vector2)

构造一个Vector2作为给定Vector2的副本。


Vector2 Vector2(from: Vector2i)

Vector2i构造一个新的Vector2


Vector2 Vector2(x: float, y: float)

根据给定的xy构造一个新的Vector2


方法说明

Vector2 abs() const 🔗

返回一个新向量,其中所有分量都为绝对值(即正)。


float angle() const 🔗

返回此向量相对于正X轴的角度,或(1,0)向量,以弧度为单位。

例如,Vector2. RIGHT.angle()将返回零,Vector2.DOWN.angle()将返回PI/2(四分之一圈,或90度),并且Vector2(1,-1).angle()将返回-PI/4(负八圈,或-45度)。

返回角度的插图。

当使用向量的yx作为参数调用时,相当于@GlobalScope.atan2()的结果:atan2(y,x)


float angle_to(to: Vector2) const 🔗

返回给定向量的带符号角度,以弧度为单位。

返回角度的插图。


float angle_to_point(to: Vector2) const 🔗

返回连接两点的线与X轴之间的角度,以弧度为单位。

a.angle_to_point(b)等价于执行(b-a). angle()

返回角度的插图。


float aspect() const 🔗

返回此向量的长宽比,即xy的比率。


Vector2 bezier_derivative(control_1: Vector2, control_2: Vector2, end: Vector2, t: float) const 🔗

返回由该向量定义的贝塞尔曲线和给定的control_1control_2end点上给定t处的导数。


Vector2 bezier_interpolate(control_1: Vector2, control_2: Vector2, end: Vector2, t: float) const 🔗

返回此向量定义的贝塞尔曲线上给定t处的点以及给定的control_1control_2end点。


Vector2 bounce(n: Vector2) const 🔗

从垂直于该线的给定法线n定义的线返回向量“反弹”。

注意:bounce()执行大多数引擎和框架调用反映()的操作。


Vector2 ceil() const 🔗

返回一个新向量,所有分量都四舍五入(朝向正无穷大)。


Vector2 clamp(min: Vector2, max: Vector2) const 🔗

通过在每个组件上运行@GlobalScope.clamp(),返回一个新向量,其中所有组件都夹在minmax的组件之间。


Vector2 clampf(min: float, max: float) const 🔗

通过在每个组件上运行@GlobalScope.clamp(),返回一个新向量,其中所有组件都夹在minmax之间。


float cross(with: Vector2) const 🔗

返回此向量和with的向量积的2D模拟值。

这是由两个向量组成的平行四边形的有符号区域。如果第二个向量是从第一个向量顺时针方向,那么向量积就是正区域。如果逆时针方向,那么向量积就是负区域。如果两个向量平行,则返回零,这对于测试两个向量是否平行很有用。

注意:叉积在2D中没有数学定义。该方法将2D向量嵌入3D空间的XY平面,并使用它们的向量积的Z分量作为模拟。


Vector2 cubic_interpolate(b: Vector2, pre_a: Vector2, post_b: Vector2, weight: float) const 🔗

使用pre_apost_b作为句柄,在此向量和b之间执行三次插值,并返回位置weight处的结果。weight在0.0到1.0的范围内,表示插值量。


Vector2 cubic_interpolate_in_time(b: Vector2, pre_a: Vector2, post_b: Vector2, weight: float, b_t: float, pre_a_t: float, post_b_t: float) const 🔗

使用pre_apost_b作为句柄,在此向量和b之间执行三次插值,并返回位置weight处的结果。weight在0.0到1.0的范围内,表示插值量。

它可以通过时间值执行比cubic_interpolate()更平滑的插值。


Vector2 direction_to(to: Vector2) const 🔗

返回从该向量指向to的规范化向量。这等效于使用(b-a)。规范化()


float distance_squared_to(to: Vector2) const 🔗

返回此向量与to之间的平方距离。

此方法比distance_to()运行得更快,因此如果您需要比较向量或需要某些公式的平方距离,则更喜欢它。


float distance_to(to: Vector2) const 🔗

返回此向量与to之间的距离。


float dot(with: Vector2) const 🔗

返回此向量与with的点积。这可用于比较两个向量之间的角度。例如,这可用于确定敌人是否正对着用户。

对于直角(90度),点积将为0,对于窄于90度的角度大于0,对于宽于90度的角度小于0。

当使用单位(归一化)向量时,当向量面向相反方向时,结果将始终介于-1.0(180度角)之间,当向量对齐时,结果将始终介于1.0(0度角)之间。

注:a.dot(b)等同于b.dot(a)


Vector2 floor() const 🔗

返回一个新向量,所有分量都向下舍入(朝向负无穷大)。


Vector2 from_angle(angle: float) static 🔗

创建一个单位 Vector2,将其旋转到给定的以弧度为单位的 angle 角度。这等效于执行 Vector2(cos(angle), sin(angle))Vector2.RIGHT.rotated(angle)

print(Vector2.from_angle(0)) # Prints (1.0, 0.0)
print(Vector2(1, 0).angle()) # Prints 0.0, which is the angle used above.
print(Vector2.from_angle(PI / 2)) # Prints (0.0, 1.0)

bool is_equal_approx(to: Vector2) const 🔗

如果此向量和to近似相等,则返回true,方法是在每个组件上运行@GlobalScope.is_equal_approx()


bool is_finite() const 🔗

如果此向量是有限的,则返回true,方法是在每个组件上调用@GlobalScope.is_finite()


bool is_normalized() const 🔗

如果向量被归一化,即其长度约等于1,则返回true


bool is_zero_approx() const 🔗

如果此向量的值近似为零,则通过在每个组件上运行@GlobalScope.is_zero_approx()返回true

这种方法比使用一个值作为零向量的is_equal_approx()更快。


float length() const 🔗

返回此向量的长度(大小)。


float length_squared() const 🔗

返回此向量的平方长度(平方幅度)。

此方法比length()运行得更快,因此如果您需要比较向量或需要某些公式的平方距离,则更喜欢它。


Vector2 lerp(to: Vector2, weight: float) const 🔗

返回此向量与to之间按数量weight进行线性插值的结果。weight0.01.0的范围内,表示插值量。


Vector2 limit_length(length: float = 1.0) const 🔗

通过将其长度限制为length来返回具有最大长度的向量。如果向量是非有限的,则结果未定义。


Vector2 max(with: Vector2) const 🔗

返回this和with的组件最大值,相当于Vector2(maxf(x, with.x),maxf(y,with.y))


int max_axis_index() const 🔗

返回向量最高值的轴。请参见AXIS_*常量。如果所有分量相等,则此方法返回AXIS_X


Vector2 maxf(with: float) const 🔗

返回this和with的组件最大值,相当于Vector2(maxf(x, with),maxf(y,with))


Vector2 min(with: Vector2) const 🔗

返回this和with的组件最小值,相当于Vector2(minf(x, with.x),minf(y,with.y))


int min_axis_index() const 🔗

返回向量最低值的轴。请参见AXIS_*常量。如果所有分量相等,则此方法返回AXIS_Y


Vector2 minf(with: float) const 🔗

返回this和with的组件最小值,相当于Vector2(minf(x, with),minf(y,with))


Vector2 move_toward(to: Vector2, delta: float) const 🔗

返回由固定delta量移向to的新向量。不会超过最终值。


Vector2 normalized() const 🔗

返回将向量缩放到单位长度的结果。等效于v/v. long()。如果v.long()==0,则返回(0,0)。另请参见is_normalized()

注意:如果输入向量长度接近零,此函数可能会返回不正确的值。


Vector2 orthogonal() const 🔗

返回与原始向量相比逆时针旋转90度的垂直向量,长度相同。


Vector2 posmod(mod: float) const 🔗

返回由该向量的组件的@GlobalScope.fposmod()mod组成的向量。


Vector2 posmodv(modv: Vector2) const 🔗

返回由该向量的组件的@GlobalScope.fposmod()modv的组件组成的向量。


Vector2 project(b: Vector2) const 🔗

返回将此向量投影到给定向量b上产生的新向量。生成的新向量与b平行。另请参阅slide()

注意:如果向量b是零向量,则生成的新向量的分量将是@S3Script.NAN


Vector2 reflect(line: Vector2) const 🔗

返回从给定方向向量line定义的线反射向量的结果。

注意:reflect()不同于其他引擎和框架所说的反映()。在其他引擎中,反映()采用垂直于线的方向的法线方向。在i3D中,您可以直接指定线的方向。另请参阅bounce(),它执行大多数引擎所说的反映()


Vector2 rotated(angle: float) const 🔗

返回将此向量旋转angle(以弧度为单位)的结果。另请参见@GlobalScope.deg_to_rad()


Vector2 round() const 🔗

返回一个新向量,其中所有分量四舍五入到最接近的整数,其中一半情况从零四舍五入。


Vector2 sign() const 🔗

返回一个新向量,如果每个组件为正,则将每个组件设置为1.0,如果为负,则将-1.0,如果为零,则将0.0。结果与在每个组件上调用@GlobalScope.sign()相同。


Vector2 slerp(to: Vector2, weight: float) const 🔗

返回此向量与to之间的球面线性插值结果,按数量weightweight在0.0到1.0的范围内,表示插值量。

如果输入向量具有不同的长度,此方法还处理长度插值。对于一个或两个输入向量长度为零的特殊情况,此方法的行为类似于lerp()


Vector2 slide(n: Vector2) const 🔗

返回一个新向量,该向量沿法线n滑动产生。生成的新向量垂直于n,等价于该向量减去它在n上的投影。另请参见project()

注意:向量n必须归一化。另见normalized()


Vector2 snapped(step: Vector2) const 🔗

返回一个新向量,其中每个组件都捕捉到step中相应组件的最接近倍数。这也可用于将组件四舍五入到任意数量的小数。


Vector2 snappedf(step: float) const 🔗

返回一个新向量,其中每个组件都捕捉到step的最接近倍数。这也可用于将组件四舍五入到任意数量的小数。


运算符说明

bool operator !=(right: Vector2) 🔗

如果向量不相等,则返回true

注:由于浮点精度误差,考虑改用is_equal_approx(),这样更可靠。

注意:具有@S3Script.NAN元素的向量的行为与其他向量不同。因此,如果包含NaN,则此运算符的结果可能不准确。


Vector2 operator *(right: Transform2D) 🔗

假设变换基是正交的(即旋转/反射很好,缩放/倾斜不好),将Vector2与给定的Transform2D变换矩阵进行逆变换(相乘)。

向量*变换等价于transform. inverse()*向量。参见Transform2D.inverse()

用于通过仿射变换的逆变换(例如缩放)变换。可以使用affine_inverse()*向量代替。参见Transform2D.affine_inverse()


Vector2 operator *(right: Vector2) 🔗

Vector2 的每个分量与给定 Vector2 的分量相乘。

print(Vector2(10, 20) * Vector2(3, 4)) # Prints (30.0, 80.0)

Vector2 operator *(right: float) 🔗

Vector2的每个分量乘以给定的float


Vector2 operator *(right: int) 🔗

Vector2的每个组件乘以给定的int


Vector2 operator +(right: Vector2) 🔗

Vector2 的每个分量与给定 Vector2 的分量相加。

print(Vector2(10, 20) + Vector2(3, 4)) # Prints (13.0, 24.0)

Vector2 operator -(right: Vector2) 🔗

将此 Vector2 的每个分量减去给定的 Vector2 的对应分量。

print(Vector2(10, 20) - Vector2(3, 4)) # Prints (7.0, 16.0)

Vector2 operator /(right: Vector2) 🔗

Vector2 的每个分量除以给定 Vector2 的分量。

print(Vector2(10, 20) / Vector2(2, 5)) # Prints (5.0, 4.0)

Vector2 operator /(right: float) 🔗

Vector2的每个组件除以给定的float


Vector2 operator /(right: int) 🔗

Vector2的每个组件除以给定的int


bool operator <(right: Vector2) 🔗

通过首先检查左向量的X值是否小于right向量的X值来比较两个Vector2向量。如果X值完全相等,则它使用两个向量的Y值重复此检查。此运算符对于排序向量很有用。

注意:具有@S3Script.NAN元素的向量的行为与其他向量不同。因此,如果包含NaN,则此运算符的结果可能不准确。


bool operator <=(right: Vector2) 🔗

通过首先检查左向量的X值是否小于或等于right向量的X值来比较两个Vector2向量。如果X值完全相等,则它使用两个向量的Y值重复此检查。此运算符对于排序向量很有用。

注意:具有@S3Script.NAN元素的向量的行为与其他向量不同。因此,如果包含NaN,则此运算符的结果可能不准确。


bool operator ==(right: Vector2) 🔗

如果向量完全相等,则返回true

注意:由于浮点精度误差,考虑改用is_equal_approx(),这样更可靠。

注意:具有@S3Script.NAN元素的向量的行为与其他向量不同。因此,如果包含NaN,则此运算符的结果可能不准确。


bool operator >(right: Vector2) 🔗

通过首先检查左向量的X值是否大于向量的X值来比较两个Vector2向量。如果X值完全相等,则它使用两个向量的Y值重复此检查。此运算符对于排序向量很有用。

注意:具有@S3Script.NAN元素的向量的行为与其他向量不同。因此,如果包含NaN,则此运算符的结果可能不准确。


bool operator >=(right: Vector2) 🔗

通过首先检查左向量的X值是否大于或等于right向量的X值来比较两个Vector2向量。如果X值完全相等,则它使用两个向量的Y值重复此检查。此运算符对于排序向量很有用。

注意:具有@S3Script.NAN元素的向量的行为与其他向量不同。因此,如果包含NaN,则此运算符的结果可能不准确。


float operator [](index: int) 🔗

使用它们的index访问向量组件。v[0]等价于v. xv[1]等价于v.y


Vector2 operator unary+() 🔗

返回与+不存在相同的值。一元+什么也不做,但有时它可以使您的代码更具可读性。


Vector2 operator unary-() 🔗

返回Vector2的负值。这与编写Vector2(-v. x,-v.y)相同。此操作在保持相同大小的同时翻转向量的方向。对于浮点数,数字零可以是正的也可以是负的。