find the midpoint between two points in ruby

To find the midpoint between two points in Ruby, you can use the following code:

main.rb
def midpoint(point1, point2)
  [(point1[0] + point2[0])/2.0, (point1[1] + point2[1])/2.0]
end
94 chars
4 lines

This function takes two points as arguments, where each point is represented as an array of two numbers: the x-coordinate and y-coordinate. The function calculates the midpoint between the two points and returns it as a new array.

Example usage:

main.rb
point1 = [1, 2]
point2 = [5, 6]
midpoint = midpoint(point1, point2)
puts midpoint # => [3.0, 4.0]
98 chars
5 lines

gistlibby LogSnag