# Find closest segment to a point?

I’m struggling a bit to find a method that identifies the closest segments for a given point.
OR
Given a point already on the path (found with GSPath.nearestPointOnPath_pathTime_()), find the next and previous points on the path.

The `GSPath.nearestPointOnPath_pathTime_()` is what you need. The integer part of the pathTime is the index of the node at the end of the segment.

1 Like

Oh, I didn’t notice it, thanks @GeorgSeifert !

Just to ensure I understand correctly how it works:
When I call `path.nearestPointOnPath_pathTime_(firstNode, None)`, if my firstNode has handles, it returns 2.0 as the index, but if no handles are attached, it returns 0.0.

Here, for example, I’m calling the method on three nodes:

Here for example I’m calling the method on three nodes :

1. 1st node
2. Node 1 unit from the 2nd Node
3. 2nd node

With handles I got :

``````1st node : 2.0
2nd node : 5.0
Node 1 unit close to 2nd node : 2.998186836859961
``````

Without :

``````1st node : 0.0
2nd node : 1.0
Node 1 unit close to 2nd node : 0.9986769556999207
``````

What is `firstNode`?

The function assumes a point somewhere on the segment between two nodes.

The first node of my path.

``````firstNode = path.nodes[0]
``````

Why would you run that thought `nearestPointOnPath_pathTime_()`?

It seems that I misunderstood your question.

Here is what I’m trying to achieve:

I’m working on a plugin to show “smart” measurement lines. In this plugin, I’m drawing a perpendicular line from the tangent of the middle node of segments. In EditView, I draw a line from the middle point to the first intersection points.

Now, I would like to know to which segment this intersection point is linked. Why? To reduce the number of measurement lines.

In this screenshot, both measurements are not needed; only one could remain, or an interpolation of these two. By finding which segment is linked to an intersection, I could do something to fix this.

So basically, I’m trying to build a method like :

``````GSPath.nearestSegmentFromPoint()
``````
1 Like

I think you just need to give it the intersectionPoint instead of the firstNode:

``````closestPoint, t = path.nearestPointOnPath_pathTime_(intersectionPoint, None)
lastPointOfTheSegment = path.nodes[int(t)]
``````

Yes. You have to use the point where you like to find the nearest point. Have a look at the Show Stem plugins. That should give you all you need.

Stem Width Plugin you meant, right Georg?
Show Stem doesn’t use any nearest point in path APIs.

I meant the “Show Stem Thickness” plugin.

2 Likes

Thanks, it works like a charm.

``````def getSegmentNearestPoint(path, point):
closest_point, t = path.nearestPointOnPath_pathTime_(point, None)
last_point_segment = path.nodes[int(t)]
last_point_xy = (last_point_segment.x, last_point_segment.y)

for s in path.segments:
x, y = s.lastPoint().x, s.lastPoint().y
if (x, y) == last_point_xy:
return s
``````
2 Likes

Now use that method in Skedge to highlight the segment closest to the mouse cursor and add add it to the Skedge sample snippets

(Or if you want and agree, I can do that for you in your name)

Would be a nice example for users.

2 Likes

Here’s the plug’n’play of your method above

5 Likes

Very cool!!

1 Like