mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
add vec2.angle
This commit is contained in:
parent
9179c31d6a
commit
dcf51ceff6
@ -19,6 +19,16 @@ class vec2(_StructLike['vec2']):
|
|||||||
def rotate(self, radians: float) -> vec2: ...
|
def rotate(self, radians: float) -> vec2: ...
|
||||||
def rotate_(self, radians: float) -> None: ...
|
def rotate_(self, radians: float) -> None: ...
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def angle(__from: vec2, __to: vec2) -> float:
|
||||||
|
"""Returns the angle in radians between vectors `from` and `to`.
|
||||||
|
|
||||||
|
The result range is `[-pi, pi]`.
|
||||||
|
|
||||||
|
+ if y axis is top to bottom, positive value means clockwise
|
||||||
|
+ if y axis is bottom to top, positive value means counter-clockwise
|
||||||
|
"""
|
||||||
|
|
||||||
class vec3(_StructLike['vec3']):
|
class vec3(_StructLike['vec3']):
|
||||||
x: float
|
x: float
|
||||||
y: float
|
y: float
|
||||||
|
@ -55,6 +55,16 @@ namespace pkpy{
|
|||||||
return VAR(Tuple({ VAR(self.x), VAR(self.y) }));
|
return VAR(Tuple({ VAR(self.x), VAR(self.y) }));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
vm->bind(type, "angle(__from: vec2, __to: vec2) -> float", [](VM* vm, ArgsView args){
|
||||||
|
PyVec2 __from = CAST(PyVec2, args[0]);
|
||||||
|
PyVec2 __to = CAST(PyVec2, args[1]);
|
||||||
|
float val = atan2f(__to.y, __to.x) - atan2f(__from.y, __from.x);
|
||||||
|
const float PI = 3.1415926535897932384f;
|
||||||
|
if(val > PI) val -= 2*PI;
|
||||||
|
if(val < -PI) val += 2*PI;
|
||||||
|
return VAR(val);
|
||||||
|
});
|
||||||
|
|
||||||
vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
|
vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
|
||||||
PyVec2& self = _CAST(PyVec2&, obj);
|
PyVec2& self = _CAST(PyVec2&, obj);
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user