Find a Vector from a point and line


In the API within the Geom module it states that a line can be defined or represented by either a point and a vector or two points.

What I would like to do is given the line and the point compute the two vectors (opposite directions) that this combination produces.

Given this some further thought, the point really should not be necessary. Given a line one should be able to computer the vectors from it.


Something like this…?

line0 = [, 0, 0),, 0, 10)]
line1 = [, 0, 0),, 0, 10)]

def opposite_vectors(line)
    vec1 = line[1].normalize
    vec1 = line[0].vector_to(line[1]).normalize    
  vec2 = vec1.reverse
  return vec1, vec2

puts opposite_vectors(line0)
puts opposite_vectors(line1)


Instead of expecting NoMethodError to be thrown, it’s better to do type checking if line[1].is_a?(Geom::Vector3d) because otherwise you hide all possible other errors that could be thrown. Exceptions should not be used for (planned) control flow, but for unexpected situations.