Coverage for src/ensae_teaching_cs/special/geometry_segment.py: 70%
43 statements
« prev ^ index » next coverage.py v7.1.0, created at 2023-04-28 06:23 +0200
« prev ^ index » next coverage.py v7.1.0, created at 2023-04-28 06:23 +0200
1"""
2@file
3@brief Defines a segment
4"""
6from .geometry_point import GeometryPoint, GeometryException
9class GeometrySegment:
10 """
11 two points
12 """
13 __slots__ = ["_a", "_b"]
15 def __init__(self, a, b):
16 """
17 constructor
18 @param a first extremity
19 @param b second extremity
20 """
21 if len(a) != len(b):
22 raise GeometryException(
23 "extremities don't share the same dimension")
24 self._a = a
25 self._b = b
27 def __eq__(self, x):
28 """
29 equal
30 """
31 return (self._a == x._a and self._b == x._b) or \
32 (self._b == x._a and self._a == x._b)
34 def __neq__(self, x):
35 """
36 different
37 """
38 return not self.__eq__(x)
40 def __len__(self):
41 """
42 dimension
43 """
44 return len(self._a)
46 def __str__(self):
47 """
48 usual
49 """
50 return f"{str(self._a)} --> {str(self._b)}"
52 def __imul__(self, k):
53 """
54 multiplication
55 """
56 self._a *= k
57 self._b *= k
58 return self
60 def vector(self):
61 """
62 @return the directional vector
63 """
64 return self._b - self._a
66 def equation(self):
67 """
68 dimension 2 only
70 @return a,b,c : ax + by +c
71 """
72 if len(self) != 2:
73 raise GeometryException(
74 "only able to define an equation in dimension 2")
75 vec = self.vector()
76 a = vec._x[1]
77 b = -vec._x[0]
78 c = - a * self._a._x[0] - b * self._a._x[1]
79 return a, b, c
81 def equation_eval(self, x):
82 """
83 @return the evaluation of the equation
84 """
85 a, b, c = self.equation()
86 return x.scalar(GeometryPoint(a, b)) + c
88 def norm2(self):
89 """
90 @return the norm2
91 """
92 v = self.vector()
93 return v.scalar(v)
95 def reverse(self):
96 """
97 switch extremities
98 """
99 x = self._a
100 self._a = self._b
101 self._b = x
103 def topdown(self):
104 """
105 the vector only points in the same semi-plan
106 """
107 if self._a > self._b:
108 self.reverse()