"""
Defines a segment
:githublink:`%|py|5`
"""
from .geometry_point import GeometryPoint, GeometryException
[docs]class GeometrySegment:
"""
two points
:githublink:`%|py|12`
"""
__slots__ = ["_a", "_b"]
[docs] def __init__(self, a, b):
"""
constructor
:param a: first extremity
:param b: second extremity
:githublink:`%|py|20`
"""
if len(a) != len(b):
raise GeometryException(
"extremities don't share the same dimension")
self._a = a
self._b = b
[docs] def __eq__(self, x):
"""
equal
:githublink:`%|py|30`
"""
return (self._a == x._a and self._b == x._b) or \
(self._b == x._a and self._a == x._b)
[docs] def __neq__(self, x):
"""
different
:githublink:`%|py|37`
"""
return not self.__eq__(x)
[docs] def __len__(self):
"""
dimension
:githublink:`%|py|43`
"""
return len(self._a)
[docs] def __str__(self):
"""
usual
:githublink:`%|py|49`
"""
return "%s --> %s" % (str(self._a), str(self._b))
[docs] def __imul__(self, k):
"""
multiplication
:githublink:`%|py|55`
"""
self._a *= k
self._b *= k
return self
[docs] def vector(self):
"""
:return: the directional vector
:githublink:`%|py|63`
"""
return self._b - self._a
[docs] def equation(self):
"""
dimension 2 only
:return: a,b,c : ax + by +c
:githublink:`%|py|71`
"""
if len(self) != 2:
raise GeometryException(
"only able to define an equation in dimension 2")
vec = self.vector()
a = vec._x[1]
b = -vec._x[0]
c = - a * self._a._x[0] - b * self._a._x[1]
return a, b, c
[docs] def equation_eval(self, x):
"""
:return: the evaluation of the equation
:githublink:`%|py|84`
"""
a, b, c = self.equation()
return x.scalar(GeometryPoint(a, b)) + c
[docs] def norm2(self):
"""
:return: the norm2
:githublink:`%|py|91`
"""
v = self.vector()
return v.scalar(v)
[docs] def reverse(self):
"""
switch extremities
:githublink:`%|py|98`
"""
x = self._a
self._a = self._b
self._b = x
[docs] def topdown(self):
"""
the vector only points in the same semi-plan
:githublink:`%|py|106`
"""
if self._a > self._b:
self.reverse()