Advertisement
Guest User

Objects with Mass, in Cobra

a guest
Dec 14th, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. """
  2. Objects with Mass
  3.  
  4. http://cobra-language.com/forums/viewtopic.php?f=4&t=17230
  5.  
  6. If you find typing:
  7.     Point(x, y, z)
  8. too cumbersome, you could rename it to 'P':
  9.     P(x, y, z)
  10. or "V" for "Vector".
  11. """
  12.  
  13. @number float64
  14.  
  15.  
  16. class Constants
  17.  
  18.     const goldenRatio = 1.61803398875
  19.  
  20.  
  21. class Point
  22.  
  23.     test
  24.         p = Point(1, 2, 3)
  25.         assert p.x == 1 and p.y == 2 and p.z == 3
  26.         assert p.toString == 'Point(1, 2, 3)'
  27.  
  28.     cue init(x as number, y as number, z as number)
  29.         base.init
  30.         _x, _y, _z = x, y, z
  31.    
  32.     get x from var as number
  33.    
  34.     get y from var as number
  35.    
  36.     get z from var as number
  37.    
  38.     def toString as String is override
  39.         return '[.typeOf.name]([.x], [.y], [.z])'
  40.  
  41.     def equals(other as Object?) as bool is override
  42.         if other inherits Point
  43.             return .equals(other)  # will call overload below
  44.         else
  45.             return false
  46.  
  47.     def equals(other as Point) as bool
  48.         return .x == other.x and .y == other.y and .z == other.z
  49.  
  50.     def getHashCode as int is override
  51.         return HashCodeUtils.combine([.x.getHashCode, .y.getHashCode, .z.getHashCode])
  52.  
  53.     def add(other as Point) as Point
  54.         test
  55.             a, b = Point(1, 2, 3), Point(-2, 5, 7)
  56.             assert a.add(b) == Point(-1, 7, 10)
  57.         body
  58.             return Point(.x + other.x, .y + other.y, .z + other.z)
  59.  
  60.  
  61. class MassObject is abstract
  62.  
  63.     cue init(location as Point)
  64.         base.init
  65.         _location = location
  66.  
  67.     def toString as String is override
  68.         sb = StringBuilder('[.typeOf.name](')
  69.         _toString(sb)
  70.         sb.append(')')
  71.         return sb.toString
  72.    
  73.     def _toString(sb as StringBuilder)
  74.         sb.append('location=[.location], centerOfMass=[.centerOfMass], totalMass=[.totalMass]')
  75.  
  76.     get location from var as Point
  77.  
  78.     def move(offset as Point)
  79.         _location = .location.add(offset)
  80.  
  81.     def centerOfMass as Point is abstract
  82.  
  83.     def totalMass as number is abstract
  84.  
  85.  
  86. class MassPoint inherits MassObject
  87.  
  88.     var _mass as number
  89.  
  90.     cue init(location as Point, mass as number)
  91.         base.init(location)
  92.         _mass = mass
  93.  
  94.     def centerOfMass as Point is override
  95.         return .location
  96.  
  97.     def totalMass as number is override
  98.         return _mass
  99.  
  100.  
  101. class RightCuboid inherits MassObject
  102.     """
  103.     http://en.wikipedia.org/wiki/Cuboid
  104.     """
  105.    
  106.     cue init(corner as Point, dimensions as Point)
  107.         base.init(corner)
  108.         _dimensions = dimensions
  109.  
  110.     def _toString(sb as StringBuilder)
  111.         base._toString(sb)
  112.         sb.append(', dimensions=[.dimensions]')
  113.  
  114.     get dimensions from var as Point
  115.  
  116.     def centerOfMass as Point is override
  117.         loc, dim = .location, .dimensions
  118.         return Point((loc.x+dim.x)/2, (loc.y+dim.y)/2, (loc.z+dim.z)/2)
  119.  
  120.     def totalMass as number is override
  121.         return .volume * .density
  122.  
  123.     def volume as number
  124.         dim = .dimensions
  125.         return dim.x * dim.y * dim.z
  126.  
  127.     def density as number
  128.         return 1.0  # to-do: make this a property?
  129.  
  130.  
  131. class Program
  132.  
  133.     def main
  134.         objects = List<of MassObject>()
  135.         objects.add(MassPoint(Point(0, 0, 0), 10))
  136.         objects.add(RightCuboid(Point(4, 4, 4), Point(1, 1, Constants.goldenRatio)))
  137.         for object in objects
  138.             print object
  139.         offset = Point(-1, -1, -1)
  140.         print 'move by', offset
  141.         for object in objects
  142.             object.move(offset)
  143.             print object
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement