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



31
32
33
# File 'lib/cem/cruzzles.rb', line 31

def x
  @x
end

#yObject

Returns the value of attribute y

Returns:

  • (Object)

    the current value of y



31
32
33
# File 'lib/cem/cruzzles.rb', line 31

def y
  @y
end

Class Method Details

.from_dir_bitmask(v) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/cem/cruzzles.rb', line 185

def self.from_dir_bitmask(v) 

  result = []
  if v & 1 > 0
    result << Dir2D::N
  end
  if v & 2 > 0
    result << Dir2D::E
  end
  if v & 4 > 0
    result << Dir2D::S
  end
  if v & 8 > 0
    result << Dir2D::W
  end
  return result
end

.from_s(s) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cem/cruzzles.rb', line 74

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

.minmax(array) ⇒ Object

Returns the minimum and maximum coordinates of the point array.



125
126
127
128
129
# File 'lib/cem/cruzzles.rb', line 125

def self.minmax(array)
  return [nil, nil] if !array || array.size == 0
  
  [array.reduce { |min, o| min.min(o) }, array.reduce { |max, o| max.max(o) } ]
end

Instance Method Details

#*(other) ⇒ Object

Scalar multiplication



57
58
59
# File 'lib/cem/cruzzles.rb', line 57

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

#+(other) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/cem/cruzzles.rb', line 40

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



48
49
50
51
52
53
54
# File 'lib/cem/cruzzles.rb', line 48

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



203
204
205
# File 'lib/cem/cruzzles.rb', line 203

def <=>(other)
  y == other.y ? x <=> other.x : y <=> other.y
end

#==(other) ⇒ Object



207
208
209
210
# File 'lib/cem/cruzzles.rb', line 207

def ==(other)
  return false if !other.respond_to?(:x) || !other.respond_to?(:y)
  y == other.y && x == other.x
end

#add!(other) ⇒ Object



131
132
133
134
135
# File 'lib/cem/cruzzles.rb', line 131

def add!(other)
  self.x += other.x
  self.y += other.y
  return self
end

#areaObject



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

def area
  return x * y
end

#dist(other) ⇒ Object

returns the euclidean distance to the given Point2D



66
67
68
# File 'lib/cem/cruzzles.rb', line 66

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

#downObject



161
162
163
# File 'lib/cem/cruzzles.rb', line 161

def down
  self + Dir2D::DOWN
end

#down!Object



165
166
167
# File 'lib/cem/cruzzles.rb', line 165

def down!
  add!(Dir2D::DOWN)
end

#flipObject



36
37
38
# File 'lib/cem/cruzzles.rb', line 36

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

#leftObject



137
138
139
# File 'lib/cem/cruzzles.rb', line 137

def left 
  self + Dir2D::LEFT
end

#left!Object



141
142
143
# File 'lib/cem/cruzzles.rb', line 141

def left!
  add!(Dir2D::LEFT)
end

#manhattan(other) ⇒ Object



61
62
63
# File 'lib/cem/cruzzles.rb', line 61

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

#max(other) ⇒ Object

Returns the component-wise maximum as a new Point2D

Point2D.new(5,2).min(Point2D.new(1,3)) == Point2D.new(5,3) -> true


109
110
111
112
113
114
115
116
117
# File 'lib/cem/cruzzles.rb', line 109

def max(other)
  return self if !other

  if other.is_a? Array
    other.reduce(self) { |max, o| max.max(o) }
  else
    Point2D.new(Cem.max(x, other.x), Cem.max(y, other.y))
  end
end

#min(other) ⇒ Object

Returns the component-wise minimum as a new Point2D

Point2D.new(5,2).min(Point2D.new(1,3)) == Point2D.new(1,2) -> true


95
96
97
98
99
100
101
102
103
# File 'lib/cem/cruzzles.rb', line 95

def min(other)
  return self if !other
  
  if other.is_a? Array
    other.reduce(self) { |min, o| min.min(o) }
  else
    Point2D.new(Cem.min(x, other.x), Cem.min(y, other.y))
  end
end

#minmax(other) ⇒ Object

Returns the minimum and maximum coordinates of this and the given point/point array.



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

def minmax(other)
  [self.min(other), self.max(other)]
end

#rightObject



145
146
147
# File 'lib/cem/cruzzles.rb', line 145

def right 
  self + Dir2D::RIGHT
end

#right!Object



149
150
151
# File 'lib/cem/cruzzles.rb', line 149

def right!
  add!(Dir2D::RIGHT)
end

#to_dir_bitmaskObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/cem/cruzzles.rb', line 169

def to_dir_bitmask 

  case self
  when Dir2D::N
    return 1
  when Dir2D::E
    return 2
  when Dir2D::S
    return 4
  when Dir2D::W
    return 8
  else
    raise "Only Dir2Ds can be converted to direction bitmask: #{self.inspect}"
  end
end

#to_sObject



32
33
34
# File 'lib/cem/cruzzles.rb', line 32

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

#upObject



153
154
155
# File 'lib/cem/cruzzles.rb', line 153

def up
  self + Dir2D::UP
end

#up!Object



157
158
159
# File 'lib/cem/cruzzles.rb', line 157

def up!
  add!(Dir2D::UP)
end