From d0771ef6dc1b8e78407c3f45343223bce2fdd40e Mon Sep 17 00:00:00 2001 From: Teodora Iulia Craciun Date: Thu, 13 Aug 2026 23:11:22 +0200 Subject: [PATCH] calculate the dispalacement and the Euclidian Norm --- src/hello_world/__init__.py | 49 +++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/src/hello_world/__init__.py b/src/hello_world/__init__.py index ec0a87d..5ecf5d9 100644 --- a/src/hello_world/__init__.py +++ b/src/hello_world/__init__.py @@ -1,12 +1,51 @@ import math +from typing import NamedTuple +class Vector(NamedTuple): + x: float + y: float + +current_pos = Vector(3,2) +target_pos = Vector(7,5) +zero_vector = Vector(0,0) def main() -> None: - print("Hello from hello-world!") - print(lenght(3,4)) + print("Hello Teodora!") -def lenght(x,y): - squared_x = x*x - squared_y = y*y + distance = displacementVector(current_pos, target_pos) + print(distance) + + lengthOfDistanceVector = length(distance) + print(lengthOfDistanceVector) + + direction = safe_normalize(distance, 0.001) + print(direction) + +## calculate the displacement from current position to target position + + +def displacementVector(current_pos, target_pos): + displacementVector = Vector(target_pos.x - current_pos.x, target_pos.y - current_pos.y) + return displacementVector + +## distance between two positions +# Euclidian norm + +def length(vector: Vector) -> float: + squared_x = vector.x*vector.x + squared_y = vector.y*vector.y squared_x + squared_y return math.sqrt(squared_x + squared_y) + +# safe normalisation +# The minimum-distance check also prevents division by zero. + +def safe_normalize(vector: Vector, minimum_distance: float) -> Vector: + vector_length = length(vector) + + if vector_length < minimum_distance: + return zero_vector + + einheitsVector = Vector(vector.x / vector_length, vector.y / vector_length) + return einheitsVector +