Class: Point2D
- Inherits:
-
Struct
- Object
- Struct
- Point2D
- Defined in:
- lib/cem/cruzzles.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Class Method Summary collapse
- .from_dir_bitmask(v) ⇒ Object
- .from_s(s) ⇒ Object
-
.minmax(array) ⇒ Object
Returns the minimum and maximum coordinates of the point array.
Instance Method Summary collapse
-
#*(other) ⇒ Object
Scalar multiplication.
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #area ⇒ Object
-
#dist(other) ⇒ Object
returns the euclidean distance to the given Point2D.
- #flip ⇒ Object
- #left ⇒ Object
- #left! ⇒ Object
- #manhattan(other) ⇒ Object
-
#max(other) ⇒ Object
Returns the component-wise maximum as a new Point2D.
-
#min(other) ⇒ Object
Returns the component-wise minimum as a new Point2D.
-
#minmax(other) ⇒ Object
Returns the minimum and maximum coordinates of this and the given point/point array.
- #to_dir_bitmask ⇒ Object
- #to_s ⇒ Object
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x
31 32 33 |
# File 'lib/cem/cruzzles.rb', line 31 def x @x end |
#y ⇒ Object
Returns the value of attribute y
31 32 33 |
# File 'lib/cem/cruzzles.rb', line 31 def y @y end |
Class Method Details
.from_dir_bitmask(v) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/cem/cruzzles.rb', line 157 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
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
175 176 177 |
# File 'lib/cem/cruzzles.rb', line 175 def <=>(other) y == other.y ? x <=> other.x : y <=> other.y end |
#==(other) ⇒ Object
179 180 181 182 |
# File 'lib/cem/cruzzles.rb', line 179 def ==(other) return false if !other.respond_to?(:x) || !other.respond_to?(:y) y == other.y && x == other.x end |
#area ⇒ Object
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 |
#flip ⇒ Object
36 37 38 |
# File 'lib/cem/cruzzles.rb', line 36 def flip Point2D.new(-x,-y) end |
#left ⇒ Object
131 132 133 |
# File 'lib/cem/cruzzles.rb', line 131 def left return self + Dir2D.LEFT end |
#left! ⇒ Object
135 136 137 138 139 |
# File 'lib/cem/cruzzles.rb', line 135 def left! x += Dir2D.LEFT.x y += Dir2D.LEFT.y return self 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 |
#to_dir_bitmask ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/cem/cruzzles.rb', line 142 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_s ⇒ Object
32 33 34 |
# File 'lib/cem/cruzzles.rb', line 32 def to_s "#{x},#{y}" end |