Abhisek92

Transform.py

Jan 24th, 2018
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. import math
  2.  
  3. class Point(object):
  4.     def __init__(self, arg):
  5.         try:
  6.             if isinstance(arg, tuple):
  7.                 if all(isinstance(element, (int, float)) for element in arg):
  8.                     if len(arg) <= 3 and len(arg) > 0:
  9.                         self.__dimension = len(arg)
  10.                         self.__point = arg
  11.                     else:
  12.                         raise DimError("Dimension Error: Expected maximum 3 dimensions")
  13.                 else:
  14.                     raise TypeError("Type Error: Expected <float/int>")
  15.             else:
  16.                 raise ArgumentError("Argument Error: Expected <tuple>")
  17.         except TypeError as type_exception:
  18.             print(type_exception)
  19.  
  20.         def get_dimension(self):
  21.             return self.__dimension
  22.  
  23. class Line(object):
  24.     def __init__(self, line):
  25.         try:
  26.             if isinstance(line, tuple):
  27.                 if len(line) == 2:
  28.                     if all(isinstance(element, Point) for element in line):
  29.                         if len(set([point.get_dimension() for point in line])) == 1:
  30.                             self.__start = line[0]
  31.                             self.__end = line[1]
  32.                         else:
  33.                             raise DimError("Dimension Error: All the points must be of same dimension")
  34.                     else:
  35.                         raise TypeError("Type Error: Expected pair of Points")
  36.                 else:
  37.                     raise SizeError("Size Error: Expected one pair of Points")
  38.             else:
  39.                 raise TypeError("Argument Error: Expected <tuple>")
  40.         except TypeError as type_exception:
  41.             print(type_exception)
  42.         except SizeError as size_exception:
  43.             print(size_exception)
  44.         except DimError as dim_exception:
  45.             print(dim_exception)
  46.  
  47.         def length(self):
  48.             return (sum([(a - b)**2 for a, b in zip(self.__start, self.__end)]))**(1/2)
  49.  
  50.         def slope(self):
  51.             if self.length() != 0:
  52.                 return [((b - a) / self.length()) for a, b in zip(self.__start, self.__end)]
  53.             else:
  54.                 return None
  55.  
  56.         def slope_angle(self, flag=False):
  57.             if self.slope() is not None:
  58.                 if not flag:
  59.                     return [(math.acos(cosine)) for cosine in self.slope()]
  60.                 else:
  61.                     return [(math.degrees(math.acos(cosine))) for cosine in self.slope()]
  62.  
  63. class LineString(object):
  64.     def __init__(self, lines):
Add Comment
Please, Sign In to add comment