Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
DavPilot committed Jan 8, 2025
1 parent 18ef448 commit 01b2700
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ def __sub__(self, other: "Vector") -> "Vector":
def __mul__(self, other: Union[float, "Vector"]) -> Union[float, "Vector"]:
if isinstance(other, (int, float)):
return Vector(self.coord_x * other, self.coord_y * other)
dot_product = self.coord_x * other.coord_x + self.coord_y * other.coord_y

dot_product = (
self.coord_x * other.coord_x + self.coord_y * other.coord_y
)
return round(dot_product, 4)

@classmethod
def create_vector_by_two_points(
cls, start: tuple[float, float], end: tuple[float, float]
cls, start: tuple[float, float], end: tuple[float, float]
) -> "Vector":
coord_x_diff = end[0] - start[0]
coord_y_diff = end[1] - start[1]
Expand All @@ -34,12 +37,14 @@ def create_vector_by_two_points(
def get_length(self) -> float:
coord_x_squared = self.coord_x ** 2
coord_y_squared = self.coord_y ** 2
return math.sqrt(coord_x_squared + coord_y_squared)
length = math.sqrt(coord_x_squared + coord_y_squared)
return length

def get_normalized(self) -> "Vector":
length = self.get_length()
if length == 0:
raise ValueError("Cannot normalize a zero-length vector.")

coord_x_normalized = self.coord_x / length
coord_y_normalized = self.coord_y / length
return Vector(coord_x_normalized, coord_y_normalized)
Expand All @@ -58,6 +63,13 @@ def rotate(self, degrees: int) -> "Vector":
radians = math.radians(degrees)
cos_angle = math.cos(radians)
sin_angle = math.sin(radians)
coord_x_rotated = self.coord_x * cos_angle - self.coord_y * sin_angle
coord_y_rotated = self.coord_x * sin_angle + self.coord_y * cos_angle

coord_x_rotated = (
self.coord_x * cos_angle - self.coord_y * sin_angle
)
coord_y_rotated = (
self.coord_x * sin_angle + self.coord_y * cos_angle
)

return Vector(coord_x_rotated, coord_y_rotated)

0 comments on commit 01b2700

Please sign in to comment.