# Find x coordinates from a given y

I have a brilliant idea for a new script, but I’m struggling with the most difficult part, which is to find a x coordinate of a segment from a given y (it assumes there are no multiple answers). I don’t need to add a point there, just want to know the x position of the point in the segment at certain height. Can anybody help?

You do know the bezier formula, right? Just find the t for your y, and then use the t to get the x.

Not this way. I’ve never needed to find the t from coordinates. I’ve been searching for the way to do this with no avail.

I ended up using brute force binary search to find it. As the script is going to do tons of this bit, I’d like to use more efficient method if there’s any. Sorry it’s kinda like stack overflow question, but trust me, it’s going to be a revolutionary script. I really mean it.

2 Likes

It sounds like you need to intersect the segment with a horizontal line. The line is at y height.

That could actually be closer to what I want, but how?

There is the Robofab MarginPen that does just that.
You can intersect any glyph at whatever x coordinate, an get the distances to the rigth or left sidebearings.
I’ve used it for my (old and abandoned) Fontlab Spacing Macro, have a look at https://github.com/impallari/Impallari-Fontlab-Macros/blob/master/IMP%20Spacing/Spacing%20Macro%20v4.py

It depends what you need. If you need the coordinates for the hole glyph, then you can use this:

layer.intersectionsBetweenPoints((-1000, y), (2000, y))

it will give you something that looks like the a measurement line. If you need it for a specific segment, you need to contact me by email.

Thanks both for help. Georg’s answer is exactly what I wanted!

Hi! I’m looking for a way to find whether a point is on the left or right from a certain curve/segment. Seems like finding the X on that curve from the known Y is a good part of it, but can’t figure out how to do it with curved paths. Any advice, please?

What’s the goal? Why do you need the left/right? Or do you mean inside/outside a path?

It’s part of a bigger tool, it needs to move all points located to the left (or right) of the certain curve. So ideally, would need to know the X on the curve from given Y, is that possible?

I thought about inside/ outside, but in this case it won’t work, since it just needs everything on either side.

Does this snippet help?

from AppKit import NSBundle, NSPoint
bundle = NSBundle.bundleForClass_(GSFont.__class__)