Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Objects with Mass
- http://cobra-language.com/forums/viewtopic.php?f=4&t=17230
- If you find typing:
- Point(x, y, z)
- too cumbersome, you could rename it to 'P':
- P(x, y, z)
- or "V" for "Vector".
- """
- @number float64
- class Constants
- const goldenRatio = 1.61803398875
- class Point
- test
- p = Point(1, 2, 3)
- assert p.x == 1 and p.y == 2 and p.z == 3
- assert p.toString == 'Point(1, 2, 3)'
- cue init(x as number, y as number, z as number)
- base.init
- _x, _y, _z = x, y, z
- get x from var as number
- get y from var as number
- get z from var as number
- def toString as String is override
- return '[.typeOf.name]([.x], [.y], [.z])'
- def equals(other as Object?) as bool is override
- if other inherits Point
- return .equals(other) # will call overload below
- else
- return false
- def equals(other as Point) as bool
- return .x == other.x and .y == other.y and .z == other.z
- def getHashCode as int is override
- return HashCodeUtils.combine([.x.getHashCode, .y.getHashCode, .z.getHashCode])
- def add(other as Point) as Point
- test
- a, b = Point(1, 2, 3), Point(-2, 5, 7)
- assert a.add(b) == Point(-1, 7, 10)
- body
- return Point(.x + other.x, .y + other.y, .z + other.z)
- class MassObject is abstract
- cue init(location as Point)
- base.init
- _location = location
- def toString as String is override
- sb = StringBuilder('[.typeOf.name](')
- _toString(sb)
- sb.append(')')
- return sb.toString
- def _toString(sb as StringBuilder)
- sb.append('location=[.location], centerOfMass=[.centerOfMass], totalMass=[.totalMass]')
- get location from var as Point
- def move(offset as Point)
- _location = .location.add(offset)
- def centerOfMass as Point is abstract
- def totalMass as number is abstract
- class MassPoint inherits MassObject
- var _mass as number
- cue init(location as Point, mass as number)
- base.init(location)
- _mass = mass
- def centerOfMass as Point is override
- return .location
- def totalMass as number is override
- return _mass
- class RightCuboid inherits MassObject
- """
- http://en.wikipedia.org/wiki/Cuboid
- """
- cue init(corner as Point, dimensions as Point)
- base.init(corner)
- _dimensions = dimensions
- def _toString(sb as StringBuilder)
- base._toString(sb)
- sb.append(', dimensions=[.dimensions]')
- get dimensions from var as Point
- def centerOfMass as Point is override
- loc, dim = .location, .dimensions
- return Point((loc.x+dim.x)/2, (loc.y+dim.y)/2, (loc.z+dim.z)/2)
- def totalMass as number is override
- return .volume * .density
- def volume as number
- dim = .dimensions
- return dim.x * dim.y * dim.z
- def density as number
- return 1.0 # to-do: make this a property?
- class Program
- def main
- objects = List<of MassObject>()
- objects.add(MassPoint(Point(0, 0, 0), 10))
- objects.add(RightCuboid(Point(4, 4, 4), Point(1, 1, Constants.goldenRatio)))
- for object in objects
- print object
- offset = Point(-1, -1, -1)
- print 'move by', offset
- for object in objects
- object.move(offset)
- print object
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement