Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2@file 

3@brief Defines a segment 

4""" 

5 

6from .geometry_point import GeometryPoint, GeometryException 

7 

8 

9class GeometrySegment: 

10 """ 

11 two points 

12 """ 

13 __slots__ = ["_a", "_b"] 

14 

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 

26 

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) 

33 

34 def __neq__(self, x): 

35 """ 

36 different 

37 """ 

38 return not self.__eq__(x) 

39 

40 def __len__(self): 

41 """ 

42 dimension 

43 """ 

44 return len(self._a) 

45 

46 def __str__(self): 

47 """ 

48 usual 

49 """ 

50 return "%s --> %s" % (str(self._a), str(self._b)) 

51 

52 def __imul__(self, k): 

53 """ 

54 multiplication 

55 """ 

56 self._a *= k 

57 self._b *= k 

58 return self 

59 

60 def vector(self): 

61 """ 

62 @return the directional vector 

63 """ 

64 return self._b - self._a 

65 

66 def equation(self): 

67 """ 

68 dimension 2 only 

69 

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 

80 

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 

87 

88 def norm2(self): 

89 """ 

90 @return the norm2 

91 """ 

92 v = self.vector() 

93 return v.scalar(v) 

94 

95 def reverse(self): 

96 """ 

97 switch extremities 

98 """ 

99 x = self._a 

100 self._a = self._b 

101 self._b = x 

102 

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()