Struct Vector2
The part of pygame.math.Vector2 Gymnasium's renderers use: two float64 components and
RotateRad(double), whose treatment of angles within Epsilon of a quarter turn decides where
CartPole's pole is drawn.
public readonly record struct Vector2 : IEquatable<Vector2>
- Implements
- Inherited Members
- Extension Methods
Constructors
Vector2(double, double)
The part of pygame.math.Vector2 Gymnasium's renderers use: two float64 components and
RotateRad(double), whose treatment of angles within Epsilon of a quarter turn decides where
CartPole's pole is drawn.
public Vector2(double X, double Y)
Parameters
Fields
Epsilon
pygame's default comparison tolerance for vectors (Vector2().epsilon, 1e-6). Rotation uses it to snap
angles near 0°, 90°, 180° and 270° to exact quarter turns.
public const double Epsilon = 1E-06
Field Value
Properties
this[int]
v[0] or v[1], the components by index as Python unpacks them.
public double this[int index] { get; }
Parameters
indexint0 for x, 1 for y.
Property Value
Exceptions
- IndexOutOfRangeException
Thrown for any other index, as pygame raises
IndexError.
X
The x component.
public double X { get; init; }
Property Value
Y
The y component.
public double Y { get; init; }
Property Value
Methods
RotateRad(double)
v.rotate_rad(angle): the vector rotated counter-clockwise by angle radians, computed as
pygame-ce 2.5.8 computes it.
public Vector2 RotateRad(double angle)
Parameters
angledoubleThe angle in radians; NaN or an infinity gives a NaN vector.
Returns
- Vector2
The rotated vector.
Remarks
The angle is first reduced into [0, 2π) with the exact floating-point remainder by 2π (the double nearest
2π), adding 2π to a negative remainder. If the reduced angle lies within Epsilon of a
multiple of π/2 (tested as remainder(angle + ε, π/2) < 2ε), the rotation is an exact quarter turn
chosen by (int)((angle + ε) / (π/2)): 0 and 4 keep the vector, 1 gives (-y, x), 2 gives
(-x, -y), 3 gives (y, -x). Otherwise it is (cos·x - sin·y, sin·x + cos·y) with the sine and
cosine of the reduced angle, each product rounded separately (no fused multiply-add).
The snapping is visible: CartPole's pole drawn at θ = 1e-7 is exactly upright, not rotated by 1e-7. The sine and cosine are .NET's Sin(double) and Cos(double), which on the reference platform (Windows x64) come from the same C runtime pygame's are compiled against.
Exceptions
- InvalidOperationException
Never thrown for a reduced angle; it stands where pygame raises
RuntimeErrorfor an impossible quarter turn, so a broken reduction fails loudly instead of rotating wrongly.