Class: OrangeZest::Point
- Inherits:
-
Object
- Object
- OrangeZest::Point
- Defined in:
- lib/orange_zest/point.rb
Overview
A point in the game’s world.
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
Returns the value of attribute z.
Class Method Summary collapse
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #==(other) ⇒ Object
-
#distance(other) ⇒ Object
Computes the distance between this point and another.
- #hash ⇒ Object
-
#initialize(x, y, z = 0) ⇒ Point
constructor
A new instance of Point.
-
#line_to(other) ⇒ <Point>
Return all points in a line from one point to another.
- #to_s ⇒ Object (also: #inspect)
Constructor Details
#initialize(x, y, z = 0) ⇒ Point
Returns a new instance of Point.
6 7 8 9 10 |
# File 'lib/orange_zest/point.rb', line 6 def initialize(x, y, z = 0) @x = x @y = y @z = z end |
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x.
4 5 6 |
# File 'lib/orange_zest/point.rb', line 4 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
4 5 6 |
# File 'lib/orange_zest/point.rb', line 4 def y @y end |
#z ⇒ Object
Returns the value of attribute z.
4 5 6 |
# File 'lib/orange_zest/point.rb', line 4 def z @z end |
Class Method Details
.[](*args) ⇒ Object
12 13 14 |
# File 'lib/orange_zest/point.rb', line 12 def self.[](*args) new(*args) end |
Instance Method Details
#+(other) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/orange_zest/point.rb', line 16 def +(other) case other when Point Point.new(x + other.x, y + other.y, z + other.z) when Numeric Point.new(x + other, y + other, z + other) else raise TypeError, "can't add point to #{other}" end end |
#-(other) ⇒ Object
31 32 33 |
# File 'lib/orange_zest/point.rb', line 31 def -(other) self + -other end |
#==(other) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/orange_zest/point.rb', line 35 def ==(other) other.is_a?(Point) && x == other.x && y == other.y && z == other.z end |
#distance(other) ⇒ Object
Computes the distance between this point and another.
52 53 54 55 56 |
# File 'lib/orange_zest/point.rb', line 52 def distance(other) x_dist = (x - other.x) y_dist = (y - other.y) Math.sqrt(x_dist**2 + y_dist**2) end |
#hash ⇒ Object
42 43 44 |
# File 'lib/orange_zest/point.rb', line 42 def hash [x, y, z].hash end |
#line_to(other) ⇒ <Point>
Return all points in a line from one point to another.
This is a 2D operation - Z is ignored. Floating point components of either point are rounded to integers.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/orange_zest/point.rb', line 65 def line_to(other) # `supercover_line` from: https://www.redblobgames.com/grids/line-drawing.html dx = other.x.round - self.x.round dy = other.y.round - self.y.round nx = dx.abs ny = dy.abs sign_x = dx <=> 0 sign_y = dy <=> 0 p = self.clone points = [p.clone] ix = 0 iy = 0 while ix < nx || iy < ny decision = (1 + 2*ix) * ny - (1 + 2*iy) * nx if decision == 0 p.x += sign_x p.y += sign_y ix += 1 iy += 1 elsif decision < 0 p.x += sign_x ix += 1 else p.y += sign_y iy += 1 end points << p.clone end points end |
#to_s ⇒ Object Also known as: inspect
46 47 48 |
# File 'lib/orange_zest/point.rb', line 46 def to_s "#<Point: #{x}, #{y}, #{z}>" end |