# Vector
# 说明
Vector类提供了三维空间中的向量操作功能,包括向量之间的计算、距离测量、角度计算、投影、旋转等功能,同时还包含游戏场景中的位置判断,如判断是否在草丛、河道等。
# 如何使用
// 创建一个向量
Vector position(100.0f, 200.0f, 50.0f);
// 计算两点之间的距离
Vector position2(150.0f, 250.0f, 75.0f);
float distance = position.Distance(position2);
// 获取标准化向量
Vector normalized = position.Normalized();
// 向某方向延伸一定距离
Vector extended = position.Extend(position2, 300.0f);
// 判断位置是否在草丛中
bool isInGrass = position.IsGrass();
# X
float X
向量的X坐标值。
Vector position(100.0f, 200.0f, 50.0f);
float xCoord = position.X;
# Y
float Y
向量的Y坐标值。
Vector position(100.0f, 200.0f, 50.0f);
float yCoord = position.Y;
# Z
float Z
向量的Z坐标值。
Vector position(100.0f, 200.0f, 50.0f);
float zCoord = position.Z;
# Zero
static Vector Zero
表示零向量(0, 0, 0)的静态成员变量。
Vector zeroVec = Vector::Zero;
# Vector()
Vector()
默认构造函数,创建一个所有分量为0的向量。
Vector vec;
// 创建了一个(0, 0, 0)的向量
# Vector(float x, float y)
Vector(float x, float y)
创建一个二维向量,Z坐标默认为0。
参数名 | 参数类型 | 描述 |
---|---|---|
x | float | X坐标值 |
y | float | Y坐标值 |
Vector position(100.0f, 200.0f);
// 创建了一个(100, 200, 0)的向量
# Vector(float x, float y, float z)
Vector(float x, float y, float z)
创建一个三维向量。
参数名 | 参数类型 | 描述 |
---|---|---|
x | float | X坐标值 |
y | float | Y坐标值 |
z | float | Z坐标值 |
Vector position(100.0f, 200.0f, 50.0f);
// 创建了一个(100, 200, 50)的向量
# Length
float Length() const
获取向量的长度(模)。
Vector vec(3.0f, 4.0f);
float length = vec.Length(); // 返回5.0
# LengthSquared
float LengthSquared() const
获取向量长度的平方,避免开方运算提高性能。
Vector vec(3.0f, 4.0f);
float lengthSquared = vec.LengthSquared(); // 返回25.0
# Distance
float Distance(const Vector& vOther, bool squared = false) const
计算当前向量与另一个向量之间的距离。
参数名 | 参数类型 | 描述 |
---|---|---|
vOther | const Vector& | 另一个向量 |
squared | bool | 是否返回距离的平方,默认为false |
Vector pos1(100.0f, 200.0f);
Vector pos2(150.0f, 250.0f);
float dist = pos1.Distance(pos2); // 获取两点之间的距离
float distSquared = pos1.Distance(pos2, true); // 获取两点之间距离的平方
# Distance
float Distance(AIBaseClient* unit, bool squared = false) const
计算当前向量与游戏单位之间的距离。
参数名 | 参数类型 | 描述 |
---|---|---|
unit | AIBaseClient* | 游戏单位 |
squared | bool | 是否返回距离的平方,默认为false |
Vector myPosition(100.0f, 200.0f);
AIBaseClient* enemy = GetNearestEnemy();
float distToEnemy = myPosition.Distance(enemy);
# Distance
float Distance(const Vector& segmentStart, const Vector& segmentEnd, bool onlyIfOnSegment = false, bool squared = false) const
计算当前向量到线段的距离。
参数名 | 参数类型 | 描述 |
---|---|---|
segmentStart | const Vector& | 线段的起始点 |
segmentEnd | const Vector& | 线段的终止点 |
onlyIfOnSegment | bool | 是否仅当投影点在线段上时才计算距离,默认为false |
squared | bool | 是否返回距离的平方,默认为false |
Vector myPosition(100.0f, 200.0f);
Vector lineStart(0.0f, 0.0f);
Vector lineEnd(200.0f, 200.0f);
float distToLine = myPosition.Distance(lineStart, lineEnd);
# DistanceSquared
float DistanceSquared(const Vector& vOther) const
计算当前向量与另一个向量之间距离的平方。
参数名 | 参数类型 | 描述 |
---|---|---|
vOther | const Vector& | 另一个向量 |
Vector pos1(100.0f, 200.0f);
Vector pos2(150.0f, 250.0f);
float distSquared = pos1.DistanceSquared(pos2);
# Polar
float Polar() const
获取向量的极角(弧度)。
Vector vec(1.0f, 1.0f);
float angle = vec.Polar(); // 返回π/4(约0.785)
# AngleBetween
float AngleBetween(const Vector& other) const
计算当前向量与另一个向量之间的夹角(弧度)。
参数名 | 参数类型 | 描述 |
---|---|---|
other | const Vector& | 另一个向量 |
Vector vec1(1.0f, 0.0f);
Vector vec2(0.0f, 1.0f);
float angle = vec1.AngleBetween(vec2); // 返回π/2(约1.57)
# CrossProduct
float CrossProduct(const Vector& other) const
计算当前向量与另一个向量的叉积。
参数名 | 参数类型 | 描述 |
---|---|---|
other | const Vector& | 另一个向量 |
Vector vec1(1.0f, 0.0f);
Vector vec2(0.0f, 1.0f);
float cross = vec1.CrossProduct(vec2);
# DotProduct
float DotProduct(const Vector& other) const
计算当前向量与另一个向量的点积。
参数名 | 参数类型 | 描述 |
---|---|---|
other | const Vector& | 另一个向量 |
Vector vec1(1.0f, 0.0f);
Vector vec2(0.0f, 1.0f);
float dot = vec1.DotProduct(vec2); // 返回0.0
# Extend
Vector Extend(const Vector& to, float distance) const
从当前向量朝向目标向量方向延伸指定距离。
参数名 | 参数类型 | 描述 |
---|---|---|
to | const Vector& | 目标向量 |
distance | float | 延伸距离 |
Vector myPosition(100.0f, 100.0f);
Vector targetPosition(200.0f, 200.0f);
Vector extended = myPosition.Extend(targetPosition, 150.0f);
# Normalized
Vector Normalized() const
返回当前向量的单位向量(长度为1)。
Vector vec(3.0f, 4.0f);
Vector normalized = vec.Normalized(); // 返回(0.6, 0.8)的单位向量
# Perpendicular
Vector Perpendicular() const
返回当前向量的垂直向量。
Vector vec(1.0f, 0.0f);
Vector perp = vec.Perpendicular(); // 返回(0.0, 1.0)
# Perpendicular2
Vector Perpendicular2() const
返回当前向量的另一个垂直向量。
Vector vec(1.0f, 0.0f);
Vector perp = vec.Perpendicular2(); // 返回(0.0, -1.0)
# Rotated
Vector Rotated(float angle) const
返回当前向量旋转指定角度后的向量。
参数名 | 参数类型 | 描述 |
---|---|---|
angle | float | 旋转角度(弧度) |
Vector vec(1.0f, 0.0f);
Vector rotated = vec.Rotated(1.57f); // 旋转π/2弧度,约等于(0.0, 1.0)
# RotateAroundPoint
Vector RotateAroundPoint(const Vector& around, float angle) const
以指定点为中心旋转向量。
参数名 | 参数类型 | 描述 |
---|---|---|
around | const Vector& | 旋转中心点 |
angle | float | 旋转角度(弧度) |
Vector vec(200.0f, 100.0f);
Vector center(100.0f, 100.0f);
Vector rotated = vec.RotateAroundPoint(center, 1.57f); // 围绕center点旋转π/2弧度
# Shorten
Vector Shorten(const Vector& to, float distance) const
从当前向量朝向目标向量方向缩短指定距离。
参数名 | 参数类型 | 描述 |
---|---|---|
to | const Vector& | 目标向量 |
distance | float | 缩短距离 |
Vector myPosition(100.0f, 100.0f);
Vector targetPosition(200.0f, 200.0f);
Vector shortened = myPosition.Shorten(targetPosition, 50.0f);
# Center
Vector Center(const Vector& to) const
返回当前向量与目标向量的中点。
参数名 | 参数类型 | 描述 |
---|---|---|
to | const Vector& | 目标向量 |
Vector pos1(100.0f, 100.0f);
Vector pos2(200.0f, 200.0f);
Vector center = pos1.Center(pos2); // 返回(150.0, 150.0)
# SetZ
Vector SetZ(float value = -1.f) const
设置向量的Z坐标,默认使用GetHeightForPosition获取地形高度。
参数名 | 参数类型 | 描述 |
---|---|---|
value | float | Z坐标值,默认为-1.f表示使用GetHeightForPosition |
Vector position(100.0f, 200.0f);
Vector withHeight = position.SetZ(); // 使用地形高度
Vector withCustomHeight = position.SetZ(50.0f); // 使用自定义高度
# ProjectOn
ProjectionInfo ProjectOn(const Vector& segmentStart, const Vector& segmentEnd) const
将当前向量投影到由两点组成的线段上。
参数名 | 参数类型 | 描述 |
---|---|---|
segmentStart | const Vector& | 线段起始点 |
segmentEnd | const Vector& | 线段终止点 |
Vector position(150.0f, 150.0f);
Vector lineStart(100.0f, 100.0f);
Vector lineEnd(200.0f, 200.0f);
ProjectionInfo proj = position.ProjectOn(lineStart, lineEnd);
# Intersection
IntersectionResult Intersection(const Vector& lineSegment1End, const Vector& lineSegment2Start, const Vector& lineSegment2End) const
计算两条线段的交点。
参数名 | 参数类型 | 描述 |
---|---|---|
lineSegment1End | const Vector& | 第一条线段的终点(当前向量为起点) |
lineSegment2Start | const Vector& | 第二条线段的起点 |
lineSegment2End | const Vector& | 第二条线段的终点 |
Vector line1Start(100.0f, 100.0f);
Vector line1End(200.0f, 200.0f);
Vector line2Start(100.0f, 200.0f);
Vector line2End(200.0f, 100.0f);
IntersectionResult result = line1Start.Intersection(line1End, line2Start, line2End);
# IsValid
bool IsValid() const
检查向量是否有效。
Vector position(100.0f, 200.0f);
bool valid = position.IsValid();
# CountAlliesInRange
int CountAlliesInRange(float range) const
计算指定范围内的友方单位数量。
参数名 | 参数类型 | 描述 |
---|---|---|
range | float | 计算范围 |
Vector position = GetMyPosition();
int alliesCount = position.CountAlliesInRange(500.0f);
# CountEnemiesInRange
int CountEnemiesInRange(float range) const
计算指定范围内的敌方单位数量。
参数名 | 参数类型 | 描述 |
---|---|---|
range | float | 计算范围 |
Vector position = GetMyPosition();
int enemiesCount = position.CountEnemiesInRange(500.0f);
# CountEnemyMinionsInRange
int CountEnemyMinionsInRange(float range) const
计算指定范围内的敌方小兵数量。
参数名 | 参数类型 | 描述 |
---|---|---|
range | float | 计算范围 |
Vector position = GetMyPosition();
int enemyMinionsCount = position.CountEnemyMinionsInRange(500.0f);
# CountAllyMinionsInRange
int CountAllyMinionsInRange(float range) const
计算指定范围内的友方小兵数量。
参数名 | 参数类型 | 描述 |
---|---|---|
range | float | 计算范围 |
Vector position = GetMyPosition();
int allyMinionsCount = position.CountAllyMinionsInRange(500.0f);
# IsOnScreenV2
bool IsOnScreenV2() const
检查该位置是否在屏幕上(2D视角)。
Vector position = GetObjectPosition();
bool onScreen = position.IsOnScreenV2();
# IsOnScreenV3
bool IsOnScreenV3() const
检查该位置是否在屏幕上(3D视角)。
Vector position = GetObjectPosition();
bool onScreen = position.IsOnScreenV3();
# IsWall
bool IsWall() const
检查该位置是否为墙体。
Vector position(1000.0f, 1000.0f);
bool isWall = position.IsWall();
# IsGrass
bool IsGrass() const
检查该位置是否为草丛。
Vector position(1000.0f, 1000.0f);
bool isGrass = position.IsGrass();
# IsRiver
bool IsRiver() const
检查该位置是否为河道。
Vector position(1000.0f, 1000.0f);
bool isRiver = position.IsRiver();
# IsInFOW
bool IsInFOW() const
检查该位置是否在战争迷雾中。
Vector position(1000.0f, 1000.0f);
bool inFog = position.IsInFOW();
# GetHeightForPosition
float GetHeightForPosition() const
获取该位置的地形高度。
Vector position(1000.0f, 1000.0f);
float height = position.GetHeightForPosition();
# UnderAllyTurret
bool UnderAllyTurret(float ExtraRange = 0.f) const
检查该位置是否在友方防御塔射程内。
参数名 | 参数类型 | 描述 |
---|---|---|
ExtraRange | float | 额外的射程范围,默认为0 |
Vector position = GetMyPosition();
bool underTurret = position.UnderAllyTurret();
bool underTurretWithExtraRange = position.UnderAllyTurret(100.0f);
# UnderEnemyTurret
bool UnderEnemyTurret(float ExtraRange = 0.f) const
检查该位置是否在敌方防御塔射程内。
参数名 | 参数类型 | 描述 |
---|---|---|
ExtraRange | float | 额外的射程范围,默认为0 |
Vector position = GetMyPosition();
bool underTurret = position.UnderEnemyTurret();
bool underTurretWithExtraRange = position.UnderEnemyTurret(100.0f);
# WorldToMinimap
Vector WorldToMinimap() const
将世界坐标转换为小地图坐标。
Vector worldPos(1000.0f, 1000.0f);
Vector minimapPos = worldPos.WorldToMinimap();
# WorldToScreen
Vector WorldToScreen() const
将世界坐标转换为屏幕坐标。
Vector worldPos(1000.0f, 1000.0f);
Vector screenPos = worldPos.WorldToScreen();
# operator=
Vector& operator=(const Vector& other)
向量赋值运算符。
Vector v1(100.0f, 200.0f);
Vector v2 = v1; // v2现在是(100.0, 200.0)的向量
# operator-
Vector operator-() const
向量取负运算符。
Vector v1(100.0f, 200.0f);
Vector v2 = -v1; // v2现在是(-100.0, -200.0)的向量
# operator+
Vector operator+(const Vector& other) const
向量加法运算符。
Vector v1(100.0f, 200.0f);
Vector v2(50.0f, 50.0f);
Vector sum = v1 + v2; // sum为(150.0, 250.0)的向量
# operator-
Vector operator-(const Vector& other) const
向量减法运算符。
Vector v1(100.0f, 200.0f);
Vector v2(50.0f, 50.0f);
Vector diff = v1 - v2; // diff为(50.0, 150.0)的向量
# operator*
Vector operator*(float scalar) const
向量与标量的乘法运算符。
Vector v1(100.0f, 200.0f);
Vector scaled = v1 * 2.0f; // scaled为(200.0, 400.0)的向量
# operator*
friend Vector operator*(float scalar, const Vector& vec)
标量与向量的乘法运算符。
Vector v1(100.0f, 200.0f);
Vector scaled = 2.0f * v1; // scaled为(200.0, 400.0)的向量
# operator/
Vector operator/(float scalar) const
向量与标量的除法运算符。
Vector v1(100.0f, 200.0f);
Vector divided = v1 / 2.0f; // divided为(50.0, 100.0)的向量
# operator==
bool operator==(const Vector& other) const
向量相等比较运算符。
Vector v1(100.0f, 200.0f);
Vector v2(100.0f, 200.0f);
bool equal = (v1 == v2); // equal为true
# operator!=
bool operator!=(const Vector& other) const
向量不等比较运算符。
Vector v1(100.0f, 200.0f);
Vector v2(50.0f, 50.0f);
bool notEqual = (v1 != v2); // notEqual为true