Class: Point
- Inherits:
-
Object
- Object
- Point
- Defined in:
- lib/ruby-doom.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
- #==(other) ⇒ Object
- #distance_to(p1) ⇒ Object
-
#initialize(x, y) ⇒ Point
constructor
A new instance of Point.
- #lineto(p1) ⇒ Object
- #scale(factor) ⇒ Object
- #slope_to(p1) ⇒ Object
- #to_s ⇒ Object
- #translate(x, y) ⇒ Object
Constructor Details
#initialize(x, y) ⇒ Point
Returns a new instance of Point.
301 302 303 304 |
# File 'lib/ruby-doom.rb', line 301 def initialize(x,y) @x=x @y=y end |
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x.
300 301 302 |
# File 'lib/ruby-doom.rb', line 300 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
300 301 302 |
# File 'lib/ruby-doom.rb', line 300 def y @y end |
Instance Method Details
#==(other) ⇒ Object
308 309 310 |
# File 'lib/ruby-doom.rb', line 308 def ==(other) return other.x == @x && other.y == @y end |
#distance_to(p1) ⇒ Object
317 318 319 |
# File 'lib/ruby-doom.rb', line 317 def distance_to(p1) Math.sqrt(((p1.x - @x) ** 2) + ((p1.y - @y) ** 2)) end |
#lineto(p1) ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/ruby-doom.rb', line 320 def lineto(p1) res = [] current_x = @x current_y = @y slope = slope_to(p1) res << Point.new(current_x, current_y) distance_to(p1).to_i.times {|s| if slope == 0 current_x += (p1.x < @x ? -1 : 1) res << Point.new(current_x, current_y) next end if slope == nil current_y += (p1.y < @y ? -1 : 1) res << Point.new(current_x, current_y) next end current_x += slope cur += (1-slope) res << Point.new(current_x, current_y) } res << p1 return res end |
#scale(factor) ⇒ Object
305 306 307 |
# File 'lib/ruby-doom.rb', line 305 def scale(factor) Point.new(x * factor, y * factor) end |
#slope_to(p1) ⇒ Object
311 312 313 314 315 316 |
# File 'lib/ruby-doom.rb', line 311 def slope_to(p1) if (p1.x - @x) == 0 return nil end (p1.y - @y) / (p1.x - @x) end |
#to_s ⇒ Object
349 350 351 |
# File 'lib/ruby-doom.rb', line 349 def to_s "(" + @x.to_s + "," + @y.to_s + ")" end |