Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
ADRIREG committed Jan 4, 2025
1 parent 1e2f747 commit 8b13a19
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@


class Vector:
def __init__(self, x: float, y: float) -> None:
self.x = round(x, 2)
self.y = round(y, 2)
def __init__(self, x_coord: float, y_coord: float) -> None:
self.x_coord = round(x_coord, 2)
self.y_coord = round(y_coord, 2)

def __add__(self, other: "Vector") -> "Vector":
return Vector(self.x + other.x, self.y
+ other.y)
return Vector(self.x_coord + other.x_coord, self.y_coord
+ other.y_coord)

def __sub__(self, other: "Vector") -> "Vector":
return Vector(self.x - other.x, self.y
- other.y)
return Vector(self.x_coord - other.x_coord, self.y_coord
- other.y_coord)

def __mul__(self, other: Vector | float) -> Vector | float:
if isinstance(other, (int, float)):
return Vector(round(self.x * other, 2), round(self.y * other, 2))
return Vector(round(self.x_coord * other, 2),
round(self.y_coord * other, 2))
elif isinstance(other, Vector):
dot_product = (self.x * other.x + self.y
* other.y)
dot_product = (self.x_coord * other.x_coord + self.y_coord
* other.y_coord)
return dot_product
else:
raise TypeError(f"Unsupported operation for * with type "
Expand All @@ -29,19 +30,19 @@ def __mul__(self, other: Vector | float) -> Vector | float:
@classmethod
def create_vector_by_two_points(cls, start_point: tuple,
end_point: tuple) -> "Vector":
x = end_point[0] - start_point[0]
y = end_point[1] - start_point[1]
return cls(x, y)
x_coord_diff = end_point[0] - start_point[0]
y_coord_diff = end_point[1] - start_point[1]
return cls(x_coord_diff, y_coord_diff)

def get_length(self) -> float:
return math.sqrt(self.x ** 2 + self.y ** 2)
return math.sqrt(self.x_coord ** 2 + self.y_coord ** 2)

def get_normalized(self) -> "Vector":
length = self.get_length()
if length == 0:
raise ValueError("Cannot normalize a zero-length vector.")
else:
return Vector(self.x / length, self.y / length)
return Vector(self.x_coord / length, self.y_coord / length)

def angle_between(self, other: "Vector") -> float:
dot_product = self * other
Expand All @@ -58,8 +59,8 @@ def get_angle(self) -> float:

def rotate(self, degrees: float) -> "Vector":
radians = math.radians(degrees)
new_x = (self.x * math.cos(radians) - self.y
* math.sin(radians))
new_y = (self.x * math.sin(radians) + self.y
* math.cos(radians))
return Vector(new_x, new_y)
new_x_coord = (self.x_coord * math.cos(radians) - self.y_coord
* math.sin(radians))
new_y_coord = (self.x_coord * math.sin(radians) + self.y_coord
* math.cos(radians))
return Vector(new_x_coord, new_y_coord)

0 comments on commit 8b13a19

Please sign in to comment.