summaryrefslogtreecommitdiff
path: root/py03/ex2/ft_coordinate_system.py
blob: f0daea164a22485e9eacaaade2d948c4dd0fda9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3

import math


class InvalidSyntax(Exception):
    def __init__(self, message="Invalid syntax"):
        self.message = message
        super().__init__(self.message)


def get_player_pos():
    while True:
        try:
            pos = input("Enter new coordinates"
                        "as floats in format 'x, y, z': ")
            split_pos = pos.split(',')
            if len(split_pos) != 3:
                raise InvalidSyntax()
            try:
                for element in split_pos:
                    float(element)
            except ValueError:
                print(f"Error on parameter '{element}':"
                      f"could not convert string to float '{element}'")
            # break because of True loop
            else:
                break
        except InvalidSyntax as err:
            print(err)
    return pos


def print_info_pos_one(pos):
    split_pos: tuple[float, ...] = tuple((float(x) for x in pos.split(",")))
    (x1, y1, z1) = split_pos
    print(f"It includes: X={x1}, Y={y1}, Z={z1}")
    x2 = y2 = z2 = 0
    distance_to_center = math.sqrt((x2-x1)**2 + (y2-y1)**2 + (z2-z1)**2)
    print(f"Distance to the center: {round(distance_to_center, 4)}")


def compute_distance_btw_pos(str_one, str_two):
    pos_one: tuple[float, ...] = tuple((float(x) for x in str_one.split(",")))
    (x1, y1, z1) = pos_one
    pos_two: tuple[float, ...] = tuple((float(x) for x in str_two.split(",")))
    (x2, y2, z2) = pos_two
    distance_btw_two = math.sqrt((x2-x1)**2 + (y2-y1)**2 + (z2-z1)**2)
    print(f"Distance between the two sets of coordinates: "
          f"{round(distance_btw_two, 4)}")


if __name__ == "__main__":
    print("=== Game Coordinate System ===")
    pos_one = get_player_pos()
    print(f"Got a first tuple: ({pos_one})")
    print_info_pos_one(pos_one)
    print()
    print("Get a second set of coordinates")
    pos_two = get_player_pos()
    compute_distance_btw_pos(pos_one, pos_two)