< Summary

Line coverage
41%
Covered lines: 68
Uncovered lines: 94
Coverable lines: 162
Total lines: 719
Line coverage: 41.9%
Branch coverage
20%
Covered branches: 6
Total branches: 30
Branch coverage: 20%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
File 1: .ctor(...)100%11100%
File 1: .ctor(...)100%11100%
File 1: .ctor(...)100%210%
File 1: get_Zero()100%11100%
File 1: get_One()100%11100%
File 1: get_Right()100%11100%
File 1: get_Left()100%210%
File 1: get_Up()100%11100%
File 1: get_Down()100%210%
File 1: get_MaxValue()100%210%
File 1: get_MinValue()100%210%
File 1: get_SqrMagnitude()100%11100%
File 1: get_SqrMagnitudeFast()100%210%
File 1: get_Magnitude()100%11100%
File 1: get_Normalized()100%11100%
File 1: NormalizedWithMagnitude(...)100%210%
File 1: Normalize(...)100%22100%
File 1: Normalize(...)0%620%
File 1: op_Addition(...)100%11100%
File 1: op_Subtraction(...)100%11100%
File 1: op_UnaryNegation(...)100%11100%
File 1: op_Multiply(...)100%11100%
File 1: op_Multiply(...)100%11100%
File 1: op_Division(...)100%11100%
File 1: op_Equality(...)50%22100%
File 1: op_Inequality(...)100%210%
File 1: Dot(...)100%11100%
File 1: Cross(...)100%11100%
File 1: Distance(...)100%11100%
File 1: DistanceSquared(...)100%11100%
File 1: Lerp(...)100%11100%
File 1: LerpUnclamped(...)100%210%
File 1: ClampMagnitude(...)100%22100%
File 1: Reflect(...)100%11100%
File 1: Project(...)50%22100%
File 1: Perpendicular(...)100%11100%
File 1: Rotate(...)100%11100%
File 1: Rotate(...)100%11100%
File 1: Scale(...)100%11100%
File 1: Min(...)100%11100%
File 1: Max(...)100%11100%
File 1: Angle(...)0%2040%
File 1: SignedAngle(...)0%620%
File 1: MoveTowards(...)0%2040%
File 1: SmoothDamp(...)100%210%
File 1: PointInTriangle(...)0%7280%
File 1: Equals(...)100%210%
File 1: Equals(...)0%620%
File 1: GetHashCode()100%210%
File 1: ToString()100%210%
File 2: get_XX()100%11100%
File 2: get_XY()100%11100%
File 2: get_XO()100%210%
File 2: get_YX()100%11100%
File 2: get_YY()100%11100%
File 2: get_YO()100%210%
File 2: get_OX()100%210%
File 2: get_OY()100%210%
File 2: get_OO()100%210%
File 2: get_XXX()100%210%
File 2: get_XXY()100%210%
File 2: get_XXO()100%210%
File 2: get_XYX()100%210%
File 2: get_XYY()100%210%
File 2: get_XYO()100%11100%
File 2: get_XOX()100%210%
File 2: get_XOY()100%210%
File 2: get_XOO()100%210%
File 2: get_YXX()100%210%
File 2: get_YXY()100%210%
File 2: get_YXO()100%210%
File 2: get_YYX()100%210%
File 2: get_YYY()100%210%
File 2: get_YYO()100%210%
File 2: get_YOX()100%210%
File 2: get_YOY()100%210%
File 2: get_YOO()100%210%
File 2: get_OXX()100%210%
File 2: get_OXY()100%210%
File 2: get_OXO()100%210%
File 2: get_OYX()100%210%
File 2: get_OYY()100%210%
File 2: get_OYO()100%210%
File 2: get_OOX()100%210%
File 2: get_OOY()100%210%
File 2: get_OOO()100%210%

File(s)

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/Math/FPVector2.cs

