How to find closest point on two lines?

How can I find the closest point on two lines?

I realize I can find the closest point on a line from a Point3d.

But I’m not sure how to always find the closest point between the two lines. Could I use Vector3d.angle_between somehow?

What I’m really trying to do is find the closest point on a line from my input point.

I want to constraint the input to the closest position on the ridge.

You can see it’s not working quite right.

Here is my code so far.

if @is_cupola
  view.pick_helper.do_pick(x, y, 50)
  peak = (1..view.pick_helper.count).map { |i| [view.pick_helper.leaf_at(i - 1), view.pick_helper.transformation_at(i - 1)] }
                                    .select { |e| e[0].get_attribute('bc', 'type') == 'peak' }[0]
  if peak
    ray = view.pickray(x, y)
    ray[1].length = (ray[0] - peak[0].bounds.center.transform(peak[1])).length
    ray_point = ray[0] + ray[1]
  end
  if peak.to_a[0] != @peak
    @model.selection.clear
    @model.selection.add(peak[0]) if peak
  end
  @peak = peak.to_a[0]
  @peak_transformation = peak.to_a[1]
  projected_point = ray_point.project_to_line(@peak.start.position.transform(@peak_transformation), @peak.end.position.transform(@peak_transformation)) if @peak && point
  point = projected_point if projected_point
end

There is a dedicated method for this:
Geom#closest_points-class_method

2 Likes

Just an idea: Instead of pickray, probably you can combine with the view.inputpoint
Something like :

.
if peak
.
.
  ip = view.inputpoint(x, y) 
  projected_point = ip.position.project_to_line((@peak.start.posi...
.
.
end

:man_facepalming: I searched for a long time… I should just have went to bed like my wife suggested.

1 Like

Really ?

  1. In the API doc, left Nav pane, click Methods to switch to method list.
  2. type “closest” into the filter box.

Result …

image

2 Likes

Thanks I never used that before.

1 Like

… betcha will from now on. :wink: