Class: Point2D

Inherits:
Struct
  • Object
show all
Defined in:
lib/cem/cruzzles.rb

Direct Known Subclasses

Dir2D

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



23
24
25
# File 'lib/cem/cruzzles.rb', line 23

def x
  @x
end

#yObject

Returns the value of attribute y

Returns:

  • (Object)

    the current value of y



23
24
25
# File 'lib/cem/cruzzles.rb', line 23

def y
  @y
end

Class Method Details

.from_dir_bitmask(v) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cem/cruzzles.rb', line 97

def self.from_dir_bitmask(v) 

  result = []
  if v & 1 > 0
    result << Point2D.new(0, -1)
  end
  if v & 2 > 0
    result << Point2D.new(1, 0)
  end
  if v & 4 > 0
    result << Point2D.new(0, 1)
  end
  if v & 8 > 0
    result << Point2D.new(-1, 0)
  end
  return result
end

.from_s(s) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cem/cruzzles.rb', line 54

def self.from_s(s)

  case s.upcase
    when 'E', 'RIGHT', 'R', '>'
      Point2D.new(+1,  0)
    when 'N', 'UP', 'TOP', 'T', 'U', '^'
      Point2D.new( 0, -1)
    when 'S', 'DOWN', 'BOTTOM', 'B', 'D', 'v', 'V'
      Point2D.new( 0, +1)
    when 'W', 'LEFT', 'L', '<'
      Point2D.new(-1,  0)
  else 
    raise s
  end
  
end

Instance Method Details

#*(other) ⇒ Object

Scalar multiplication



41
42
43
# File 'lib/cem/cruzzles.rb', line 41

def *(other)
  Point2D.new(x * other, y * other)    
end

#+(other) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/cem/cruzzles.rb', line 32

def +(other)
  if other.is_a? Array
    other.map { |o| self + o }
  else
    Point2D.new(x + other.x, y + other.y)
  end
end

#==(other) ⇒ Object

def <=>(other)

y == other.y ? x <=> other.x : y <=> other.y

end



119
120
121
# File 'lib/cem/cruzzles.rb', line 119

def ==(other)
  y == other.y && x == other.x
end

#dist(other) ⇒ Object

returns the euclidean distance to the given Point2D



50
51
52
# File 'lib/cem/cruzzles.rb', line 50

def dist(other)
  return ((x - other.x) ** 2 + (y - other.y) ** 2).sqrt
end

#flipObject



28
29
30
# File 'lib/cem/cruzzles.rb', line 28

def flip
  Point2D.new(-x,-y)
end

#leftObject



71
72
73
# File 'lib/cem/cruzzles.rb', line 71

def left 
  return self + Dir2D.LEFT
end

#left!Object



75
76
77
78
79
# File 'lib/cem/cruzzles.rb', line 75

def left!
  x += Dir2D.LEFT.x
  y += Dir2D.LEFT.y
  return self
end

#manhattan(other) ⇒ Object



45
46
47
# File 'lib/cem/cruzzles.rb', line 45

def manhattan(other)
  return (x - other.x).abs + (y - other.y).abs
end

#to_dir_bitmaskObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cem/cruzzles.rb', line 82

def to_dir_bitmask 

  if x == 0 && y == -1
    return 1
  elsif x == 1 && y == 0
    return 2
  elsif x == 0 && y == 1
    return 4
  elsif x == -1 && y == 0
    return 8
  else
    raise self.p
  end
end

#to_sObject



24
25
26
# File 'lib/cem/cruzzles.rb', line 24

def to_s
  "#{x},#{y}"
end