Class: Castaway::Point

Inherits:
Struct
  • Object
show all
Defined in:
lib/castaway/point.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#xObject

Returns the value of attribute x

Returns:

  • (Object)

    the current value of x



3
4
5
# File 'lib/castaway/point.rb', line 3

def x
  @x
end

#yObject

Returns the value of attribute y

Returns:

  • (Object)

    the current value of 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_geometryObject



58
59
60
# File 'lib/castaway/point.rb', line 58

def to_geometry
  format('+%.2f+%.2f', x, y)
end

#to_sObject



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

Returns:

  • (Boolean)


32
33
34
# File 'lib/castaway/point.rb', line 32

def zero?
  x == 0 && y == 0
end