Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop1 #1113

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 63 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,64 @@
import math
from typing import Union, Tuple


class Vector:
# write your code here
pass
def __init__(self, point_x: float, point_y: float) -> None:
self.point_x = round(point_x, 2)
self.point_y = round(point_y, 2)

def __add__(self, other: "Vector") -> "Vector":
return Vector(self.point_x + other.point_x, self.point_y + other.point_y)

def __sub__(self, other: "Vector") -> "Vector":
return Vector(self.point_x - other.point_x, self.point_y - other.point_y)

def __mul__(self, other: Union[float, "Vector"]) -> Union[float, "Vector"]:
if isinstance(other, (int, float)):
return Vector(round(self.point_x * other, 2), round(self.point_y * other, 2))
elif isinstance(other, Vector):
dot_product = self.point_x * other.point_x + self.point_y * other.point_y
return dot_product
else:
raise TypeError("Multiplication with unsupported type")

@classmethod
def create_vector_by_two_points(
cls, start_point: Tuple[float, float], end_point: Tuple[float, float]
) -> "Vector":
vector_x = end_point[0] - start_point[0]
vector_y = end_point[1] - start_point[1]
return cls(vector_x, vector_y)

def get_length(self) -> float:
return math.sqrt(self.point_x ** 2 + self.point_y ** 2)

def get_normalized(self) -> "Vector":
length = self.get_length()
if length == 0:
return Vector(0, 0)
return Vector(self.point_x / length, self.point_y / length)

def angle_between(self, other: "Vector") -> int:
dot_product = self * other
length_product = self.get_length() * other.get_length()
if length_product == 0:
return 0
cos_theta = dot_product / length_product
cos_theta = max(min(cos_theta, 1), -1)
angle_radians = math.acos(cos_theta)
return round(math.degrees(angle_radians))

def get_angle(self) -> int:
y_axis_vector = Vector(0, 1)
return self.angle_between(y_axis_vector)

def rotate(self, degrees: int) -> "Vector":
radians = math.radians(degrees)
new_x = (
self.point_x * math.cos(radians) - self.point_y * math.sin(radians)
)
new_y = (
self.point_x * math.sin(radians) + self.point_y * math.cos(radians)
)
return Vector(new_x, new_y)
Loading