-
Notifications
You must be signed in to change notification settings - Fork 10
/
g81_relative.rb
61 lines (53 loc) · 2.4 KB
/
g81_relative.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- encoding : utf-8 -*-
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Copyright (C) 2004 Sam Hocevar
# 14 rue de Plaisance, 75014 Paris, France
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
# 0. You just DO WHAT THE FUCK YOU WANT TO.
#
#
# David Hagege <[email protected]>
class G81Relative
def self.convert_distance_to_fractional_turns(distance)
screw_pitch = 0.5
rat = Rational(distance / screw_pitch).round(1)
rat = rat == 0 / 1 ? 0 : rat
"#{rat.abs}#{rat > 0 ? "CW" : "CCW"}"
end
def self.convert_distance_to_degrees(distance)
screw_pitch = 0.5
deg = (distance / screw_pitch * 360).round
"#{deg.abs}°#{deg > 0 ? "CW" : "CCW"}"
end
def self.nb_fmt(nb)
"%.2f" % nb
end
def self.g81_relative(content)
points = content.scan(/-?\d.\d+/).map(&:to_f)
center = points[24]
top_left, top_middle, top_right, middle_left,
middle_right, bottom_left,
bottom_center, bottom_right = points.values_at(0, 3, 6, 21, 27, 42, 45, 48).map { |x| (x - center).round(2) }
%Q{
Raw values:
#{nb_fmt(top_left)}\t#{nb_fmt(top_middle)}\t#{nb_fmt(top_right)}
#{nb_fmt(middle_left)}\t0.00\t#{nb_fmt(middle_right)}
#{nb_fmt(bottom_left)}\t#{nb_fmt(bottom_center)}\t#{nb_fmt(bottom_right)}
Degrees:
#{convert_distance_to_degrees(top_left)}\t#{convert_distance_to_degrees(top_middle)}\t#{convert_distance_to_degrees(top_right)}
#{convert_distance_to_degrees(middle_left)}\t0\t#{convert_distance_to_degrees(middle_right)}
#{convert_distance_to_degrees(bottom_left)}\t#{convert_distance_to_degrees(bottom_center)}\t#{convert_distance_to_degrees(bottom_right)}
Fractional Turns:
#{convert_distance_to_fractional_turns(top_left)}\t#{convert_distance_to_fractional_turns(top_middle)}\t#{convert_distance_to_fractional_turns(top_right)}
#{convert_distance_to_fractional_turns(middle_left)}\t0\t#{convert_distance_to_fractional_turns(middle_right)}
#{convert_distance_to_fractional_turns(bottom_left)}\t#{convert_distance_to_fractional_turns(bottom_center)}\t#{convert_distance_to_fractional_turns(bottom_right)}
}
end
end