Skip to content

Commit

Permalink
[bezierTools] make curveCurveIntersections more precise, improve de-d…
Browse files Browse the repository at this point in the history
…uplication

#3354 (comment)
  • Loading branch information
anthrotype committed Nov 28, 2023
1 parent dad5bc6 commit bfd874a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
5 changes: 3 additions & 2 deletions Lib/fontTools/misc/bezierTools.py
Expand Up @@ -1323,7 +1323,8 @@ def midpoint(r):
return 0.5 * (r[0] + r[1])

# If they do overlap but they're tiny, approximate
if rectArea(bounds1) < precision and rectArea(bounds2) < precision:
precision2 = precision * precision
if rectArea(bounds1) < precision2 and rectArea(bounds2) < precision2:
return [(midpoint(range1), midpoint(range2))]

c11, c12 = _split_segment_at_t(curve1, 0.5)
Expand Down Expand Up @@ -1356,7 +1357,7 @@ def midpoint(r):
)
)

unique_key = lambda ts: (int(ts[0] / precision), int(ts[1] / precision))
unique_key = lambda ts: (round(ts[0] / precision), round(ts[1] / precision))
seen = set()
unique_values = []

Expand Down
6 changes: 4 additions & 2 deletions Tests/misc/bezierTools_test.py
Expand Up @@ -202,5 +202,7 @@ def test_intersections_linelike():
def test_curve_curve_touching_each_other():
seg1 = [(0, 0), (0, 100), (100, 100), (100, 0)]
seg2 = [(0, 150), (0, 50), (100, 50), (100, 150)]
pt = curveCurveIntersections(seg1, seg2)[0][0]
assert pt == pytest.approx((50.0, 75.0), rel=1e-2)
intersections = curveCurveIntersections(seg1, seg2)
assert len(intersections) == 1
pt = intersections[0][0]
assert pt == pytest.approx((50.0, 75.0), rel=1e-3)

0 comments on commit bfd874a

Please sign in to comment.