Class: OrangeZest::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/orange_zest/point.rb

Overview

A point in the game’s world.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#xObject

Returns the value of attribute x.



4
5
6
# File 'lib/orange_zest/point.rb', line 4

def x
  @x
end

#yObject

Returns the value of attribute y.



4
5
6
# File 'lib/orange_zest/point.rb', line 4

def y
  @y
end

#zObject

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

#-@Object



27
28
29
# File 'lib/orange_zest/point.rb', line 27

def -@
  Point.new(-x, -y, -z)
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

#hashObject



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.

Parameters:

Returns:



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_sObject Also known as: inspect



46
47
48
# File 'lib/orange_zest/point.rb', line 46

def to_s
  "#<Point: #{x}, #{y}, #{z}>"
end