diff --git a/include/typings/linalg.pyi b/include/typings/linalg.pyi index 7a412843..2cc0c62f 100644 --- a/include/typings/linalg.pyi +++ b/include/typings/linalg.pyi @@ -17,6 +17,7 @@ class vec2: def length_squared(self) -> float: ... def normalize(self) -> vec2: ... def rotate(self, radians: float) -> vec2: ... + def rotate_(self, radians: float) -> None: ... def addr(self) -> float_p: ... class vec3: diff --git a/src/linalg.cpp b/src/linalg.cpp index 439cc8d6..7c0cbae9 100644 --- a/src/linalg.cpp +++ b/src/linalg.cpp @@ -83,6 +83,18 @@ namespace pkpy{ return VAR(self); }); + vm->bind_method<1>(type, "rotate_", [](VM* vm, ArgsView args){ + Vec2& self = _CAST(PyVec2&, args[0]); + float radian = CAST(f64, args[1]); + float cr = cosf(radian); + float sr = sinf(radian); + Mat3x3 rotate(cr, -sr, 0.0f, + sr, cr, 0.0f, + 0.0f, 0.0f, 1.0f); + self = rotate.transform_vector(self); + return vm->None; + }); + BIND_VEC_ADDR(2) BIND_VEC_VEC_OP(2, __add__, +) BIND_VEC_VEC_OP(2, __sub__, -) diff --git a/tests/80_linalg.py b/tests/80_linalg.py index d40c8639..cbfd2646 100644 --- a/tests/80_linalg.py +++ b/tests/80_linalg.py @@ -38,6 +38,12 @@ radians = random.uniform(-10*math.pi, 10*math.pi) test_vec2_copy = rotated_vec2(test_vec2_copy, radians) assert test_vec2.rotate(radians).__dict__ == test_vec2_copy.__dict__ +# test rotate_ +a = test_vec2.rotate(0.7) +assert a is not test_vec2 +b = test_vec2.rotate_(0.7) +assert b is None +assert a == test_vec2 # test vec3-------------------------------------------------------------------- # 生成随机测试目标