MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / Vec2D

Class Vec2D

tools/python-3.11.9-amd64/Lib/turtle.py:237–279  ·  view source on GitHub ↗

A 2 dimensional vector class, used as a helper class for implementing turtle graphics. May be useful for turtle graphics programs also. Derived from tuple, so a vector is a tuple! Provides (for a, b vectors, k number): a+b vector addition a-b vector subtraction

Source from the content-addressed store, hash-verified

235
236
237class Vec2D(tuple):
238 """A 2 dimensional vector class, used as a helper class
239 for implementing turtle graphics.
240 May be useful for turtle graphics programs also.
241 Derived from tuple, so a vector is a tuple!
242
243 Provides (for a, b vectors, k number):
244 a+b vector addition
245 a-b vector subtraction
246 a*b inner product
247 k*a and a*k multiplication with scalar
248 |a| absolute value of a
249 a.rotate(angle) rotation
250 """
251 def __new__(cls, x, y):
252 return tuple.__new__(cls, (x, y))
253 def __add__(self, other):
254 return Vec2D(self[0]+other[0], self[1]+other[1])
255 def __mul__(self, other):
256 if isinstance(other, Vec2D):
257 return self[0]*other[0]+self[1]*other[1]
258 return Vec2D(self[0]*other, self[1]*other)
259 def __rmul__(self, other):
260 if isinstance(other, int) or isinstance(other, float):
261 return Vec2D(self[0]*other, self[1]*other)
262 return NotImplemented
263 def __sub__(self, other):
264 return Vec2D(self[0]-other[0], self[1]-other[1])
265 def __neg__(self):
266 return Vec2D(-self[0], -self[1])
267 def __abs__(self):
268 return math.hypot(*self)
269 def rotate(self, angle):
270 """rotate self counterclockwise by angle
271 """
272 perp = Vec2D(-self[1], self[0])
273 angle = math.radians(angle)
274 c, s = math.cos(angle), math.sin(angle)
275 return Vec2D(self[0]*c+perp[0]*s, self[1]*c+perp[1]*s)
276 def __getnewargs__(self):
277 return (self[0], self[1])
278 def __repr__(self):
279 return "(%.2f,%.2f)" % self
280
281
282##############################################################################

Callers 14

__add__Method · 0.85
__mul__Method · 0.85
__rmul__Method · 0.85
__sub__Method · 0.85
__neg__Method · 0.85
rotateMethod · 0.85
TNavigatorClass · 0.85
resetMethod · 0.85
gotoMethod · 0.85
setxMethod · 0.85
setyMethod · 0.85
distanceMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected