blueloveTH 2 سال پیش
والد
کامیت
d4bde2fcdd
3فایلهای تغییر یافته به همراه19 افزوده شده و 0 حذف شده
  1. 1 0
      include/typings/linalg.pyi
  2. 12 0
      src/linalg.cpp
  3. 6 0
      tests/80_linalg.py

+ 1 - 0
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:

+ 12 - 0
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__, -)

+ 6 - 0
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--------------------------------------------------------------------
 # 生成随机测试目标