Class: JennCad::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/jenncad/position.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Point

Returns a new instance of Point.



58
59
60
61
# File 'lib/jenncad/position.rb', line 58

def initialize(args={})
  @pos = {x: 0.to_d, y: 0.to_d, z: 0.to_d}
  add(args)
end

Instance Attribute Details

#posObject

Returns the value of attribute pos.



57
58
59
# File 'lib/jenncad/position.rb', line 57

def pos
  @pos
end

Instance Method Details

#add(args) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/jenncad/position.rb', line 92

def add(args)
  if args.kind_of? Point
    @pos[:x] += args.x
    @pos[:y] += args.y
    @pos[:z] += args.z
    return self
  end

  args.each do |k, val|
    if [:chain, :debug].include? k
      next
    end
    unless val.kind_of? Numeric
      next
    end
    if val.to_d == 0.0
      next
    end

    keys = k.to_s.chars
    axis = []
    axis << keys.delete("x")
    axis << keys.delete("y")
    axis << keys.delete("z")
    multi = 1

    if keys.size > 0
      if keys.include?("h")
        multi = 0.5
      elsif keys.include?("q")
        multi = 0.25
      elsif keys.include?("d")
        multi = 2.0
      end
      if keys.include?("n") || keys.include?("i")
        multi *= -1
      end
    end
    axis.compact.each do |a|
      @pos[a.to_sym] += val.to_d * multi.to_d
    end
  end
  self
end

#to_aObject



80
81
82
83
84
85
86
# File 'lib/jenncad/position.rb', line 80

def to_a
  if @z != 0.0
    [@pos[:x], @pos[:y], @pos[:z]]
  else
    [@pos[:x], @pos[:y]]
  end
end

#to_hObject



88
89
90
# File 'lib/jenncad/position.rb', line 88

def to_h
  @pos.clone.delete_if{|k,v| v.to_d == 0.0}
end

#xObject



63
64
65
# File 'lib/jenncad/position.rb', line 63

def x
  @pos[:x]
end

#yObject



67
68
69
# File 'lib/jenncad/position.rb', line 67

def y
  @pos[:y]
end

#zObject



71
72
73
# File 'lib/jenncad/position.rb', line 71

def z
  @pos[:z]
end

#zero?Boolean

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/jenncad/position.rb', line 75

def zero?
  return true if x == 0.0 && y == 0.0 && z == 0.0
  false
end