Skip to content

Commit

Permalink
Fix numerical stability for almost parallel lines, fixes #287
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Apr 15, 2024
1 parent e923d93 commit 9df26aa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
16 changes: 14 additions & 2 deletions path_intersection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,20 @@ func TestSelfIntersections(t *testing.T) {
//{"L10 10L10 0L0 10z", Intersections{ {Point{5.0, 5.0}, 1, 3, 0.5, 0.5, 0.25 * math.Pi, 0.75 * math.Pi, BintoA, NoParallel, false},
//}},

/// bugs
{"M3.512162397982181 1.239754268684486L3.3827323986701674 1.1467946944092953L3.522449858001167 1.2493787337129587A0.21166666666666667 0.21166666666666667 0 0 1 3.5121623979821806 1.2397542686844856z", []PathIntersection{}},
// parallel segment
{"L10 0L5 0L15 0", []PathIntersection{
{Point{5.0, 0.0}, 1, 0.5, 0.0, false, true, false},
{Point{10.0, 0.0}, 3, 0.5, 0.0, false, true, false},
}},
{"M-0.1997406229376793 296.9999999925494L-0.1997406229376793 158.88740153238177L-0.19974062293732664 158.8874019079834L-0.19999999999964735 20.77454766193328", []PathIntersection{
{Point{-0.1997406229376793, 158.88740172019808}, 1, 0.9999999986401219, 270.0 * math.Pi / 180.0, true, false, false},
{Point{-0.1997406229376793, 158.88740172019808}, 3, 1.359651237533596e-09, 269.9998923980606 * math.Pi / 180.0, false, false, false},
}}, // #287

// bugs
{"M3.512162397982181 1.239754268684486L3.3827323986701674 1.1467946944092953L3.522449858001167 1.2493787337129587A0.21166666666666667 0.21166666666666667 0 0 1 3.5121623979821806 1.2397542686844856z", []PathIntersection{
// TODO: add
}},
}
for _, tt := range tts {
t.Run(fmt.Sprint(tt.p), func(t *testing.T) {
Expand Down
9 changes: 5 additions & 4 deletions path_intersection_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ func pathIntersections(p, q *Path, withTangents, withParallelTangents bool) ([]P
})
}
} else {

// intersection is parallel
m := 0
for {
Expand Down Expand Up @@ -1031,13 +1032,12 @@ func intersectionLineLine(zs Intersections, a0, a1, b0, b1 Point) Intersections

da := a1.Sub(a0)
db := b1.Sub(b0)
div := da.PerpDot(db)
if Equal(div, 0.0) {
angle0 := da.Angle()
angle1 := db.Angle()
if angleEqual(angle0, angle1) || angleEqual(angle0, angle1+math.Pi) {
// parallel
if Equal(da.PerpDot(b1.Sub(a0)), 0.0) {
// aligned, rotate to x-axis
angle0 := da.Angle()
angle1 := db.Angle()
a := a0.Rot(-angle0, Point{}).X
b := a1.Rot(-angle0, Point{}).X
c := b0.Rot(-angle0, Point{}).X
Expand Down Expand Up @@ -1071,6 +1071,7 @@ func intersectionLineLine(zs Intersections, a0, a1, b0, b1 Point) Intersections
return zs
}

div := da.PerpDot(db)
ta := db.PerpDot(a0.Sub(b0)) / div
tb := da.PerpDot(a0.Sub(b0)) / div
if Interval(ta, 0.0, 1.0) && Interval(tb, 0.0, 1.0) {
Expand Down

0 comments on commit 9df26aa

Please sign in to comment.