Vector2
A 2D vector with x and y components.
Vectors represent directions and displacements in the layout plane. They are used for translations, port directions, and geometric calculations. Vectors are immutable -- all methods return new Vector2 instances.
v = Vector2(3, 4)
v.length() # 5.0
v.normalize() # Vector2(0.6, 0.8)
# Unit vectors and angle construction
right = Vector2.unit_x() # Vector2(1, 0)
diagonal = Vector2.from_angle(45) # Vector2(0.707..., 0.707...)Attributes
attributexfloatX component.
attributeyfloatY component.
Static methods
funcunit_x() -> Vector2Return the unit vector along the X axis: Vector2(1, 0).
Returns
Vector2funcunit_y() -> Vector2Return the unit vector along the Y axis: Vector2(0, 1).
Returns
Vector2funcfrom_angle(angle_deg) -> Vector2Create a unit vector from an angle in degrees. 0 degrees points along +X, 90 degrees points along +Y.
paramangle_degfloatAngle in degrees (counter-clockwise from +X).
Returns
Vector2A unit vector pointing in the given direction.
Methods
func__init__(x=0.0, y=0.0) -> NoneCreate a new vector.
paramxfloat= 0.0X component.
paramyfloat= 0.0Y component.
Returns
Nonefunclength() -> floatReturn the Euclidean length (magnitude) of the vector.
Returns
floatfuncnormalize() -> Vector2Return a unit vector in the same direction. The original vector must be non-zero.
Returns
Vector2A new vector with length 1.
funcdot(other) -> floatCompute the dot product with another vector.
paramotherVector2The other vector.
Returns
floatThe scalar dot product.
funcperpendicular() -> Vector2Return a vector perpendicular to this one (rotated 90 degrees counter-clockwise).
Returns
Vector2funcrotate(angle_deg) -> Vector2Rotate the vector by the given angle.
paramangle_degfloatRotation angle in degrees (counter-clockwise).
Returns
Vector2A new rotated vector.
Supported operations
Addition: v1 + v2 returns a new Vector2 that is the component-wise sum.
Vector2(1, 2) + Vector2(3, 4) # Vector2(4, 6)Subtraction: v1 - v2 returns a new Vector2 that is the component-wise difference.
Vector2(5, 7) - Vector2(1, 2) # Vector2(4, 5)Scalar multiplication: v * scalar or scalar * v scales both components.
Vector2(3, 4) * 2 # Vector2(6, 8)
2 * Vector2(3, 4) # Vector2(6, 8)Negation: -v returns a vector pointing in the opposite direction.
-Vector2(3, -4) # Vector2(-3, 4)