Module: Fusuma::Plugin::Touchscreen::Math
- Defined in:
- lib/fusuma/plugin/touchscreen/math.rb
Class Method Summary collapse
- .angle_between(base_point, other_point) ⇒ Object
- .angles_average(angles) ⇒ Object
- .angles_difference(angle1, angle2) ⇒ Object
- .center(points) ⇒ Object
- .distance(point1, point2) ⇒ Object
- .distance_from_line(x, y, k, b) ⇒ Object
Class Method Details
.angle_between(base_point, other_point) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/fusuma/plugin/touchscreen/math.rb', line 58 def self.angle_between(base_point, other_point) radians = ::Math.atan2(other_point[:y] - base_point[:y], other_point[:x] - base_point[:x]) degrees = radians * 180.0 / ::Math::PI degrees += 360.0 if degrees < 0 degrees end |
.angles_average(angles) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/fusuma/plugin/touchscreen/math.rb', line 27 def self.angles_average(angles) sum_x = 0.0 sum_y = 0.0 angles.each do |angle| radians = angle * ::Math::PI / 180.0 sum_x += ::Math.cos(radians) sum_y += ::Math.sin(radians) end average_radians = ::Math.atan2(sum_y, sum_x) average_degrees = average_radians * 180.0 / ::Math::PI average_degrees += 360.0 if average_degrees < 0 average_degrees end |
.angles_difference(angle1, angle2) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/fusuma/plugin/touchscreen/math.rb', line 6 def self.angles_difference(angle1, angle2) normalized_angle1 = normalize_angle(angle1) normalized_angle2 = normalize_angle(angle2) raw_difference = normalized_angle2 - normalized_angle1 if raw_difference < -180 raw_difference += 360 elsif raw_difference > 180 raw_difference -= 360 end raw_difference end |
.center(points) ⇒ Object
52 53 54 55 56 |
# File 'lib/fusuma/plugin/touchscreen/math.rb', line 52 def self.center(points) x = points.map { |p| p[:x] }.reduce(:+) / points.size y = points.map { |p| p[:y] }.reduce(:+) / points.size { x: x, y: y } end |
.distance(point1, point2) ⇒ Object
44 45 46 |
# File 'lib/fusuma/plugin/touchscreen/math.rb', line 44 def self.distance(point1, point2) ::Math.sqrt((point1[:x] - point2[:x])**2 + (point1[:y] - point2[:y])**2) end |
.distance_from_line(x, y, k, b) ⇒ Object
48 49 50 |
# File 'lib/fusuma/plugin/touchscreen/math.rb', line 48 def self.distance_from_line(x, y, k, b) (k * x + b - y).abs / ::Math.sqrt(k**2 + 1) end |