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

TP3: fix exo 3 + correction partielle #10

Merged
merged 12 commits into from Mar 26, 2024
23 changes: 23 additions & 0 deletions TP3/Correction/person.py
@@ -0,0 +1,23 @@
"""Representation of a person."""


class Person:
def __init__(self, first_name: str, last_name: str, age: int, email: str, phone: str) -> None:
self.first_name = first_name.capitalize()
self.last_name = last_name.upper()
self.age = age
self.email = email
self.phone = phone


def main():
john = Person("John", "Doe", 30, "john.doe@gmail.com", "+33612345678")
print(f"First name: {john.first_name}")
print(f"Last name: {john.last_name}")
print(f"Age: {john.age}")
print(f"Email: {john.email}")
print(f"Phone: {john.phone}")


if __name__ == "__main__":
main()
101 changes: 101 additions & 0 deletions TP3/Correction/vectors.py
@@ -0,0 +1,101 @@
"""Manipulation of 2D-vectors.

Note :
``from __future__ import annotations`` est nécessaire si l'on veut pouvoir utiliser une classe
en tant qu'annotation de type dans cette même classe ! Comme dans l'exemple suivant :
class Point:
@classmethod
def origin(cls) -> Point: # "Error: Point not defined" serait levé sans l'utilisation de ``from __future__ import annotations``

"""

from __future__ import annotations
LoicRiegel marked this conversation as resolved.
Show resolved Hide resolved

import math


class Point:
def __init__(self, x: float, y: float) -> None:
self.x = x
self.y = y

@classmethod
def origin(cls) -> Point:
return Point(0, 0)

def __str__(self) -> str:
return f"Point({self.x}, {self.y})"


class Vector:
def __init__(self, x_coordinate: float, y_coordinate: float) -> None:
self.x = x_coordinate
self.y = y_coordinate

@classmethod
def from_points(cls, tail: Point, head: Point) -> Vector:
return Vector(head.x - tail.x, head.y - head.y)

@property
def magnitude(self) -> float:
LoicRiegel marked this conversation as resolved.
Show resolved Hide resolved
return math.sqrt(self.x**2 + self.y**2)
LoicRiegel marked this conversation as resolved.
Show resolved Hide resolved

def dot_prod(self, other: Vector) -> float:
return self.x * other.x + self.y * other.y

def __str__(self) -> str:
return f"Vector({self.x}, {self.y})"

def __add__(self, other: Vector) -> Vector:
return Vector(self.x + other.x, self.y + other.y)


def main():
origin = Point.origin()
point_A = Point(-2, -1)
point_B = Point(3, 7)

print(f"Point O: x={origin.x} y={origin.y}")
print(f"Point A: x={point_A.x} y={point_A.y}")
print(f"Point B: x={point_B.x} y={point_B.y}")


def main_2():
v = Vector(1, 1)
v2 = Vector(3, -2)

print(f"{v.magnitude = }")
print(f"{v.dot_prod(v2) = }")
print(f"{v2.dot_prod(v) = }")


def main_3():
v = Vector(1, 1)
v2 = Vector(3, -2)

print(f"{v + v2 =}")
print(f"{-v =}")
print(f"{- (-v) =}")
print(f"{v - v2 =}")
print(f"{v * 2 =}")
print(f"{v2 * (-10) =}")


def main_4():
origin = Point.origin()
point_A = Point(-2, -1)
point_B = Point(3, 7)

vector_AB = Vector.from_points(point_A, point_B)
vector_OA = Vector.from_points(origin, point_A)
vector_AB = Vector.from_points(point_A, point_B)

print(f"Vector OA: dx={vector_OA.x} dy={vector_OA.y}")
print(f"Vector AB: dx={vector_AB.x} dy={vector_AB.y}")


if __name__ == "__main__":
main()
# main_2()
# main_3()
# main_4()
9 changes: 6 additions & 3 deletions TP3/README.md
Expand Up @@ -48,8 +48,9 @@ Créez une classe `Point` et une classe `Vector` de manière à représenter des
Vérifier que la fonction `main` s'exécute sans erreurs et que les éléments affichés sont mathématiquements corrects.


#### 3.2 Produit scalaire
#### 3.2 Produit scalaire et norme

Ajouter une méthode qui calcule la norme d'un vecteur.
Ajouter une méthode qui calcule le produit scalaire de deux vecteurs.


Expand All @@ -72,16 +73,18 @@ Pour les annotations de types, on pourra utiliser le type `Self`
```py
from typing import Self
```
ou bien utiliser `Vector` directement (dans ce cas, il sera nécessaire d'importer `from __future__ import annotations`).

#### 3.4 Constructeur alternatif

On souhaite pouvoir créer un vecteur en donnant à l'initialisation seulement le point d'arrivé. Dans ce cas, le point de départ sera l'origine.
On souhaite que ce code fonctionne :
```py
vector_OB = Vector.from_origin(point_B)
origin = Point.origin() # Point(0, 0)
vector_OB = Vector.from_points(point_A, point_B)
```

En utilisant une **méthode de class**, implémenter la méthode `from_origin` sur la classe `Vector`.
En utilisant une **méthode de class**, implémenter la méthode `origin` sur la classe `Point` et `from_points` sur la classe `Vector`.


## Exercice 4
Expand Down
47 changes: 26 additions & 21 deletions TP3/vectors.py
Expand Up @@ -4,42 +4,47 @@


def main():
origin = Point(0, 0)
origin = Point.origin()
point_A = Point(-2, -1)
point_B = Point(3, 7)

print(f"Point O: x={origin.x} y={origin.y}")
print(f"Point A: x={point_A.x} y={point_A.y}")

vector_OA = Vector(origin, point_A)
vector_AB = Vector(point_A, point_B)

print(f"Vector OA: dx={vector_OA.x} dy={vector_OA.y}")
print(f"Vector AB: dx={vector_AB.x} dy={vector_AB.y}")
print(f"Point B: x={point_B.x} y={point_B.y}")


def main_2():
v = Vector(1, 2)
v2 = Vector(1, 0)
print(v.dot_prod(v2))
print(v2.dot_prod(v))
v = Vector(1, 1)
v2 = Vector(3, -2)

print(f"{v.magnitude() = }") # or v.magnitude if using the @property decorator
print(f"{v.dot_prod(v2) = }")
print(f"{v2.dot_prod(v) = }")


def main_3():
v = Vector(1, 2)
v2 = Vector(1, 0)
print(-v)
print(-(-v))
print(v + v2)
print(v - v2)
print(v * 3)
print(v2 * -10)
v = Vector(1, 1)
v2 = Vector(3, -2)

print(f"{v + v2 =}")
print(f"{-v =}")
print(f"{- (-v) =}")
print(f"{v - v2 =}")
print(f"{v * 2 =}")
print(f"{v2 * (-10) =}")


def main_4():
origin = Point.origin()
point_A = Point(-2, -1)
point_B = Point(3, 7)
vector_OB = Vector.from_origin(point_B)
print(vector_OB)

vector_AB = Vector.from_points(point_A, point_B)
vector_OA = Vector.from_points(origin, point_A)
vector_AB = Vector.from_points(point_A, point_B)

print(f"Vector OA: dx={vector_OA.x} dy={vector_OA.y}")
print(f"Vector AB: dx={vector_AB.x} dy={vector_AB.y}")


if __name__ == "__main__":
Expand Down