Class: Draught::Point

Inherits:
Object
  • Object
show all
Includes:
Pointlike
Defined in:
lib/draught/point.rb

Constant Summary collapse

ZERO =
new(0, 0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pointlike

#points

Constructor Details

#initialize(x, y) ⇒ Point

Returns a new instance of Point.



16
17
18
# File 'lib/draught/point.rb', line 16

def initialize(x, y)
  @x, @y = x, y
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



14
15
16
# File 'lib/draught/point.rb', line 14

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



14
15
16
# File 'lib/draught/point.rb', line 14

def y
  @y
end

Class Method Details

.from_matrix(matrix) ⇒ Object



7
8
9
10
# File 'lib/draught/point.rb', line 7

def self.from_matrix(matrix)
  x, y = matrix.to_a.flatten
  Point.new(x, y)
end

Instance Method Details

#==(other) ⇒ Object



24
25
26
27
# File 'lib/draught/point.rb', line 24

def ==(other)
  other.point_type == point_type &&
    other.x == x && other.y == y
end

#approximates?(other, delta) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/draught/point.rb', line 29

def approximates?(other, delta)
  other.point_type == point_type &&
    ((other.x - x).abs <= delta) &&
    ((other.y - y).abs <= delta)
end

#point_typeObject



20
21
22
# File 'lib/draught/point.rb', line 20

def point_type
  :point
end

#to_matrixObject



43
44
45
# File 'lib/draught/point.rb', line 43

def to_matrix
  @matrix ||= Matrix[[x],[y],[1]].freeze
end

#transform(transformation) ⇒ Object



47
48
49
# File 'lib/draught/point.rb', line 47

def transform(transformation)
  transformation.call(self)
end

#translate(vector) ⇒ Object



35
36
37
# File 'lib/draught/point.rb', line 35

def translate(vector)
  transform(vector.to_transform)
end

#translation_to(point) ⇒ Object



39
40
41
# File 'lib/draught/point.rb', line 39

def translation_to(point)
  Vector.translation_between(self, point)
end