Skip to content

Commit

Permalink
✨ 更新 vector
Browse files Browse the repository at this point in the history
  • Loading branch information
xinansky committed Jun 23, 2024
1 parent b6ad03e commit 7974521
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 43 deletions.
33 changes: 14 additions & 19 deletions Runtime/Extension/Unity/Vector/Vector2/Vector2.Extend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ public static float Distance(this Vector2 source, in float x, in float y = 0)
/// 3:一个矢量和本身进行点积的结果 是该矢量的模的平方 公式:v*v=source*source+target*target+v3*v3+v.*v.=|v|²
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Dot(this Vector2 source, in Vector2 target)
{
return source.x * target.x + source.y * target.y;
}
public static float Dot(this Vector2 source, in Vector2 target) { return source.x * target.x + source.y * target.y; }

/// <summary>
/// 点积 内积 乘积
Expand All @@ -77,10 +74,7 @@ public static float Dot(this Vector2 source, in Vector2 target)
/// 3:一个矢量和本身进行点积的结果 是该矢量的模的平方 公式:v*v=v1*v1+v2*v2+v3*v3+v.*v.=|v|²
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Dot(this Vector2 source, in float x, in float y = 0)
{
return source.x * x + source.y * y;
}
public static float Dot(this Vector2 source, in float x, in float y = 0) { return source.x * x + source.y * y; }

/// <summary>
/// 叉积 外积
Expand All @@ -92,10 +86,7 @@ public static float Dot(this Vector2 source, in float x, in float y = 0)
/// 3:叉积不满足结合律 (a*b)*c!=a*(b*c)
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Cross(this Vector2 source, in Vector2 target)
{
return new Vector3(0, 0, source.x * target.y - source.y * target.x);
}
public static Vector2 Cross(this Vector2 source, in Vector2 target) { return new Vector3(0, 0, source.x * target.y - source.y * target.x); }

/// <summary>
/// 叉积 外积
Expand All @@ -107,18 +98,22 @@ public static Vector2 Cross(this Vector2 source, in Vector2 target)
/// 3:叉积不满足结合律 (a*b)*c!=a*(b*c)
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Cross(this Vector2 source, in float x, in float y)
{
return new Vector3(0, 0, source.x * y - source.y * x);
}
public static Vector3 Cross(this Vector2 source, in float x, in float y) { return new Vector3(0, 0, source.x * y - source.y * x); }

/// <summary>
/// 计算角度
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Angle(this Vector2 source, in Vector2 target)
public static double Angle(this Vector2 source, in Vector2 target) { return Vector2.Angle(source, target); }

/// <summary>
/// 是否约等于另一个向量
/// </summary>
public static Vector2Int RoundToInt(this Vector2 v)
{
return Vector2.Angle(source, target);
var x = Mathf.RoundToInt(v.x);
var y = Mathf.RoundToInt(v.y);
return new Vector2Int(x, y);
}
}
}
}
35 changes: 20 additions & 15 deletions Runtime/Extension/Unity/Vector/Vector3/Vector3.Extend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public static float Distance(this Vector3 source, in Vector3 target)
public static Vector3 Cross(this Vector3 source, in float x, in float y, in float z)
{
return new Vector3(
source.y * z - source.z * y,
source.z * x - source.x * z,
source.x * y - source.y * x);
source.y * z - source.z * y,
source.z * x - source.x * z,
source.x * y - source.y * x);
}

/// <summary>
Expand All @@ -65,9 +65,9 @@ public static Vector3 Cross(this Vector3 source, in float x, in float y, in floa
public static Vector3 Cross(this Vector3 source, in Vector3 target)
{
return new Vector3(
source.y * target.z - source.z * target.y,
source.z * target.x - source.x * target.z,
source.x * target.y - source.y * target.x);
source.y * target.z - source.z * target.y,
source.z * target.x - source.x * target.z,
source.x * target.y - source.y * target.x);
}

//Dot > 0 方向基本相同,夹角在0°到90°之间
Expand All @@ -84,10 +84,7 @@ public static Vector3 Cross(this Vector3 source, in Vector3 target)
/// 3:一个矢量和本身进行点积的结果 是该矢量的模的平方 公式:v*v=v1*v1+v2*v2+v3*v3+v.*v.=|v|²
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Dot(this Vector3 source, in float x, in float y = 0, in float z = 0)
{
return source.x * x + source.y * y + source.z * z;
}
public static float Dot(this Vector3 source, in float x, in float y = 0, in float z = 0) { return source.x * x + source.y * y + source.z * z; }

/// <summary>
/// 点积 内积 乘积
Expand All @@ -99,10 +96,7 @@ public static float Dot(this Vector3 source, in float x, in float y = 0, in floa
/// 3:一个矢量和本身进行点积的结果 是该矢量的模的平方 公式:v*v=v1*v1+v2*v2+v3*v3+v.*v.=|v|²
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Dot(this Vector3 source, in Vector3 target)
{
return source.x * target.x + source.y * target.y + source.z * target.z;
}
public static float Dot(this Vector3 source, in Vector3 target) { return source.x * target.x + source.y * target.y + source.z * target.z; }

/// <summary>
/// Is the <see cref="Vector3.x"/> or <see cref="Vector3.y"/> or <see cref="Vector3.z"/> NaN?
Expand All @@ -126,5 +120,16 @@ public static bool Approximately(this Vector3 sourceValue, Vector3 targetValue)
sourceValue.y.Approximately(targetValue.y) &&
sourceValue.z.Approximately(targetValue.z);
}

/// <summary>
/// 是否约等于另一个向量
/// </summary>
public static Vector3Int RoundToInt(this Vector3 v)
{
var x = Mathf.RoundToInt(v.x);
var y = Mathf.RoundToInt(v.y);
var z = Mathf.RoundToInt(v.z);
return new Vector3Int(x, y, z);
}
}
}
}
18 changes: 9 additions & 9 deletions Runtime/Extension/Unity/Vector/Vector4/Vector4.Extend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public static float Distance(this Vector4 source, in Vector4 target)
public static Vector4 Cross(this in Vector4 source, in Vector4 target)
{
return new Vector4(
source.y * target.z - source.z * target.y,
source.z * target.w - source.w * target.z,
source.w * target.x - source.x * target.w,
source.x * target.y - source.y * target.x);
source.y * target.z - source.z * target.y,
source.z * target.w - source.w * target.z,
source.w * target.x - source.x * target.w,
source.x * target.y - source.y * target.x);
}

/// <summary>
Expand All @@ -68,10 +68,10 @@ public static Vector4 Cross(this in Vector4 source, in Vector4 target)
public static Vector4 Cross(this in Vector4 source, in float x, in float y = 0, in float z = 0, in float w = 0)
{
return new Vector4(
source.y * z - source.z * y,
source.z * w - source.w * z,
source.w * x - source.x * w,
source.x * y - source.y * x);
source.y * z - source.z * y,
source.z * w - source.w * z,
source.w * x - source.x * w,
source.x * y - source.y * x);
}

//Dot > 0 方向基本相同,夹角在0°到90°之间
Expand Down Expand Up @@ -107,4 +107,4 @@ public static bool IsNaN(this Vector4 vector)
float.IsNaN(vector.w);
}
}
}
}

0 comments on commit 7974521

Please sign in to comment.