Class: Castaway::Point
- Inherits:
-
Struct
- Object
- Struct
- Castaway::Point
- Defined in:
- lib/castaway/point.rb
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Class Method Summary collapse
Instance Method Summary collapse
- #*(factor) ⇒ Object
- #+(pt) ⇒ Object
- #-(pt) ⇒ Object
- #rotate(radians) ⇒ Object
- #scale(sx, sy = sx) ⇒ Object
- #to_geometry ⇒ Object
- #to_s ⇒ Object
- #translate(dx, dy) ⇒ Object
- #zero? ⇒ Boolean
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x
3 4 5 |
# File 'lib/castaway/point.rb', line 3 def x @x end |
#y ⇒ Object
Returns the value of attribute y
3 4 5 |
# File 'lib/castaway/point.rb', line 3 def y @y end |
Class Method Details
.make(*args) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/castaway/point.rb', line 4 def self.make(*args) if args.length == 1 && args[0].is_a?(Array) new(args[0][0], args[0][1]) elsif args.length == 1 && args[0].is_a?(Point) args[0] else raise ArgumentError, "can't make a point from #{args.inspect}" end end |
Instance Method Details
#*(factor) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/castaway/point.rb', line 14 def *(factor) if factor.respond_to?(:x) Point.new(x * factor.x, y * factor.y) elsif factor.respond_to?(:width) Point.new(x * factor.width, y * factor.height) else Point.new(x * factor, y * factor) end end |
#+(pt) ⇒ Object
28 29 30 |
# File 'lib/castaway/point.rb', line 28 def +(pt) Point.new(x + pt.x, y + pt.y) end |
#-(pt) ⇒ Object
24 25 26 |
# File 'lib/castaway/point.rb', line 24 def -(pt) Point.new(x - pt.x, y - pt.y) end |
#rotate(radians) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/castaway/point.rb', line 44 def rotate(radians) cos = Math.cos(radians) sin = Math.sin(radians) nx = x * cos - y * sin ny = y * cos + x * sin Point.new(nx, ny) end |
#scale(sx, sy = sx) ⇒ Object
40 41 42 |
# File 'lib/castaway/point.rb', line 40 def scale(sx, sy = sx) Point.new(x * sx, y * sy) end |
#to_geometry ⇒ Object
58 59 60 |
# File 'lib/castaway/point.rb', line 58 def to_geometry format('+%.2f+%.2f', x, y) end |
#to_s ⇒ Object
54 55 56 |
# File 'lib/castaway/point.rb', line 54 def to_s format('(%.2f, %.2f)', x, y) end |
#translate(dx, dy) ⇒ Object
36 37 38 |
# File 'lib/castaway/point.rb', line 36 def translate(dx, dy) Point.new(x + dx, y + dy) end |
#zero? ⇒ Boolean
32 33 34 |
# File 'lib/castaway/point.rb', line 32 def zero? x == 0 && y == 0 end |