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__)
objc.loadBundleFunctions(bundle, globals(), [("GSIntersectBezier3Line", b"@{CGPoint=dd}{CGPoint=dd}{CGPoint=dd}{CGPoint=dd}{CGPoint=dd}{CGPoint=dd}")])

intersections = GSIntersectBezier3Line(NSPoint(0, 0), NSPoint(10, 10), NSPoint(10, 30), NSPoint(0, 40), NSPoint(60, 20), NSPoint(-10000, 20))
for i in intersections:
	print(i.pointValue())

It first loads a c-function from Glyphs (those are not automatically exposed by the pyobjc runtime).

To check if a point is left or right of a curve, make a line (the last two points in the functions arguments) from that point to a far away point with the same y and check if that line intersects with a curve segments. There can be up to three intersections. You might like to skip results with an even count.

2 Likes

Yes, this is perfect, thanks a lot!