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 = [Geom::Point3d.new(0, 0, 0), Geom::Point3d.new(0, 0, 10)]
line1 = [Geom::Point3d.new(0, 0, 0), Geom::Vector3d.new(0, 0, 10)]

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

puts opposite_vectors(line0)
puts opposite_vectors(line1)
2 Likes

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.

2 Likes