#LineLine coverage
 1// Copyright (c) 2026 Sideline Authors. All rights reserved.
 2// Licensed under GPL-3.0.
 3
 4#nullable enable
 5
 6using System;
 7using System.Runtime.CompilerServices;
 8using System.Runtime.InteropServices;
 9using Lattice.Generators;
 10
 11namespace Lattice.Math
 12{
 13    /// <summary>
 14    /// 2D 定点数向量
 15    /// <para>参考 FrameSyncEngine 设计,高性能实现</para>
 16    /// <para>内存布局:连续 16 字节(2 * 8 bytes)</para>
 17    /// </summary>
 18    /// <remarks>
 19    /// <para><b>Swizzle 属性:</b></para>
 20    /// 本结构使用 <see cref="GenerateSwizzleAttribute"/> Source Generator 自动生成 Swizzle 属性。
 21    /// 可用属性包括:XX, XY, YX, YY, XXX, XYX, XYY, YXX, YXY, YYX, YYY, XXXY, XXYZ 等。
 22    ///
 23    /// <para><b>内存布局:</b></para>
 24    /// 使用 <see cref="StructLayoutAttribute"/> 显式控制内存布局,确保跨平台一致性。
 25    /// X 分量在偏移 0,Y 分量在偏移 8。
 26    /// </remarks>
 27    [StructLayout(LayoutKind.Explicit, Size = 16)]  // 2 * 8 bytes, Explicit for cross-platform consistency
 28    [GenerateSwizzle(MaxDimension = 3, IncludeZero = true)]
 29    public readonly partial struct FPVector2 : IEquatable<FPVector2>
 30    {
 31        #region 字段
 32
 33        /// <summary>X 分量</summary>
 34        [FieldOffset(0)]
 35        public readonly FP X;
 36
 37        /// <summary>Y 分量</summary>
 38        [FieldOffset(8)]
 39        public readonly FP Y;
 40
 41        #endregion
 42
 43        #region 构造函数
 44
 45        /// <summary>
 46        /// 从两个 FP 构造
 47        /// </summary>
 48        /// <param name="x">X 分量</param>
 49        /// <param name="y">Y 分量</param>
 50        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 51        public FPVector2(FP x, FP y)
 52        {
 18453            X = x;
 18454            Y = y;
 18455        }
 56
 57        /// <summary>
 58        /// 从两个 int 构造(隐式转换)
 59        /// </summary>
 60        /// <param name="x">X 分量</param>
 61        /// <param name="y">Y 分量</param>
 62        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 63        public FPVector2(int x, int y)
 64        {
 34265            X = (FP)x;
 34266            Y = (FP)y;
 34267        }
 68
 69        /// <summary>
 70        /// 从单个 FP 构造(两个分量相同)
 71        /// </summary>
 72        /// <param name="value">X 和 Y 分量的值</param>
 73        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 74        public FPVector2(FP value)
 75        {
 076            X = value;
 077            Y = value;
 078        }
 79
 80        #endregion
 81
 82        #region 常量
 83
 84        /// <summary>零向量 (0, 0)</summary>
 685        public static FPVector2 Zero => default;
 86
 87        /// <summary>单位向量 (1, 1)</summary>
 288        public static FPVector2 One => new(FP._1, FP._1);
 89
 90        /// <summary>右向量 (1, 0)</summary>
 791        public static FPVector2 Right => new(FP._1, FP._0);
 92
 93        /// <summary>左向量 (-1, 0)</summary>
 094        public static FPVector2 Left => new(-FP._1, FP._0);
 95
 96        /// <summary>上向量 (0, 1)</summary>
 597        public static FPVector2 Up => new(FP._0, FP._1);
 98
 99        /// <summary>下向量 (0, -1)</summary>
 0100        public static FPVector2 Down => new(FP._0, -FP._1);
 101
 102        /// <summary>最大值向量</summary>
 0103        public static FPVector2 MaxValue => new(new FP(long.MaxValue), new FP(long.MaxValue));
 104
 105        /// <summary>最小值向量</summary>
 0106        public static FPVector2 MinValue => new(new FP(long.MinValue), new FP(long.MinValue));
 107
 108        #endregion
 109
 110        // Swizzle 属性由 Source Generator 自动生成
 111        // 参见 Tools/SwizzleGenerator
 112
 113        #region 属性
 114
 115        /// <summary>
 116        /// 向量长度的平方(不开方,速度快)- 精确版
 117        /// <para>使用四舍五入,精度更高</para>
 118        /// </summary>
 119        /// <remarks>
 120        /// 计算公式:(X*X + Y*Y) / ONE,+32768 实现四舍五入
 121        /// </remarks>
 122        public readonly FP SqrMagnitude
 123        {
 124            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 125            get
 126            {
 127                // (X*X + Y*Y) / ONE,+32768 实现四舍五入
 10128                long x2 = (X.RawValue * X.RawValue + 32768) >> 16;
 10129                long y2 = (Y.RawValue * Y.RawValue + 32768) >> 16;
 10130                return new FP(x2 + y2);
 131            }
 132        }
 133
 134        /// <summary>
 135        /// 向量长度的平方 - 快速版(截断)
 136        /// <para>无四舍五入开销,适合性能敏感场景</para>
 137        /// </summary>
 138        /// <remarks>
 139        /// 误差:最大 1 LSB(约 0.000015)
 140        /// </remarks>
 141        public readonly FP SqrMagnitudeFast
 142        {
 143            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 144            get
 145            {
 146                // 直接截断,无 +32768 开销
 0147                long x2 = (X.RawValue * X.RawValue) >> 16;
 0148                long y2 = (Y.RawValue * Y.RawValue) >> 16;
 0149                return new FP(x2 + y2);
 150            }
 151        }
 152
 153        /// <summary>
 154        /// 向量长度
 155        /// </summary>
 156        public readonly FP Magnitude
 157        {
 158            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 5159            get => FPMath.Sqrt(SqrMagnitude);
 160        }
 161
 162        /// <summary>
 163        /// 归一化向量(FrameSync 免除法优化)
 164        /// <para>零向量返回零向量</para>
 165        /// <para>使用倒数乘法而非除法,速度快约 2-3 倍</para>
 166        /// </summary>
 167        public readonly FPVector2 Normalized
 168        {
 169            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 11170            get => Normalize(this);
 171        }
 172
 173        /// <summary>
 174        /// 归一化向量并返回原始长度
 175        /// <para>FrameSync 风格免除法实现</para>
 176        /// </summary>
 177        /// <param name="magnitude">输出原始长度</param>
 178        /// <returns>归一化后的向量</returns>
 179        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 180        public readonly FPVector2 NormalizedWithMagnitude(out FP magnitude)
 181        {
 0182            return Normalize(this, out magnitude);
 183        }
 184
 185        /// <summary>
 186        /// 归一化向量(静态方法,FrameSync 优化)
 187        /// <para>使用指数-尾数分解 + 倒数乘法,避免除法</para>
 188        /// </summary>
 189        /// <param name="value">要归一化的向量</param>
 190        /// <returns>归一化后的向量</returns>
 191        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 192        public static FPVector2 Normalize(FPVector2 value)
 193        {
 11194            ulong sqrmag = (ulong)(value.X.RawValue * value.X.RawValue + value.Y.RawValue * value.Y.RawValue);
 13195            if (sqrmag == 0) return Zero;
 196
 9197            var (reciprocal, shift) = FPMath.GetReciprocalForNormalize(sqrmag);
 198
 9199            return new FPVector2(
 9200                new FP(value.X.RawValue * reciprocal >> shift),
 9201                new FP(value.Y.RawValue * reciprocal >> shift)
 9202            );
 203        }
 204
 205        /// <summary>
 206        /// 归一化向量并返回原始长度(FrameSync 优化)
 207        /// </summary>
 208        /// <param name="value">要归一化的向量</param>
 209        /// <param name="magnitude">输出原始长度</param>
 210        /// <returns>归一化后的向量</returns>
 211        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 212        public static FPVector2 Normalize(FPVector2 value, out FP magnitude)
 213        {
 0214            ulong sqrmag = (ulong)(value.X.RawValue * value.X.RawValue + value.Y.RawValue * value.Y.RawValue);
 0215            if (sqrmag == 0)
 216            {
 0217                magnitude = FP.Zero;
 0218                return Zero;
 219            }
 220
 0221            var sqrt = FPMath.GetSqrtDecomp(sqrmag);
 0222            var (reciprocal, shift) = FPMath.GetReciprocalForNormalize(sqrmag);
 223
 0224            magnitude = FP.FromRaw((long)sqrt.Mantissa << sqrt.Exponent >> 14);
 225
 0226            return new FPVector2(
 0227                new FP(value.X.RawValue * reciprocal >> shift),
 0228                new FP(value.Y.RawValue * reciprocal >> shift)
 0229            );
 230        }
 231
 232        #endregion
 233
 234        #region 运算符
 235
 236        /// <summary>向量加法</summary>
 237        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 238        public static FPVector2 operator +(FPVector2 a, FPVector2 b)
 129239            => new(a.X + b.X, a.Y + b.Y);
 240
 241        /// <summary>向量减法</summary>
 242        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 243        public static FPVector2 operator -(FPVector2 a, FPVector2 b)
 4244            => new(a.X - b.X, a.Y - b.Y);
 245
 246        /// <summary>向量取反</summary>
 247        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 248        public static FPVector2 operator -(FPVector2 v)
 1249            => new(-v.X, -v.Y);
 250
 251        /// <summary>向量 * 标量</summary>
 252        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 253        public static FPVector2 operator *(FPVector2 v, FP s)
 5254            => new(v.X * s, v.Y * s);
 255
 256        /// <summary>标量 * 向量</summary>
 257        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 258        public static FPVector2 operator *(FP s, FPVector2 v)
 1259            => v * s;
 260
 261        /// <summary>向量 / 标量</summary>
 262        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 263        public static FPVector2 operator /(FPVector2 v, FP s)
 1264            => new(v.X / s, v.Y / s);
 265
 266        /// <summary>相等比较</summary>
 267        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 268        public static bool operator ==(FPVector2 a, FPVector2 b)
 2269            => a.X.RawValue == b.X.RawValue && a.Y.RawValue == b.Y.RawValue;
 270
 271        /// <summary>不等比较</summary>
 272        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 273        public static bool operator !=(FPVector2 a, FPVector2 b)
 0274            => !(a == b);
 275
 276        #endregion
 277
 278        #region 静态方法
 279
 280        /// <summary>
 281        /// 点积
 282        /// </summary>
 283        /// <param name="a">第一个向量</param>
 284        /// <param name="b">第二个向量</param>
 285        /// <returns>点积结果</returns>
 286        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 287        public static FP Dot(FPVector2 a, FPVector2 b)
 288        {
 6289            long x = (a.X.RawValue * b.X.RawValue + 32768) >> 16;
 6290            long y = (a.Y.RawValue * b.Y.RawValue + 32768) >> 16;
 6291            return new FP(x + y);
 292        }
 293
 294        /// <summary>
 295        /// 叉积(2D 叉积返回标量,表示有向面积)
 296        /// </summary>
 297        /// <param name="a">第一个向量</param>
 298        /// <param name="b">第二个向量</param>
 299        /// <returns>叉积结果(标量)</returns>
 300        /// <remarks>
 301        /// 2D 叉积等价于 a.X * b.Y - a.Y * b.X,表示两个向量张成的平行四边形的有向面积。
 302        /// 结果为正表示 b 在 a 的逆时针方向,为负表示顺时针方向。
 303        /// </remarks>
 304        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 305        public static FP Cross(FPVector2 a, FPVector2 b)
 306        {
 1307            long x = (a.X.RawValue * b.Y.RawValue + 32768) >> 16;
 1308            long y = (a.Y.RawValue * b.X.RawValue + 32768) >> 16;
 1309            return new FP(x - y);
 310        }
 311
 312        /// <summary>
 313        /// 两个向量之间的距离
 314        /// </summary>
 315        /// <param name="a">第一个向量</param>
 316        /// <param name="b">第二个向量</param>
 317        /// <returns>欧几里得距离</returns>
 318        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 319        public static FP Distance(FPVector2 a, FPVector2 b)
 1320            => (a - b).Magnitude;
 321
 322        /// <summary>
 323        /// 两个向量之间的距离平方(更快)
 324        /// </summary>
 325        /// <param name="a">第一个向量</param>
 326        /// <param name="b">第二个向量</param>
 327        /// <returns>距离平方</returns>
 328        /// <remarks>
 329        /// 适用于比较距离大小而不需要精确值的场景(如范围检测)。
 330        /// 比 <see cref="Distance"/> 快约 3-5 倍(省去了平方根运算)。
 331        /// </remarks>
 332        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 333        public static FP DistanceSquared(FPVector2 a, FPVector2 b)
 1334            => (a - b).SqrMagnitude;
 335
 336        /// <summary>
 337        /// 线性插值(t 限制在 [0,1])
 338        /// </summary>
 339        /// <param name="a">起始向量</param>
 340        /// <param name="b">目标向量</param>
 341        /// <param name="t">插值系数 [0, 1]</param>
 342        /// <returns>插值结果</returns>
 343        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 344        public static FPVector2 Lerp(FPVector2 a, FPVector2 b, FP t)
 345        {
 3346            t = FPMath.Clamp01(t);
 3347            return new FPVector2(
 3348                new FP(a.X.RawValue + ((b.X.RawValue - a.X.RawValue) * t.RawValue + 32768 >> 16)),
 3349                new FP(a.Y.RawValue + ((b.Y.RawValue - a.Y.RawValue) * t.RawValue + 32768 >> 16))
 3350            );
 351        }
 352
 353        /// <summary>
 354        /// 线性插值(t 不限制)
 355        /// </summary>
 356        /// <param name="a">起始向量</param>
 357        /// <param name="b">目标向量</param>
 358        /// <param name="t">插值系数(任意值)</param>
 359        /// <returns>插值结果</returns>
 360        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 361        public static FPVector2 LerpUnclamped(FPVector2 a, FPVector2 b, FP t)
 362        {
 0363            return new FPVector2(
 0364                new FP(a.X.RawValue + ((b.X.RawValue - a.X.RawValue) * t.RawValue + 32768 >> 16)),
 0365                new FP(a.Y.RawValue + ((b.Y.RawValue - a.Y.RawValue) * t.RawValue + 32768 >> 16))
 0366            );
 367        }
 368
 369        /// <summary>
 370        /// 限制向量长度
 371        /// </summary>
 372        /// <param name="vector">要限制的向量</param>
 373        /// <param name="maxLength">最大长度</param>
 374        /// <returns>限制后的向量</returns>
 375        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 376        public static FPVector2 ClampMagnitude(FPVector2 vector, FP maxLength)
 377        {
 2378            FP sqrMag = vector.SqrMagnitude;
 2379            FP maxSqr = maxLength * maxLength;
 2380            if (sqrMag.RawValue > maxSqr.RawValue)
 1381                return vector.Normalized * maxLength;
 1382            return vector;
 383        }
 384
 385        /// <summary>
 386        /// 反射向量
 387        /// </summary>
 388        /// <param name="direction">入射方向</param>
 389        /// <param name="normal">法线(需归一化)</param>
 390        /// <returns>反射后的方向</returns>
 391        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 392        public static FPVector2 Reflect(FPVector2 direction, FPVector2 normal)
 393        {
 1394            FP twoDot = Dot(direction, normal) * 2;
 1395            return direction - normal * twoDot;
 396        }
 397
 398        /// <summary>
 399        /// 投影向量到指定法线上
 400        /// </summary>
 401        /// <param name="vector">要投影的向量</param>
 402        /// <param name="onNormal">投影目标法线</param>
 403        /// <returns>投影后的向量</returns>
 404        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 405        public static FPVector2 Project(FPVector2 vector, FPVector2 onNormal)
 406        {
 1407            FP sqrMag = onNormal.SqrMagnitude;
 1408            if (sqrMag.RawValue == 0) return Zero;
 1409            FP dot = Dot(vector, onNormal);
 1410            return onNormal * (dot / sqrMag);
 411        }
 412
 413        /// <summary>
 414        /// 垂直向量(逆时针旋转 90 度)
 415        /// </summary>
 416        /// <param name="vector">输入向量</param>
 417        /// <returns>逆时针旋转 90 度后的向量</returns>
 418        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 419        public static FPVector2 Perpendicular(FPVector2 vector)
 1420            => new(-vector.Y, vector.X);
 421
 422        /// <summary>
 423        /// 旋转向量(逆时针)
 424        /// </summary>
 425        /// <param name="vector">要旋转的向量</param>
 426        /// <param name="radians">旋转角度(弧度)</param>
 427        /// <returns>旋转后的向量</returns>
 428        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 429        public static FPVector2 Rotate(FPVector2 vector, FP radians)
 430        {
 1431            FP sin = FP.Sin(radians);
 1432            FP cos = FP.Cos(radians);
 1433            return Rotate(vector, sin, cos);
 434        }
 435
 436        /// <summary>
 437        /// 旋转向量(传入已计算的 sin/cos)
 438        /// </summary>
 439        /// <param name="vector">要旋转的向量</param>
 440        /// <param name="sin">正弦值</param>
 441        /// <param name="cos">余弦值</param>
 442        /// <returns>旋转后的向量</returns>
 443        /// <remarks>
 444        /// 适用于需要多次旋转或 sin/cos 已预计算的场景,避免重复计算三角函数。
 445        /// </remarks>
 446        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 447        public static FPVector2 Rotate(FPVector2 vector, FP sin, FP cos)
 448        {
 1449            FP x = vector.X * cos - vector.Y * sin;
 1450            FP y = vector.X * sin + vector.Y * cos;
 1451            return new FPVector2(x, y);
 452        }
 453
 454        /// <summary>
 455        /// 分量逐元素相乘
 456        /// </summary>
 457        /// <param name="a">第一个向量</param>
 458        /// <param name="b">第二个向量</param>
 459        /// <returns>逐元素乘积</returns>
 460        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 461        public static FPVector2 Scale(FPVector2 a, FPVector2 b)
 1462            => new(a.X * b.X, a.Y * b.Y);
 463
 464        /// <summary>
 465        /// 取各分量的最小值
 466        /// </summary>
 467        /// <param name="a">第一个向量</param>
 468        /// <param name="b">第二个向量</param>
 469        /// <returns>各分量的最小值</returns>
 470        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 471        public static FPVector2 Min(FPVector2 a, FPVector2 b)
 1472            => new(FPMath.Min(a.X, b.X), FPMath.Min(a.Y, b.Y));
 473
 474        /// <summary>
 475        /// 取各分量的最大值
 476        /// </summary>
 477        /// <param name="a">第一个向量</param>
 478        /// <param name="b">第二个向量</param>
 479        /// <returns>各分量的最大值</returns>
 480        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 481        public static FPVector2 Max(FPVector2 a, FPVector2 b)
 1482            => new(FPMath.Max(a.X, b.X), FPMath.Max(a.Y, b.Y));
 483
 484        #region 几何工具函数(参考 FrameSync 设计)
 485
 486        /// <summary>
 487        /// 计算两个向量之间的夹角(弧度)- 优化版
 488        /// <para>范围:[0, π]</para>
 489        /// <para>优化:合并平方根计算,减少一次 Sqrt</para>
 490        /// </summary>
 491        /// <param name="a">第一个向量</param>
 492        /// <param name="b">第二个向量</param>
 493        /// <returns>夹角(弧度)</returns>
 494        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 495        public static FP Angle(FPVector2 a, FPVector2 b)
 496        {
 0497            FP dot = Dot(a, b);
 0498            FP sqrMagA = a.SqrMagnitude;
 0499            FP sqrMagB = b.SqrMagnitude;
 0500            if (sqrMagA.RawValue == 0 || sqrMagB.RawValue == 0) return FP.Zero;
 501
 502            // 优化:sqrt(|a|² * |b|²) = |a| * |b|,只需一次 Sqrt
 0503            FP cos = dot / FPMath.Sqrt(sqrMagA * sqrMagB);
 0504            cos = FPMath.Clamp(cos, -FP._1, FP._1);
 0505            return FP.Acos(cos);
 506        }
 507
 508        /// <summary>
 509        /// 计算有符号夹角(弧度)
 510        /// <para>范围:[-π, π],从 a 到 b 逆时针为正</para>
 511        /// </summary>
 512        /// <param name="a">起始向量</param>
 513        /// <param name="b">目标向量</param>
 514        /// <returns>有符号夹角(弧度)</returns>
 515        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 516        public static FP SignedAngle(FPVector2 a, FPVector2 b)
 517        {
 0518            FP angle = Angle(a, b);
 0519            FP cross = Cross(a, b);
 0520            return cross.RawValue < 0 ? -angle : angle;
 521        }
 522
 523        /// <summary>
 524        /// 向量插值(按距离)
 525        /// <para>将 vector 向 target 移动 maxDistanceDelta</para>
 526        /// </summary>
 527        /// <param name="vector">当前向量</param>
 528        /// <param name="target">目标向量</param>
 529        /// <param name="maxDistanceDelta">最大移动距离</param>
 530        /// <returns>移动后的向量</returns>
 531        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 532        public static FPVector2 MoveTowards(FPVector2 vector, FPVector2 target, FP maxDistanceDelta)
 533        {
 0534            FPVector2 diff = target - vector;
 0535            FP dist = diff.Magnitude;
 0536            if (dist.RawValue <= maxDistanceDelta.RawValue || dist.RawValue == 0)
 0537                return target;
 0538            return vector + diff / dist * maxDistanceDelta;
 539        }
 540
 541        /// <summary>
 542        /// 平滑阻尼(SmoothDamp)
 543        /// <para>FrameSync 风格平滑移动</para>
 544        /// </summary>
 545        /// <param name="current">当前位置</param>
 546        /// <param name="target">目标位置</param>
 547        /// <param name="velocity">当前速度(引用,会被修改)</param>
 548        /// <param name="smoothTime">平滑时间</param>
 549        /// <param name="deltaTime">时间增量</param>
 550        /// <returns>平滑后的位置</returns>
 551        public static FPVector2 SmoothDamp(FPVector2 current, FPVector2 target, ref FPVector2 velocity, FP smoothTime, F
 552        {
 0553            FP omega = 2 / smoothTime;
 0554            FP x = omega * deltaTime;
 0555            FP exp = FP._1 / (FP._1 + x + x * x * FP._0_50); // exp(-x) 近似
 556
 0557            FPVector2 change = velocity * deltaTime;
 0558            FPVector2 diff = current - target;
 559
 0560            FPVector2 temp = (velocity + omega * diff) * deltaTime;
 0561            velocity = (velocity - omega * temp) * exp;
 562
 0563            return target + (diff + change) * exp;
 564        }
 565
 566        /// <summary>
 567        /// 判断点是否在三角形内(重心坐标法)
 568        /// </summary>
 569        /// <param name="p">要判断的点</param>
 570        /// <param name="a">三角形顶点 A</param>
 571        /// <param name="b">三角形顶点 B</param>
 572        /// <param name="c">三角形顶点 C</param>
 573        /// <returns>如果点在三角形内(包括边上)返回 true</returns>
 574        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 575        public static bool PointInTriangle(FPVector2 p, FPVector2 a, FPVector2 b, FPVector2 c)
 576        {
 0577            FP d1 = Cross(b - a, p - a);
 0578            FP d2 = Cross(c - b, p - b);
 0579            FP d3 = Cross(a - c, p - c);
 580
 0581            bool hasNeg = (d1.RawValue < 0) || (d2.RawValue < 0) || (d3.RawValue < 0);
 0582            bool hasPos = (d1.RawValue > 0) || (d2.RawValue > 0) || (d3.RawValue > 0);
 583
 0584            return !(hasNeg && hasPos);
 585        }
 586
 587        #endregion
 588
 589        #endregion
 590
 591        #region 实例方法
 592
 593        /// <summary>
 594        /// 与另一个向量相等判断
 595        /// </summary>
 596        /// <param name="other">要比较的向量</param>
 597        /// <returns>相等返回 true</returns>
 598        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 599        public readonly bool Equals(FPVector2 other)
 0600            => this == other;
 601
 602        /// <summary>
 603        /// 与对象相等判断
 604        /// </summary>
 605        /// <param name="obj">要比较的对象</param>
 606        /// <returns>相等返回 true</returns>
 607        public override readonly bool Equals(object? obj)
 0608            => obj is FPVector2 other && Equals(other);
 609
 610        /// <summary>
 611        /// 获取哈希码
 612        /// </summary>
 613        /// <returns>哈希码</returns>
 614        public override readonly int GetHashCode()
 615        {
 0616            int hash = 17;
 0617            hash = hash * 31 + X.GetHashCode();
 0618            hash = hash * 31 + Y.GetHashCode();
 0619            return hash;
 620        }
 621
 622        /// <summary>
 623        /// 转换为字符串
 624        /// </summary>
 625        /// <returns>字符串表示 "(X, Y)"</returns>
 626        public override readonly string ToString()
 0627            => $"({X}, {Y})";
 628
 629        #endregion
 630    }
 631}

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/obj/Release/net8.0/SwizzleGenerator/Lattice.Generators.SwizzleGenerator/FPVector2.Swizzle.g.cs

File '/home/runner/work/Sideline/Sideline/godot/scripts/lattice/obj/Release/net8.0/SwizzleGenerator/Lattice.Generators.SwizzleGenerator/FPVector2.Swizzle.g.cs' does not exist (any more).

Methods/Properties

.ctor(Lattice.Math.FP,Lattice.Math.FP)
.ctor(System.Int32,System.Int32)
.ctor(Lattice.Math.FP)
get_Zero()
get_One()
get_Right()
get_Left()
get_Up()
get_Down()
get_MaxValue()
get_MinValue()
get_SqrMagnitude()
get_SqrMagnitudeFast()
get_Magnitude()
get_Normalized()
NormalizedWithMagnitude(Lattice.Math.FP&)
Normalize(Lattice.Math.FPVector2)
Normalize(Lattice.Math.FPVector2,Lattice.Math.FP&)
op_Addition(Lattice.Math.FPVector2,Lattice.Math.FPVector2)
op_Subtraction(Lattice.Math.FPVector2,Lattice.Math.FPVector2)
op_UnaryNegation(Lattice.Math.FPVector2)
op_Multiply(Lattice.Math.FPVector2,Lattice.Math.FP)
op_Multiply(Lattice.Math.FP,Lattice.Math.FPVector2)
op_Division(Lattice.Math.FPVector2,Lattice.Math.FP)
op_Equality(Lattice.Math.FPVector2,Lattice.Math.FPVector2)
op_Inequality(Lattice.Math.FPVector2,Lattice.Math.FPVector2)
Dot(Lattice.Math.FPVector2,Lattice.Math.FPVector2)
Cross(Lattice.Math.FPVector2,Lattice.Math.FPVector2)
Distance(Lattice.Math.FPVector2,Lattice.Math.FPVector2)
DistanceSquared(Lattice.Math.FPVector2,Lattice.Math.FPVector2)
Lerp(Lattice.Math.FPVector2,Lattice.Math.FPVector2,Lattice.Math.FP)
LerpUnclamped(Lattice.Math.FPVector2,Lattice.Math.FPVector2,Lattice.Math.FP)
ClampMagnitude(Lattice.Math.FPVector2,Lattice.Math.FP)
Reflect(Lattice.Math.FPVector2,Lattice.Math.FPVector2)
Project(Lattice.Math.FPVector2,Lattice.Math.FPVector2)
Perpendicular(Lattice.Math.FPVector2)
Rotate(Lattice.Math.FPVector2,Lattice.Math.FP)
Rotate(Lattice.Math.FPVector2,Lattice.Math.FP,Lattice.Math.FP)
Scale(Lattice.Math.FPVector2,Lattice.Math.FPVector2)
Min(Lattice.Math.FPVector2,Lattice.Math.FPVector2)
Max(Lattice.Math.FPVector2,Lattice.Math.FPVector2)
Angle(Lattice.Math.FPVector2,Lattice.Math.FPVector2)
SignedAngle(Lattice.Math.FPVector2,Lattice.Math.FPVector2)
MoveTowards(Lattice.Math.FPVector2,Lattice.Math.FPVector2,Lattice.Math.FP)
SmoothDamp(Lattice.Math.FPVector2,Lattice.Math.FPVector2,Lattice.Math.FPVector2&,Lattice.Math.FP,Lattice.Math.FP)
PointInTriangle(Lattice.Math.FPVector2,Lattice.Math.FPVector2,Lattice.Math.FPVector2,Lattice.Math.FPVector2)
Equals(Lattice.Math.FPVector2)
Equals(System.Object)
GetHashCode()
ToString()
get_XX()
get_XY()
get_XO()
get_YX()
get_YY()
get_YO()
get_OX()
get_OY()
get_OO()
get_XXX()
get_XXY()
get_XXO()
get_XYX()
get_XYY()
get_XYO()
get_XOX()
get_XOY()
get_XOO()
get_YXX()
get_YXY()
get_YXO()
get_YYX()
get_YYY()
get_YYO()
get_YOX()
get_YOY()
get_YOO()
get_OXX()
get_OXY()
get_OXO()
get_OYX()
get_OYY()
get_OYO()
get_OOX()
get_OOY()
get_OOO()