calculate the dispalacement and the Euclidian Norm

This commit is contained in:
Teodora Iulia Craciun 2026-08-13 23:11:22 +02:00
parent d6b2f8df82
commit d0771ef6dc

View File

@ -1,12 +1,51 @@
import math 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: def main() -> None:
print("Hello from hello-world!") print("Hello Teodora!")
print(lenght(3,4))
def lenght(x,y): distance = displacementVector(current_pos, target_pos)
squared_x = x*x print(distance)
squared_y = y*y
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 squared_x + squared_y
return math.sqrt(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