# Find nearest point for a given coordinate

157 views

### Jens Kutilek

Sep 3, 2013, 12:41:49 PM9/3/13
Hi,

is there any shortcut method that returns the on-curve point of a glyph which has the closest distance from a given coordinate?

Or do I have to bake my own? ;)

Cheers,
Jens

### Tal Leming

Sep 3, 2013, 1:33:46 PM9/3/13
I think you'll have to bake your own.

A custom point pen might be the easiest way to do it. You could store something like (x, y, contour index, point index) for all points passed through addPoint and then iterate over that when you test the coordinate. Or you could start the pen with the coordinate and as a point comes in through addPoint you could test the distance and store the contour index and point index if the distance is closer to the coordinate than the point that was formerly closest. If there is a direct hit, you can stop. Or… Lots of fun ways to do this. :-)

Tal
> --
> --
> You received this message because you are subscribed to the Google Groups "RoboFab" group.
> To post to this group, send email to rob...@googlegroups.com
> To unsubscribe from this group, send email to robofab-u...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/robofab?hl=en
>
> Messages from newly joined members are subject to moderation.
> ---
> You received this message because you are subscribed to the Google Groups "RoboFab" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to robofab+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

### Frederik Berlaen

Sep 3, 2013, 1:57:09 PM9/3/13
or in case you are using RoboFont you can use:

"""
g = CurrentGlyph()
closestPointFactory = g.naked().getRepresentation("doodle.closestPoint")
contourIndex, pointOnTheCurve = closestPointFactory.getClosestContourIndex((100, 100))
"""

but I guess Ive spoiled your nice evening finding it out :)

good luck!

gr Frederik
www.typemytype.com

### Jens Kutilek

Sep 4, 2013, 11:04:14 AM9/4/13
Thanks, Tal and Frederik,

I've built a pen for it. I love pens :)

The closestPointFactory doesn't do exactly what I wanted, which is the closest of the existing points (excluding off-curve points).

(The closestPointFactory returns the coordinate of the shortest distance from the reference point to the curve of the closest contour.)

Cheers,
Jens

> -----Original Message-----
> Behalf Of Frederik Berlaen
>
> or in case you are using RoboFont you can use:
>
> """
> g = CurrentGlyph()
> closestPointFactory = g.naked().getRepresentation("doodle.closestPoint")
> contourIndex, pointOnTheCurve =
> closestPointFactory.getClosestContourIndex((100, 100))
> """
>
> but I guess Ive spoiled your nice evening finding it out :)
>
> good luck!
>
> gr Frederik
> www.typemytype.com
>
>
>
> On 03 Sep 2013, at 19:33, Tal Leming wrote:
>
> > I think you'll have to bake your own.
> >
> > A custom point pen might be the easiest way to do it. You could store
> something like (x, y, contour index, point index) for all points passed through
> addPoint and then iterate over that when you test the coordinate. Or you could
> start the pen with the coordinate and as a point comes in through addPoint you
> could test the distance and store the contour index and point index if the
> distance is closer to the coordinate than the point that was formerly closest. If
> there is a direct hit, you can stop. Or... Lots of fun ways to do this. :-)