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

Solution #1128

Open
wants to merge 1 commit 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
69 changes: 67 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,68 @@
from __future__ import annotations
import math


class Vector:
# write your code here
pass
def __init__(self, x_coordinate: float, y_coordinate: float) -> None:
self.x = round(x_coordinate, 2)
self.y = round(y_coordinate, 2)

def __add__(self, other_v: Vector) -> Vector:
return Vector(x_coordinate=self.x + other_v.x ,
y_coordinate=self.y + other_v.y)

def __sub__(self, other_v: Vector) -> Vector:
return Vector(x_coordinate=self.x - other_v.x,
y_coordinate=self.y - other_v.y)

def __mul__(self, other_v: Vector | float | int) -> Vector | float:
if isinstance(other_v, (float, int)):
return Vector(x_coordinate=self.x * other_v,
y_coordinate=self.y * other_v)
return self.x * other_v.x + self.y * other_v.y

@classmethod
def create_vector_by_two_points(cls, start_point: tuple[float, float],
end_point: tuple[float, float]) -> Vector:
return Vector(x_coordinate=end_point[0] - start_point[0],
y_coordinate=end_point[1] - start_point[1])

def get_length(self) -> float:
return (self.x ** 2 + self.y ** 2) ** 0.5

def get_normalized(self) -> Vector:
len_of_vector = self.get_length()
return Vector(x_coordinate=self.x / len_of_vector,
y_coordinate=self.y / len_of_vector)

def angle_between(self, other_v: Vector) -> int:
dot_product = self * other_v

self_len = self.get_length()
other_v_len = other_v.get_length()

cos_of_angle = dot_product / (self_len * other_v_len)
angle_rad = math.acos(cos_of_angle)
angle_deg = math.degrees(angle_rad)

return round(angle_deg)

def get_angle(self) -> int:
length = self.get_length()
if length == 0:
return 0

cos_angle = self.y / length
angle_rad = math.acos(cos_angle)
angle_deg = math.degrees(angle_rad)

return round(angle_deg)

def rotate(self, degrees: int) -> Vector:
angle_rad = math.radians(degrees)

new_x = self.x * math.cos(angle_rad) - self.y * math.sin(angle_rad)
new_y = self.x * math.sin(angle_rad) + self.y * math.cos(angle_rad)

return Vector(x_coordinate=new_x,
y_coordinate=new_y)
Loading