python-lottie  0.7.0+dev66cafb9
A framework to work with lottie files and telegram animated stickers (tgs)
nvector.py
Go to the documentation of this file.
1 import operator
2 import math
3 
4 
5 def vop(op, a, b):
6  return list(map(op, a, b))
7 
8 
9 class NVector():
10  def __init__(self, *components):
11  self.componentscomponents = list(components)
12 
13  def __str__(self):
14  return str(self.componentscomponents)
15 
16  def __repr__(self):
17  return "NVector(%s)" % ", ".join(map(str, self))
18 
19  def __len__(self):
20  return len(self.componentscomponents)
21 
22  def to_list(self):
23  return list(self.componentscomponents)
24 
25  def __add__(self, other):
26  return type(self)(*vop(operator.add, self.componentscomponents, other.components))
27 
28  def __sub__(self, other):
29  return type(self)(*vop(operator.sub, self.componentscomponents, other.components))
30 
31  def __mul__(self, scalar):
32  if isinstance(scalar, NVector):
33  return type(self)(*vop(operator.mul, self.componentscomponents, scalar.components))
34  return type(self)(*(c * scalar for c in self.componentscomponents))
35 
36  def __truediv__(self, scalar):
37  return type(self)(*(c / scalar for c in self.componentscomponents))
38 
39  def __iadd__(self, other):
40  self.componentscomponents = vop(operator.add, self.componentscomponents, other.components)
41  return self
42 
43  def __isub__(self, other):
44  self.componentscomponents = vop(operator.sub, self.componentscomponents, other.components)
45  return self
46 
47  def __imul__(self, scalar):
48  if isinstance(scalar, NVector):
49  self.componentscomponents = vop(operator.mul, self.componentscomponents, scalar.components)
50  else:
51  self.componentscomponents = [c * scalar for c in self.componentscomponents]
52  return self
53 
54  def __itruediv__(self, scalar):
55  self.componentscomponents = [c / scalar for c in self.componentscomponents]
56  return self
57 
58  def __neg__(self):
59  return type(self)(*(-c for c in self.componentscomponents))
60 
61  def __getitem__(self, key):
62  if isinstance(key, slice):
63  return NVector(*self.componentscomponents[key])
64  return self.componentscomponents[key]
65 
66  def __setitem__(self, key, value):
67  self.componentscomponents[key] = value
68 
69  def __eq__(self, other):
70  return self.componentscomponents == other.components
71 
72  def __abs__(self):
73  return type(self)(*(abs(c) for c in self.componentscomponents))
74 
75  @property
76  def length(self) -> float:
77  return math.hypot(*self.componentscomponents)
78 
79  def dot(self, other):
80  return sum(map(operator.mul, self.componentscomponents, other.components))
81 
82  def clone(self):
83  return NVector(*self.componentscomponents)
84 
85  def lerp(self, other, t):
86  return self * (1-t) + other * t
87 
88  @property
89  def x(self):
90  return self.componentscomponents[0]
91 
92  @x.setter
93  def x(self, v):
94  self.componentscomponents[0] = v
95 
96  @property
97  def y(self):
98  return self.componentscomponents[1]
99 
100  @y.setter
101  def y(self, v):
102  self.componentscomponents[1] = v
103 
104  @property
105  def z(self):
106  return self.componentscomponents[2]
107 
108  @z.setter
109  def z(self, v):
110  self.componentscomponents[2] = v
111 
112  def element_scaled(self, other):
113  return type(self)(*vop(operator.mul, self.componentscomponents, other.components))
114 
115  def cross(self, other):
116  """
117  @pre len(self) == len(other) == 3
118  """
119  a = self
120  b = other
121  return type(self)(
122  a[1] * b[2] - a[2] * b[1],
123  a[2] * b[0] - a[0] * b[2],
124  a[0] * b[1] - a[1] * b[0],
125  )
126 
127  @property
128  def polar_angle(self):
129  """
130  @pre len(self) == 2
131  """
132  return math.atan2(self.yyy, self.xxx)
133 
134 
135 def Point(x, y):
136  return NVector(x, y)
137 
138 
139 def Size(x, y):
140  return NVector(x, y)
141 
142 
143 def Point3D(x, y, z):
144  return NVector(x, y, z)
145 
146 
147 def PolarVector(length, theta):
148  return NVector(length * math.cos(theta), length * math.sin(theta))
def dot(self, other)
Definition: nvector.py:79
def __abs__(self)
Definition: nvector.py:72
def __len__(self)
Definition: nvector.py:19
def __iadd__(self, other)
Definition: nvector.py:39
def lerp(self, other, t)
Definition: nvector.py:85
def __itruediv__(self, scalar)
Definition: nvector.py:54
def element_scaled(self, other)
Definition: nvector.py:112
def __str__(self)
Definition: nvector.py:13
def to_list(self)
Definition: nvector.py:22
def __getitem__(self, key)
Definition: nvector.py:61
float length(self)
Definition: nvector.py:76
def __init__(self, *components)
Definition: nvector.py:10
def __sub__(self, other)
Definition: nvector.py:28
def __setitem__(self, key, value)
Definition: nvector.py:66
def __add__(self, other)
Definition: nvector.py:25
def __mul__(self, scalar)
Definition: nvector.py:31
def __eq__(self, other)
Definition: nvector.py:69
def x(self, v)
Definition: nvector.py:93
def y(self, v)
Definition: nvector.py:101
def __imul__(self, scalar)
Definition: nvector.py:47
def __truediv__(self, scalar)
Definition: nvector.py:36
def __repr__(self)
Definition: nvector.py:16
def __neg__(self)
Definition: nvector.py:58
def polar_angle(self)
Definition: nvector.py:128
def __isub__(self, other)
Definition: nvector.py:43
def cross(self, other)
Definition: nvector.py:115
def Point3D(x, y, z)
Definition: nvector.py:143
def Point(x, y)
Definition: nvector.py:135
def Size(x, y)
Definition: nvector.py:139
def PolarVector(length, theta)
Definition: nvector.py:147
def vop(op, a, b)
Definition: nvector.py:5