Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b5c2cb3

Browse files
committed
vector 2 td
1 parent 42027ae commit b5c2cb3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎01-data-model/vector2td.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from math import hypot
2+
3+
4+
class Vector:
5+
6+
def __init__(self, x=0, y=0):
7+
self.x = x
8+
self.y = y
9+
10+
def __repr__(self):
11+
return 'Vector(%r, %r)' % (self.x, self.y)
12+
13+
def __abs__(self):
14+
return hypot(self.x, self.y)
15+
16+
def __bool__(self):
17+
return bool(abs(self))
18+
19+
def __add__(self, other):
20+
x = self.x + other.x
21+
y = self.y + other.y
22+
return Vector(x, y)
23+
24+
def __mul__(self, scalar):
25+
return Vector(self.x * scalar, self.y * scalar)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /