Class: Doodl::Location
- Inherits:
-
Object
- Object
- Doodl::Location
- Defined in:
- lib/layout/layout.rb,
lib/layout/fr_layout.rb
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #*(scalar) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(scalar) ⇒ Object
- #add!(a, b = nil) ⇒ Object
- #dist(other) ⇒ Object
- #div!(scalar) ⇒ Object
-
#initialize(a, b) ⇒ Location
constructor
A new instance of Location.
- #length ⇒ Object
- #mul!(scalar) ⇒ Object
- #normalize! ⇒ Object
- #rotate!(theta) ⇒ Object
- #rotate_radians!(theta) ⇒ Object
- #safedistance ⇒ Object
- #sub!(a, b = nil) ⇒ Object
Constructor Details
#initialize(a, b) ⇒ Location
Returns a new instance of Location.
152 153 154 155 156 157 158 159 |
# File 'lib/layout/layout.rb', line 152 def initialize(a, b) if a.is_a?(Numeric) and b.is_a?(Numeric) @x, @y = a, b else @x = b.x - a.x @y = b.y - a.y end end |
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x.
150 151 152 |
# File 'lib/layout/layout.rb', line 150 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
150 151 152 |
# File 'lib/layout/layout.rb', line 150 def y @y end |
Instance Method Details
#*(scalar) ⇒ Object
177 178 179 |
# File 'lib/layout/layout.rb', line 177 def *(scalar) Location.new(scalar * @x, scalar * @y) end |
#+(other) ⇒ Object
169 170 171 |
# File 'lib/layout/layout.rb', line 169 def +(other) Location.new(@x + other.x, @y + other.y) end |
#-(other) ⇒ Object
173 174 175 |
# File 'lib/layout/layout.rb', line 173 def -(other) Location.new(@x - other.x, @y - other.y) end |
#-@ ⇒ Object
165 166 167 |
# File 'lib/layout/layout.rb', line 165 def -@ self * (-1) end |
#/(scalar) ⇒ Object
181 182 183 |
# File 'lib/layout/layout.rb', line 181 def /(scalar) Location.new(@x / scalar, @y / scalar) end |
#add!(a, b = nil) ⇒ Object
197 198 199 200 201 202 203 204 205 206 |
# File 'lib/layout/layout.rb', line 197 def add!(a, b = nil) if b @x += a @y += b else @x += a.x @y += a.y end return self end |
#dist(other) ⇒ Object
161 162 163 |
# File 'lib/layout/layout.rb', line 161 def dist(other) Math.sqrt((@x - other.x) ** 2 + (@y - other.y) ** 2) end |
#div!(scalar) ⇒ Object
185 186 187 188 189 |
# File 'lib/layout/layout.rb', line 185 def div!(scalar) @x /= scalar @y /= scalar return self end |
#length ⇒ Object
216 217 218 |
# File 'lib/layout/layout.rb', line 216 def length Math.sqrt(@x**2 + @y**2) end |
#mul!(scalar) ⇒ Object
191 192 193 194 195 |
# File 'lib/layout/layout.rb', line 191 def mul!(scalar) @x *= scalar @y *= scalar return self end |
#normalize! ⇒ Object
220 221 222 223 224 |
# File 'lib/layout/layout.rb', line 220 def normalize! m = length div!(length) return self end |
#rotate!(theta) ⇒ Object
234 235 236 |
# File 'lib/layout/layout.rb', line 234 def rotate!(theta) self.rotate_radians!((Math::PI/180.0) * theta) end |
#rotate_radians!(theta) ⇒ Object
226 227 228 229 230 231 232 |
# File 'lib/layout/layout.rb', line 226 def rotate_radians!(theta) m = length a = Math.cos(theta) * @x - Math.sin(theta) * @y b = Math.sin(theta) * @x + Math.cos(theta) * @y @x, @y = a, b return self end |
#safedistance ⇒ Object
100 101 102 |
# File 'lib/layout/fr_layout.rb', line 100 def safedistance [self.length, Float::EPSILON].max end |
#sub!(a, b = nil) ⇒ Object
208 209 210 211 212 213 214 |
# File 'lib/layout/layout.rb', line 208 def sub!(a, b = nil) if b add!(-a, -b) else add!(-a) end end |