Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.79 KB | None | 0 0
  1. At=Struct.new(:al, :bl, :a, :b) do
  2.     def initialize(*args)
  3.         super
  4.        self.b ||= self.a
  5.     end
  6.  
  7.     def -@
  8.         At.new(al, bl, -a, -b)
  9.     end
  10.  
  11.     def positive?
  12.         if self.a >= 0 && self.b >= 0
  13.             return true
  14.         end
  15.     end
  16.  
  17.     def negative?
  18.         if self.a < 0 && self.b<0
  19.             return true
  20.         end
  21.     end
  22.      
  23.     def +(other)
  24.         At.new(
  25.             self.al + other.al,
  26.             self.bl + other.bl,
  27.             self.a + other.a,
  28.             self.b + other.b
  29.         )
  30.     end
  31.  
  32.     def -(other)
  33.         At.new(
  34.             self.al + other.al,
  35.             self.bl + other.bl,
  36.             self.a - other.a,
  37.             self.b - other.b
  38.         )
  39.     end
  40.  
  41.    
  42.     def *(other)
  43.         if [self, other].all?(&:positive?)
  44.             At.new(
  45.                 self.a * other.al + other.a * self.al,
  46.                 self.b * other.bl + other.b * self.bl,
  47.                 self.a * other.a,
  48.                 self.b * other.b
  49.             )
  50.         elsif [self, other].all?(&:negative?)
  51.             -self * -other
  52.         elsif self.negative? && other.positive?
  53.             -(-self * other)
  54.          elsif self.positive? && other.negative?
  55.             -(self * -other)
  56.         end
  57.     end
  58.    
  59.     def /(other)
  60.     if [self, other].all?(&:positive?) 
  61.         At.new(
  62.             ((self.a * other.bl + other.b * self.al) / (other.b**2)).round(2),
  63.             ((self.b * other.al + other.a * self.bl) / (other.a**2)).round(2),
  64.             (self.a / other.b).round(2),
  65.             (self.b / other.a).round(2)
  66.         )
  67.     elsif [self, other].all?(&:negative?)
  68.             -self / -other
  69.     elsif self.negative? && other.positive?
  70.             -(-self / other)
  71.     elsif self.positive? && other.negative?
  72.             -(self / -other)
  73.     end
  74.     end
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement